Esempio n. 1
0
        public void ValidateSave()
        {
            WorldEntity world = createTestWorld();

            XElement xElement = WorldTransformer.Instance.ToXElement(world, TransformerSettings.WorldNamespace + "World");

            xElement.Save("unittestsave.xml", SaveOptions.OmitDuplicateNamespaces);

            XmlSchemaSet schemaSet = new XmlSchemaSet();

            schemaSet.Add(null, "WorldSchema1.xsd");

            XDocument xDocument = new XDocument(xElement);
            xDocument.AddAnnotation(SaveOptions.OmitDuplicateNamespaces);
            xDocument.Save("unittest2.xml", SaveOptions.OmitDuplicateNamespaces);

            XElement x1 = new XElement(TransformerSettings.WorldNamespace + "Outer");
            x1.Add(new XElement(TransformerSettings.WorldNamespace + "Inner"));
            x1.Save("unittest3.xml", SaveOptions.OmitDuplicateNamespaces);
            string val = "";
            xDocument.Validate(schemaSet, (o, vea) => {
                val += o.GetType().Name + "\n";
                val += vea.Message + "\n";
            }, true);

            Assert.AreEqual("", val);
        }
Esempio n. 2
0
 /// <summary>
 /// Создание объекта формирования xml файла
 /// </summary>
 /// <param name="workSheetName">Имя листа</param>
 public XMLExcel( string workSheetName )
 {
     _data = new XDocument( new XDeclaration( "1.0", "", "" ) );
     _data.AddAnnotation( "mso-application progid=\"Excel.Sheet\"" );
     _rootEX = new XElement( _ss + "Workbook",
         new XAttribute( "xmlns", _ss.NamespaceName ),
         new XAttribute( XNamespace.Xmlns + "o", _o.NamespaceName ),
         new XAttribute( XNamespace.Xmlns + "x", _x.NamespaceName ),
         new XAttribute( XNamespace.Xmlns + "ss", _ss.NamespaceName ),
         new XAttribute( XNamespace.Xmlns + "html", _html.NamespaceName ),
         new XElement( _o + "DocumentProperties",
             new XAttribute( "xmlns", _o.NamespaceName ),
             new XElement( _o + "Author", " " ),
             new XElement( _o + "Version", "12.00" ) ),
         new XElement( _x + "ExcelWorkbook",
             new XAttribute( "xmlns", _x.NamespaceName ),
             new XElement( _x + "ProtectStructure", "False" ),
             new XElement( _x + "ProtectWindows", "False" ) ),
         XElement.Parse( Resources.XMLExcelStyle ).FirstNode,
         new XElement( _ss + "Worksheet", new XAttribute( _ss + "Name", workSheetName ),
         _workTable = new XElement( _ss + "Table",
             new XAttribute( _x + "FullColumns", "1" ),
             new XAttribute( _x + "FullRows", "1" ),
             new XAttribute( _ss + "DefaultRowHeight", "15" ) ) ) );
     _data.AddFirst( _rootEX );
 }
Esempio n. 3
0
        /// <summary>
        /// Wrap an existing <see cref="XDocument"/> as an <see cref="XdmNode"/>. This is the <see cref="XDocument"/>
        /// equivalent of <see cref="DocumentBuilder.Wrap(XmlDocument)"/>.
        /// </summary>
        /// <remarks>
        /// PoC:
        /// 
        /// Creates wrapper objects for all nodes in the document graph and stores them using 
        /// <see cref="XObject.AddAnnotation(object)"/>. Will throw if any node has already been wrapped.
        /// 
        /// Idealy this would be an extension to <see cref="DocumentBuilder"/>, but DocumentBuilder does not expose
        /// its Configuration object publically.
        /// </remarks>
        public static XdmNode Wrap(this Processor processor, XDocument doc)
        {
            if (doc.Annotation<XObjectWrapper>() != null)
                throw new InvalidOperationException("XDocument is already annotated with a wrapper.");
            var docWrapper = (XDocumentWrapper)XObjectWrapper.MakeWrapper(doc);
            docWrapper.setConfiguration(processor.Implementation);
            doc.AddAnnotation(docWrapper);
            foreach (var node in doc.DescendantNodes())
            {
                if (node.Annotation<XObjectWrapper>() != null)
                    throw new InvalidOperationException(string.Format("{0} is already annotated with a wrapper.", node.GetType().Name));

                node.AddAnnotation(XObjectWrapper.MakeWrapper(node));

                if (node.NodeType == XmlNodeType.Element)
                {
                    foreach (var attr in ((XElement)node).Attributes())
                    {
                        if (attr.Annotation<XObjectWrapper>() != null)
                            throw new InvalidOperationException("Attribute is already annotated with a wrapper.");

                        attr.AddAnnotation(XObjectWrapper.MakeWrapper(attr));
                    }
                }
            }

            return (XdmNode)XdmValue.Wrap(docWrapper);
        }
