示例#1
0
        public void Read(CircuitDiagramDocument document, Stream stream)
        {
            var context = new ReaderContext();

            var xml  = XDocument.Load(XmlReader.Create(stream));
            var root = xml.Root;

            var propertiesEl = (from el in root.Elements()
                                where el.Name == Ns.Document + "properties"
                                select el).Single();

            propertiesReader.ReadProperties(document, propertiesEl, context);

            var definitionsEl = (from el in root.Elements()
                                 where el.Name == Ns.Document + "definitions"
                                 select el).Single();

            definitionsReader.ReadDefinitions(document, definitionsEl, context);

            var elementsEl = (from el in root.Elements()
                              where el.Name == Ns.Document + "elements"
                              select el).Single();

            elementsReader.ReadElements(document, elementsEl, context);
        }
示例#2
0
        public CircuitDiagramDocument ReadCircuit(Stream stream)
        {
            var document = new CircuitDiagramDocument();

            mainDocumentReader.Read(document, stream);

            return(document);
        }
        public CircuitDiagramDocument ReadCircuit(Stream stream)
        {
            var document = new CircuitDiagramDocument();

            using (var package = Package.Open(stream, FileMode.Open, FileAccess.Read))
            {
                var mainDocumentPart = package.GetPart(new Uri(@"/circuitdiagram/Document.xml", UriKind.Relative));
                using (var mainDoc = mainDocumentPart.GetStream())
                    mainDocumentReader.Read(document, mainDoc);
            }

            return(document);
        }
