コード例 #1
0
ファイル: Map.cs プロジェクト: henrihs/bluebrick-id-fork
        public void ReadXml(System.Xml.XmlReader reader)
        {
            // reset the counter of modifications because we just load the map (no modification done)
            mNumberOfModificationSinceLastSave = 0;
            // version
            readVersionNumber(reader);
            // check if the BlueBrick program is not too old, that
            // means the user try to load a file generated with
            /// a earlier version of BlueBrick
            if (mDataVersionOfTheFileLoaded > CURRENT_DATA_VERSION)
            {
                MessageBox.Show(null, Properties.Resources.ErrorMsgProgramObsolete,
                    Properties.Resources.ErrorMsgTitleError, MessageBoxButtons.OK,
                    MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                return;
            }
            // get the number of layer for the progressbar
            if (mDataVersionOfTheFileLoaded >= 3)
            {
                int nbItems = reader.ReadElementContentAsInt();
                // init the progress bar with the real number of layer items (+1 for the header +1 for the link rebuilding)
                MainForm.Instance.resetProgressBar(nbItems + 2);
            }
            // check is there is a background color
            if (reader.Name.Equals("BackgroundColor"))
                mBackgroundColor = XmlReadWrite.readColor(reader);
            // data of the map
            mAuthor = reader.ReadElementContentAsString();
            mLUG = reader.ReadElementContentAsString();
            mShow = reader.ReadElementContentAsString();
            reader.ReadToDescendant("Day");
            int day = reader.ReadElementContentAsInt();
            int month = reader.ReadElementContentAsInt();
            int year = reader.ReadElementContentAsInt();
            mDate = new DateTime(year, month, day);
            // read the comment if the version is greater than 0
            if (mDataVersionOfTheFileLoaded > 0)
            {
                reader.ReadToFollowing("Comment");
                mComment = reader.ReadElementContentAsString().Replace("\n", Environment.NewLine);
            }
            else
            {
                reader.ReadToFollowing("CurrentSnapGridSize");
            }
            if (mDataVersionOfTheFileLoaded < 2)
            {
                // skip the static data of layers that before we were saving
                // but now I think it is stupid, since we don't have action to change that
                // and we don't have way to update the enabled of the buttons
                reader.ReadElementContentAsFloat(); // CurrentSnapGridSize
                reader.ReadElementContentAsBoolean(); // SnapGridEnabled
                reader.ReadElementContentAsFloat(); // CurrentRotationStep
            }

            // read the export data if the version is 5 or higher
            if (mDataVersionOfTheFileLoaded > 4)
            {
                reader.ReadToDescendant("ExportPath");
                // read the relative export path and store it temporarly in the absolute path variable
                // the absolute path will be computed after the xml serialization is finished
                mExportAbsoluteFileName = reader.ReadElementContentAsString();
                // read the other export info
                mExportFileTypeIndex = reader.ReadElementContentAsInt();
                mExportArea = XmlReadWrite.readRectangleF(reader);
                mExportScale = reader.ReadElementContentAsFloat();
                // read even more info from version 8
                if (mDataVersionOfTheFileLoaded > 7)
                {
                    mExportWatermark = XmlReadWrite.readBoolean(reader);
                    mExportBrickHull = XmlReadWrite.readBoolean(reader);
                    mExportElectricCircuit = XmlReadWrite.readBoolean(reader);
                    mExportConnectionPoints = XmlReadWrite.readBoolean(reader);
                }
                reader.ReadEndElement();
            }

            // selected layer
            int selectedLayerIndex = reader.ReadElementContentAsInt();
            // step the progress bar after the read of the header
            MainForm.Instance.stepProgressBar();

            // layers
            // first clear the hashtable that contains all the bricks
            Map.sHashtableForRulerAttachementRebuilding.Clear();
            // then load all the layers
            bool layerFound = reader.ReadToDescendant("Layer");
            while (layerFound)
            {
                // get the 'type' attribute of the layer
                reader.ReadAttributeValue();
                string layerType = reader.GetAttribute(0);

                // instantiate the right layer according to the type
                Layer layer = null;
                if (layerType.Equals("grid"))
                    layer = new LayerGrid();
                else if (layerType.Equals("brick"))
                    layer = new LayerBrick();
                else if (layerType.Equals("text"))
                    layer = new LayerText();
                else if (layerType.Equals("area"))
                    layer = new LayerArea();
                else if (layerType.Equals("ruler"))
                    layer = new LayerRuler();

                // read and add the new layer
                if (layer != null)
                {
                    layer.ReadXml(reader);
                    mLayers.Add(layer);
                }

                // read the next layer
                layerFound = reader.ReadToNextSibling("Layer");
            }
            reader.ReadEndElement(); // end of Layers

            // once we have finished to read all the layers thus all the items, we need to recreate all the links they have between them
            foreach (Layer layer in mLayers)
                layer.recreateLinksAfterLoading();
            // then clear again the hash table to free the memory
            Map.sHashtableForRulerAttachementRebuilding.Clear();

            // step the progress bar after the rebuilding of links
            MainForm.Instance.stepProgressBar();

            // if the selected index is valid, reset the selected layer
            // use the setter in order to enable the toolbar buttons
            if ((selectedLayerIndex >= 0) && (selectedLayerIndex < mLayers.Count))
                SelectedLayer = mLayers[selectedLayerIndex];
            else
                SelectedLayer = null;

            // DO NOT READ YET THE BRICK URL LIST, BECAUSE THE BRICK DOWNLOAD FEATURE IS NOT READY
            if (false)
            {
                // read the url of all the parts for version 5 or later
                if ((mDataVersionOfTheFileLoaded > 5) && !reader.IsEmptyElement)
                {
                    bool urlFound = reader.ReadToDescendant("BrickUrl");
                    while (urlFound)
                    {
                        // read the next url
                        urlFound = reader.ReadToNextSibling("BrickUrl");
                    }
                    reader.ReadEndElement();
                }
            }

            // construct the watermark
            computeGeneralInfoWatermark();
            // for old version, make disapear the progress bar, since it was just an estimation
            MainForm.Instance.finishProgressBar();
        }
コード例 #2
0
ファイル: FastXmlReader.cs プロジェクト: Fedorm/core-master
        private static Entity ReadRow(Entity parent, System.Xml.XmlTextReader r)
        {
            if (parent != null && r.AttributeCount <= 1) //tabular section, no attributes or "key"
            {
                String tsName = r.Name;

                if (r.MoveToFirstAttribute())
                {
                    if (r.Name.ToLower().Equals("key")) //only key attrbute is allowed
                        parent.AddTabularSection(tsName, r.Value);
                    else
                        throw new Exception(String.Format("Invalid tabular section attribute '{0}'", r.Name));
                }
                else
                    parent.AddTabularSection(tsName);

                return parent;
            }
            else
            {
                Entity entity = new Entity(r.Depth);
                if (r.MoveToFirstAttribute())
                {
                    do
                    {
                        String name = r.Name;
                        r.ReadAttributeValue();
                        entity.Attributes.Add(name, r.ReadContentAsString());
                    }
                    while (r.MoveToNextAttribute());
                }

                if (parent != null)
                {
                    parent.CurrentTabularSection.AddEntity(entity);
                    return parent;
                }
                else
                    return entity;
            }
        }
コード例 #3
0
            public override void ReadXml(System.Xml.XmlReader reader)
            {
                // read the id of the brick, then add this brick in the hashtable
                if (Map.DataVersionOfTheFileLoaded >= 7)
                {
                    int brickId = int.Parse(reader.GetAttribute(0));
                    Id = brickId.ToString();
                    Map.sHashtableForRulerAttachementRebuilding.Add(brickId, this);
                }
                // read the base class
                base.ReadXml(reader);
                // avoid using the accessor to reduce the number of call of updateBitmap
                mPartNumber = BrickLibrary.Instance.getActualPartNumber(reader.ReadElementContentAsString().ToUpperInvariant());
                // but then update its electric list
                mElectricCircuitIndexList = BrickLibrary.Instance.getElectricCircuitList(mPartNumber);
                mOrientation = reader.ReadElementContentAsFloat();
                mActiveConnectionPointIndex = reader.ReadElementContentAsInt();
                // the altitude
                if (Map.DataVersionOfTheFileLoaded >= 3)
                    mAltitude = reader.ReadElementContentAsFloat();
                // update the bitmap
                updateImage();
                updateSnapMargin();
                // read the connexion points if any
                reader.ReadAttributeValue();
                int count = int.Parse(reader.GetAttribute(0));

                // check the number of connection is the same in the Brick library and in the loading file.
                // They can be different if the file was saved with an old part library and then one part
                // was updated to add or remove connection. So there is 3 different cases:
                // - if they are the same: no problems.
                // - if there is more connections in the part lib than in the file: we reserve enough space in the list,
                // based on the library value, and then we will add empty connections instances to fullfill the list
                // after finishing reading the connection tag.
                // - if there is more parts in the file than in the list, we need to discard some connection,
                // so we add a check in the parsing to create the last connections as default one (of type brick).
                // This will ensure that the link will be broken (because all connections or type BRICK are
                // broken after the loading of the file is finished) and then the GC will destroy these connections.
                // And of course the list is reserved based on the library value.

                // So first, we ask the number of connection to the part lib then allocate
                // the list of connection based on the number set in the library and not the number
                // read in the file, because finally the number of connection must be like the part lib says
                int connectionCountInBrickLibrary = BrickLibrary.Instance.getConnectionCount(mPartNumber);
                if (connectionCountInBrickLibrary > 0)
                    mConnectionPoints = new List<ConnectionPoint>(connectionCountInBrickLibrary);

                // now check if we need to parse some connection in the file
                if (count > 0)
                {
                    // declare a counter for the connections
                    int connexionIndex = 0;
                    bool connexionFound = reader.ReadToDescendant("Connexion");
                    while (connexionFound)
                    {
                        // a boolean saying if the current connection is valid or will be destroyed later
                        // because it is over the number indicated by the part library
                        // be careful mConnectionPoints can be null, so use the int var instead
                        bool isConnectionValid = (connexionIndex < connectionCountInBrickLibrary);

                        // read the id (hashcode key) of the connexion
                        reader.ReadAttributeValue();
                        String id = reader.GetAttribute(0);
                        int hashCode = int.Parse(id.Substring(1));

                        // look in the hastable if this connexion alread exists, else create it
                        ConnectionPoint connexion = ConnectionPoint.sHashtableForLinkRebuilding[hashCode] as ConnectionPoint;
                        if (connexion == null)
                        {
                            // instanciate a ConnectionPoint, and add it in the hash table
                            if (isConnectionValid)
                                connexion = new ConnectionPoint(this, connexionIndex);
                            else
                                connexion = new ConnectionPoint();
                            ConnectionPoint.sHashtableForLinkRebuilding.Add(hashCode, connexion);
                        }
                        else
                        {
                            // set the connexion type, if not set during the above creation
                            if (isConnectionValid)
                                connexion.Type = BrickLibrary.Instance.getConnexionType(this.PartNumber, connexionIndex);
                        }

                        //read the connexion data and add it in the Connection list
                        connexion.mMyBrick = this;
                        connexion.ReadXml(reader);

                        // during the reading of the connection list in the file, we check if
                        // we didn't reached the limit of the part library. If there is more connection
                        // in the file than in the part lib, we continue to read the connections,
                        // but we don't add them in the connection list.
                        if (isConnectionValid)
                            mConnectionPoints.Add(connexion);

                        // increment the connexion index
                        connexionIndex++;

                        // read the next brick
                        connexionFound = reader.ReadToNextSibling("Connexion");
                    }
                    reader.ReadEndElement();

                    // check if we read all the connections in the file, if not we have to instanciate
                    // empty connection to fullfill the list
                    if (mConnectionPoints != null)
                        for (int i = mConnectionPoints.Count; i < mConnectionPoints.Capacity; ++i)
                        {
                            ConnectionPoint connexion = new ConnectionPoint(this, i);
                            mConnectionPoints.Add(connexion);
                            // we don't need to add this connection in the hastable since we know this
                            // connection doesn't exist in the file, so there is no link attached to it
                        }

                    // update the connexion position which is not stored in the bbm file
                    // in file version before 3 it was stored, but I removed it because the connexion
                    // point can move in different part libraries
                    updateConnectionPosition();
                }
                else
                {
                    reader.Read();
                }
                // read the end element of the brick
                reader.ReadEndElement();
            }