Esempio n. 4
0
        /// <summary>
        /// create a new ship's data file
        /// </summary>
        /// <param name="shipname"></param>
        /// <param name="man"></param>
        /// <param name="power"></param>
        /// <param name="jump"></param>
        /// <param name="power"></param>
        /// <param name="cargo">int - cargo capacity in Dtons</param>
        /// <param name="credits">int - current credits</param>
        /// <param name="day">int - day (0..365)</param>
        /// <param name="jumpcost">int - cost per jump (fuel, life support, etc)</param>
        /// <param name="monthly">int - monthly costs (mortgage, maintenance, etc)</param>
        /// <param name="sec">string - SEC string of initial system</param>
        /// <param name="version">string - version: CT, MT, T5, CU</param>
        /// <param name="year">int - imperial year (i.e., 1105)</param>
        /// <param name="secfile">string - SEC format file</param>
        public Starship(string shipname, int man, int power, int jump, int cargo, int monthly, int jumpcost, int day, int year, 
            int credits, string version, string secfile, string sec, string sectorname, int tradeDM, bool illegals)
        {
            XDocument ns = new XDocument();
            XDeclaration dec = new XDeclaration("1.0", "utf-8", "yes");
            ns.AddAnnotation(dec);

            XElement rootNode = new XElement("ShipData");
            ns.Add(rootNode);

            ns.Element("ShipData").Add(
                new XElement("system",
                    new XAttribute("version", version),
                    new XElement("day", day.ToString()),
                    new XElement("year", year.ToString()),
                    new XElement("sec", sec),
                    new XElement("secfile", secfile),
                    new XElement("sectorName", sectorname),
                    new XElement("cargoID",0),
                    new XElement("tradeDM", tradeDM),
                    new XElement("illegals", illegals)));

            ns.Element("ShipData").Add(
                new XElement("Ship"));

            ns.Element("ShipData").Element("Ship").Add(
                    new XElement("Name", shipname),
                    new XElement("Manuever", man.ToString()),
                    new XElement("Power", power.ToString()),
                    new XElement("Jump", jump.ToString()),
                    new XElement("Cargo", cargo.ToString()),
                    new XElement("CargoHeld", "0"),
                    new XElement("credits", credits),
                    new XElement("costs",
                        new XElement("Monthly", monthly.ToString()),
                        new XElement("perJump", jumpcost.ToString()),
                        new XElement("lastPaid", String.Format("{0:000}-{1:0000}", day, year))));

            ns.Element("ShipData").Add(
                new XElement("Cargo"));
            ns.Element("ShipData").Add(
                new XElement("Travelogue"));

            ns.Save(shipname + ".xml");

            clearData();
        }
Esempio n. 5
0
 /// <summary>
 /// Gets the XDocument for a part	
 /// </summary>
 public static XDocument GetXDocument(this OpenXmlPart part)
 {
     XDocument xdoc = part.Annotation<XDocument>();
     if (xdoc != null)
         return xdoc;
     try
     {
         using (StreamReader sr = new StreamReader(part.GetStream()))
         using (XmlReader xr = XmlReader.Create(sr))
         {
             xdoc = XDocument.Load(xr);
             xdoc.Changed += ElementChanged;
             xdoc.Changing += ElementChanged;
         }
     }
     catch (XmlException)
     {
         xdoc = new XDocument();
         xdoc.AddAnnotation(new ChangedSemaphore());
     }
     part.AddAnnotation(xdoc);
     return xdoc;
 }