示例#4
0
        public void ReadDefinitions(CircuitDiagramDocument document,
                                    XElement definitions,
                                    ReaderContext context)
        {
            var sources = from el in definitions.Elements()
                          where el.Name == Ns.Document + "src"
                          select el;

            foreach (var source in sources)
            {
                ComponentTypeCollection collection = null;
                var collectionAttr = source.Attribute("col");
                if (collectionAttr != null)
                {
                    Uri collectionUri;
                    if (Uri.TryCreate(collectionAttr.Value, UriKind.Absolute, out collectionUri))
                    {
                        collection = new ComponentTypeCollection(collectionUri);
                    }
                    else
                    {
                        context.Log(ReaderErrorCodes.UnableToParseValueAsUri, collectionAttr, collectionAttr.Value);
                    }
                }

                foreach (var componentType in source.Elements().Where(el => el.Name == Ns.Document + "add"))
                {
                    var idAttr = componentType.Attribute("id");
                    if (idAttr == null)
                    {
                        context.Log(ReaderErrorCodes.MissingRequiredAttribute, componentType, "id");
                        continue;
                    }
                    string id = idAttr.Value;

                    var guid           = componentType.GetGuidAttribute(Ns.DocumentComponentDescriptions + "guid", context);
                    var collectionItem = componentType.GetCollectionItemAttribute("item", context);
                    var componentName  =
                        componentType.GetComponentNameAttribute(Ns.DocumentComponentDescriptions + "name", context);

                    context.RegisterComponentType(id, new ComponentType(guid,
                                                                        collection,
                                                                        collectionItem,
                                                                        componentName,
                                                                        new List <PropertyName>(),
                                                                        new List <ConnectionName>(),
                                                                        new List <ComponentConfiguration>()));
                }
            }
        }
        public void SetUp()
        {
            var resourceName = Assembly.GetExecutingAssembly().GetManifestResourceNames().First(x => x.EndsWith("Data.TestCircuit.cddx"));

            using (var resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
                using (var ms = new MemoryStream())
                {
                    resourceStream.CopyTo(ms);
                    ms.Seek(0, SeekOrigin.Begin);

                    var reader = new CircuitDiagramDocumentReader();
                    document = reader.ReadCircuit(ms);
                }
        }
示例#6
0
        public void ReadProperties(CircuitDiagramDocument document,
                                   XElement properties,
                                   ReaderContext context)
        {
            int widthValue  = 0;
            int heightValue = 0;

            try
            {
                var widthEl = (from el in properties.Elements()
                               where el.Name == Namespaces.Document + "width"
                               select el).Single();


                if (!int.TryParse(widthEl.Value, out widthValue))
                {
                    context.Log(ReaderErrorCodes.UnableToParseValueAsInt, widthEl, widthEl.Value);
                }
            }
            catch (InvalidOperationException ex) when(ex.Message.Contains("more than one"))
            {
                context.Log(ReaderErrorCodes.DuplicateElement, properties, "width");
            }
            catch (InvalidOperationException ex) when(ex.Message.Contains("no matching"))
            {
                context.Log(ReaderErrorCodes.MissingRequiredElement, properties, "width");
            }

            try
            {
                var heightEl = (from el in properties.Elements()
                                where el.Name == Namespaces.Document + "height"
                                select el).Single();

                if (!int.TryParse(heightEl.Value, out heightValue))
                {
                    context.Log(ReaderErrorCodes.UnableToParseValueAsInt, heightEl, heightEl.Value);
                }
            }
            catch (InvalidOperationException ex) when(ex.Message.Contains("more than one"))
            {
                context.Log(ReaderErrorCodes.DuplicateElement, properties, "height");
            }
            catch (InvalidOperationException ex) when(ex.Message.Contains("no matching"))
            {
                context.Log(ReaderErrorCodes.MissingRequiredElement, properties, "height");
            }

            document.Size = new Size(widthValue, heightValue);
        }
示例#7
0
        public void ReadDefinitions(CircuitDiagramDocument document,
                                    XElement definitions,
                                    ReaderContext context)
        {
            var sources = from el in definitions.Elements()
                          where el.Name == Ns.Document + "src"
                          select el;

            foreach (var source in sources)
            {
                var collection     = ComponentType.UnknownCollection;
                var collectionAttr = source.Attribute("col");
                if (collectionAttr != null)
                {
                    if (!Uri.TryCreate(collectionAttr.Value, UriKind.Absolute, out collection))
                    {
                        context.Log(ReaderErrorCodes.UnableToParseValueAsUri, collectionAttr, collectionAttr.Value);
                    }
                }

                foreach (var componentType in source.Elements().Where(el => el.Name == Ns.Document + "add"))
                {
                    var idAttr = componentType.Attribute("id");
                    if (idAttr == null)
                    {
                        context.Log(ReaderErrorCodes.MissingRequiredAttribute, componentType, "id");
                        continue;
                    }
                    string id = idAttr.Value;

                    var guid           = componentType.GetGuidAttribute(Ns.DocumentComponentDescriptions + "guid", context);
                    var collectionItem = componentType.GetCollectionItemAttribute("item", context);
                    var name           = componentType.GetComponentNameAttribute("name", context);

                    var type = collectionItem != null ? new ComponentType(collection ?? ComponentType.UnknownCollection, collectionItem) : ComponentType.Unknown(name ?? guid.ToString());

                    if (guid.HasValue)
                    {
                        type = new TypeDescriptionComponentType(guid ?? Guid.Empty, type);
                    }

                    context.RegisterComponentType(id, type);
                }
            }
        }
示例#8
0
        public void ReadElements(CircuitDiagramDocument document,
                                 XElement elements,
                                 ReaderContext context)
        {
            var components = from el in elements.Elements()
                             where el.Name == Ns.Document + "c"
                             select el;

            foreach (var componentElement in components)
            {
                var typeAttr = componentElement.Attribute("tp");
                if (typeAttr == null)
                {
                    context.Log(ReaderErrorCodes.MissingRequiredAttribute, componentElement, "tp");
                    continue;
                }

                var componentType = context.GetComponentType(ParseType(typeAttr.Value));
                if (componentType == null)
                {
                    context.Log(ReaderErrorCodes.UnknownComponentType, typeAttr, typeAttr.Value);
                    continue;
                }

                Component component;

                if (componentElement.Attribute("x") != null)
                {
                    component = new PositionalComponent(componentType, new LayoutInformation());

                    // Layout
                    ReadLayout((PositionalComponent)component, componentElement, context);
                }
                else
                {
                    component = new Component(componentType);
                }

                // Properties

                var propertiesElement = componentElement.Elements(Ns.Document + "prs").SingleOrDefault();
                var properties        = propertiesElement != null
                    ? from el in propertiesElement.Elements()
                                        where el.Name == Ns.Document + "p"
                                        select el
                    : new XElement[0];

                foreach (var propertyElement in properties)
                {
                    var keyAttr = propertyElement.Attribute("k");
                    if (keyAttr == null)
                    {
                        context.Log(ReaderErrorCodes.MissingRequiredAttribute, propertyElement, "k");
                        continue;
                    }

                    var valueAttr = propertyElement.Attribute("v");
                    if (valueAttr == null)
                    {
                        context.Log(ReaderErrorCodes.MissingRequiredAttribute, propertyElement, "v");
                        continue;
                    }

                    component.Properties[keyAttr.Value] = PropertyValue.Dynamic(valueAttr.Value);
                }

                // Connections

                var connectionsElement = componentElement.Elements(Ns.Document + "cns").SingleOrDefault();
                var connections        = connectionsElement != null
                    ? from el in connectionsElement.Elements()
                                         where el.Name == Ns.Document + "cn"
                                         select el
                    : new XElement[0];

                foreach (var connectionElement in connections)
                {
                    var idAttr = connectionElement.Attribute("id");
                    if (idAttr == null)
                    {
                        context.Log(ReaderErrorCodes.MissingRequiredAttribute, connectionElement, "id");
                        continue;
                    }

                    var pointAttr = connectionElement.Attribute("pt");
                    if (pointAttr == null)
                    {
                        context.Log(ReaderErrorCodes.MissingRequiredAttribute, connectionElement, "pt");
                        continue;
                    }

                    var namedConnection =
                        component.Connections.FirstOrDefault(x => x.Value.Name.Value == pointAttr.Value).Value ?? new NamedConnection(pointAttr.Value, component);
                    context.ApplyConnection(idAttr.Value, namedConnection);
                }

                document.Elements.Add(component);
            }

            var wires = from el in elements.Elements()
                        where el.Name == Ns.Document + "w"
                        select el;

            foreach (var wireElement in wires)
            {
                var wire = new Wire(new LayoutInformation());
                ReadLayout(wire, wireElement, context);

                document.Elements.Add(wire);
            }
        }
示例#9
0
 public void Setup()
 {
     reader   = new DefinitionsReader();
     context  = new ReaderContext();
     document = new CircuitDiagramDocument();
 }
 public void Setup()
 {
     reader   = new PropertiesReader();
     context  = new ReaderContext();
     document = new CircuitDiagramDocument();
 }