Esempio n. 6
0
        private static void SerializeWithSaveOptions(SerializeNode serialize, bool testXElement, bool testXDocument)
        {
            // Test both options at once as they don't really collide
            SaveOptions so = SaveOptions.DisableFormatting | SaveOptions.OmitDuplicateNamespaces;

            XElement root = XElement.Parse("<root xmlns:a='uri'><child xmlns:a='uri'><baby xmlns:a='uri'>text</baby></child></root>");
            XElement child = root.Element("child");
            XElement baby = child.Element("baby");
            XNode text = baby.FirstNode;

            // Verify that without annotation the output gets indented and the duplicate ns decls are not removed
            if (testXElement)
            {
                Assert.Equal(NormalizeNewLines(serialize(child)), "<child xmlns:a=\"uri\">  <baby xmlns:a=\"uri\">text</baby></child>");
            }

            // Now add annotation to the leaf element node
            // Even though it's in effect the output should stay the same (as there is only one namespace decl and mixed content).
            baby.AddAnnotation(so);

            if (testXElement)
            {
                Assert.Equal(serialize(baby), "<baby xmlns:a=\"uri\">text</baby>");
            }

            // Now add annotation to the middle node
            child.AddAnnotation(so);

            if (testXElement)
            {
                // Verify that the options are applied correctly
                Assert.Equal(NormalizeNewLines(serialize(child)), "<child xmlns:a=\"uri\"><baby>text</baby></child>");
                // Verify that the root node is not affected as we don't look for the annotation among descendants
                Assert.Equal(NormalizeNewLines(serialize(root)), "<root xmlns:a=\"uri\">  <child xmlns:a=\"uri\">    <baby xmlns:a=\"uri\">text</baby>  </child></root>");
            }

            // And now add the annotation to the root and remove it from the child to test that we can correctly skip over a node
            root.AddAnnotation(so);
            child.RemoveAnnotations(typeof(SaveOptions));

            if (testXElement)
            {
                // Verify that the options are still applied to child
                Assert.Equal(serialize(child), "<child xmlns:a=\"uri\"><baby>text</baby></child>");
                // And they should be also applied to the root now
                Assert.Equal(serialize(root), "<root xmlns:a=\"uri\"><child><baby>text</baby></child></root>");
            }

            // Add a document node above it all to test that it works on non-XElement as well
            XDocument doc = new XDocument(root);
            // Add the annotation to the doc and remove it from the root
            doc.AddAnnotation(so);
            root.RemoveAnnotations(typeof(SaveOptions));

            // Options should still apply to root as well as the doc
            if (testXElement)
            {
                Assert.Equal(serialize(root), "<root xmlns:a=\"uri\"><child><baby>text</baby></child></root>");
            }

            if (testXDocument)
            {
                Assert.Equal(serialize(doc), "<root xmlns:a=\"uri\"><child><baby>text</baby></child></root>");
            }
        }
Esempio n. 7
0
        // Used to track changes to parts

        /// <summary>
        /// Gets the XDocument for a part	
        /// </summary>
        public static XDocument GetXDocumentWithTracking(this OpenXmlPart part)
        {
            var xdoc = part.Annotation<XDocument>();
            if (xdoc != null)
                return xdoc;
            try
            {
                using (var sr = new StreamReader(part.GetStream()))
                using (XmlReader xr = XmlReader.Create(sr))
                {
                    xdoc = XDocument.Load(xr);
                    xdoc.Changed += ElementChanged;
                    xdoc.Changing += ElementChanged;
                }
            }
            catch (XmlException)
            {
                var xdec = new XDeclaration("1.0", "UTF-8", "yes");
                xdoc = new XDocument(xdec);
                xdoc.AddAnnotation(new ChangedSemaphore());
            }
            part.AddAnnotation(xdoc);
            return xdoc;
        }
Esempio n. 8
0
        /// <summary>
        /// Configures the instance document.
        /// </summary>
        void Initialize(XDocument document)
        {
            if (document != null)
            {
                if (model != null)
                    document.AddAnnotation(model.Interface<Model>());
                if (instance != null)
                    document.AddAnnotation(instance.Interface<Instance>());

                document.Validate(new XmlSchemaSet(), (s, a) => { }, true);
            }

            this.document = document;
        }
        private static string GetDefaultParagraphStyleName(XDocument stylesXDoc)
        {
            XElement defaultParagraphStyle;
            string defaultParagraphStyleName = null;

            StylesInfo stylesInfo = stylesXDoc.Annotation<StylesInfo>();

            if (stylesInfo != null)
                defaultParagraphStyleName = stylesInfo.DefaultParagraphStyleName;
            else
            {
                defaultParagraphStyle = stylesXDoc
                    .Root
                    .Elements(W.style)
                    .FirstOrDefault(s =>
                    {
                        if ((string)s.Attribute(W.type) != "paragraph")
                            return false;
                        var defaultAttribute = s.Attribute(W._default);
                        var isDefault = false;
                        if (defaultAttribute != null &&
                            (bool)s.Attribute(W._default).ToBoolean())
                            isDefault = true;
                        return isDefault;
                    });
                defaultParagraphStyleName = null;
                if (defaultParagraphStyle != null)
                    defaultParagraphStyleName = (string)defaultParagraphStyle.Attribute(W.styleId);
                stylesInfo = new StylesInfo()
                {
                    DefaultParagraphStyleName = defaultParagraphStyleName,
                };
                stylesXDoc.AddAnnotation(stylesInfo);
            }
            return defaultParagraphStyleName;
        }