Exemple #1
0
 private bool processText(string text)
 {
     foreach (PropertyInfo c in GetType().GetProperties())
     {
         foreach (XsAttributeAttribute a in CustomAttributeHelper.All <XsAttributeAttribute>(c))
         {
             if (a.Name.Length == 0)
             {
                 string s = (string)c.GetValue(this, null) ?? string.Empty;
                 if (_textFound)
                 {
                     s = s.TrimEnd() + Environment.NewLine + text.TrimStart();
                 }
                 else
                 {
                     s = text;
                 }
                 _textFound = true;
                 c.SetValue(this, s, null);
                 return(true);
             }
         }
     }
     return(false);
 }
Exemple #2
0
        /// Resolve collection property and node for the current XML node. Exception is thrown if nothing is found.
        protected void ResolveCollectionAndTypeForNode(XmlReader reader, IXsContext context, out PropertyInfo collection, out Type type)
        {
            foreach (PropertyInfo c in GetType().GetProperties())
            {
                foreach (XsElementAttribute e in CustomAttributeHelper.All <XsElementAttribute>(c))
                {
                    if (e.Name.Length == 0 && (string.IsNullOrEmpty(e.CollectionItemElementName) || string.Compare(e.CollectionItemElementName, reader.LocalName, StringComparison.OrdinalIgnoreCase) == 0))
                    {
                        collection = c;
                        type       = e.CollectionItemType;
                        if (context != null && (type == null || type.IsInterface || type.IsAbstract))
                        {
                            type = context.ResolveType(reader);
                        }
                        if (type == null || type.IsInterface || type.IsAbstract)
                        {
                            continue;
                        }
                        return;
                    }
                }
            }

            collection = null;
            type       = null;
            throw new XsException(reader, string.Format("Unknown xml element '{0}'", reader.Name));
        }
Exemple #3
0
        /// <summary>
        /// Write element text
        /// </summary>
        /// <param name="writer">XML writer</param>
        protected virtual void WriteText(XmlWriter writer)
        {
            // Write text
            string text = string.Empty;

            foreach (PropertyInfo c in GetType().GetProperties())
            {
                foreach (XsAttributeAttribute ab in CustomAttributeHelper.All <XsAttributeAttribute>(c))
                {
                    if (ab.Name == string.Empty)
                    {
                        object v = c.GetValue(this, null);
                        text += v;
                        break;
                    }
                }
            }
            if (text.Length > 0)
            {
                if (text.IndexOfAny("><&".ToCharArray()) != -1)
                {
                    writer.WriteCData(text);
                }
                else
                {
                    writer.WriteValue(text);
                }
            }
        }
Exemple #4
0
 private static bool hasText(PropertyInfo p)
 {
     foreach (XsAttributeAttribute a in CustomAttributeHelper.All <XsAttributeAttribute>(p))
     {
         if (a.Name == string.Empty) // Text
         {
             return(true);
         }
     }
     return(false);
 }
Exemple #5
0
        /// <summary>
        /// Write element to the output stream
        /// </summary>
        /// <param name="writer">Where to write</param>
        /// <param name="nameOverride">Local name to be used, or null if name should be retirevent from <see cref="XsTypeAttribute"/> of the type.</param>
        public virtual void WriteXml(XmlWriter writer, string nameOverride)
        {
            string namesp = null;

            if (nameOverride == null)
            {
                foreach (XsTypeAttribute a in CustomAttributeHelper.All <XsTypeAttribute>(GetType()))
                {
                    nameOverride = a.Name;
                    namesp       = a.Namespace;
                    break;
                }
                if (nameOverride == null)
                {
                    return;
                }
            }
            writer.WriteStartElement(nameOverride, namesp);

            WriteAttributes(writer);
            WriteText(writer);

            foreach (PropertyInfo c in GetOrderedElementProperties(GetType()))
            {
                object             v  = c.GetValue(this, null);
                IXsElement         o  = v as IXsElement;
                XsElementAttribute ab = CustomAttributeHelper.First <XsElementAttribute>(c);
                if (ab == null)
                {
                    continue;
                }
                if (ab.Name.Length == 0)
                {
                    IEnumerable e = (v as IEnumerable);
                    if (e != null)
                    {
                        foreach (IXsElement action in e)
                        {
                            if (action != null)
                            {
                                action.WriteXml(writer, ab.CollectionItemElementName);
                            }
                        }
                    }
                }
                else if (o != null && !(ab.SkipIfEmpty && ab.IsEmpty(v)))
                {
                    o.WriteXml(writer, ab.Name);
                }
            }

            writer.WriteEndElement();
        }
Exemple #6
0
 /// Try to find property matching the current XmlReader node or return null if not found.
 protected PropertyInfo FindRelatedProperty(XmlReader reader)
 {
     foreach (PropertyInfo c in GetType().GetProperties())
     {
         foreach (XsElementAttribute elements in CustomAttributeHelper.All <XsElementAttribute>(c))
         {
             if (string.Compare(elements.Name, reader.LocalName, StringComparison.OrdinalIgnoreCase) == 0)
             {
                 return(c);
             }
         }
     }
     return(null);
 }
Exemple #7
0
 /// Get collection property associated with the current XML node, or null
 protected PropertyInfo FindCollectionForNode(XmlReader reader)
 {
     foreach (PropertyInfo c in GetType().GetProperties())
     {
         foreach (XsElementAttribute e in CustomAttributeHelper.All <XsElementAttribute>(c))
         {
             if (e.Name.Length == 0 && (string.IsNullOrEmpty(e.CollectionItemElementName) || string.Compare(e.CollectionItemElementName, reader.LocalName, StringComparison.OrdinalIgnoreCase) == 0))
             {
                 return(c);
             }
         }
     }
     return(null);
 }
Exemple #8
0
 private static bool allowsText(Type t)
 {
     foreach (var pi in t.GetProperties())
     {
         foreach (var att in CustomAttributeHelper.All <XsAttributeAttribute>(pi))
         {
             if (string.IsNullOrEmpty(att.Name))
             {
                 return(true);
             }
         }
     }
     return(false);
 }
Exemple #9
0
        /// Return names of XML attributes associated with this property, or null if no attributes are found
        public static string[] GetNames(PropertyInfo pi, bool includingDeprecated)
        {
            var v = CustomAttributeHelper.All <XsAttributeAttribute>(pi);

            if (v != null && v.Length > 0)
            {
                int cnt = 0;
                for (int i = 0; i < v.Length; ++i)
                {
                    if (includingDeprecated || v[i].Deprecated == false)
                    {
                        cnt++;
                    }
                }

                if (cnt == 0)
                {
                    return(null);
                }

                var ret = new string[cnt];
                var n   = 0;
                for (int i = 0; i < v.Length; ++i)
                {
                    if (includingDeprecated || v[i].Deprecated == false)
                    {
                        ret[n++] = v[i].Name;
                    }
                }
                return(ret);
            }
            if (CustomAttributeHelper.Has <XmlIgnoreAttribute>(pi) || !pi.CanRead || !pi.CanWrite || pi.GetIndexParameters().Length != 0 || pi.GetSetMethod() == null)
            {
                return(null);
            }
            if (CustomAttributeHelper.Has <XsElementAttribute>(pi))
            {
                return(null);
            }
            return(new string[] { (pi.Name.Substring(0, 1).ToLowerInvariant() + pi.Name.Substring(1)) });
        }
Exemple #10
0
        /// <summary>
        /// Initialize action
        /// </summary>
        public virtual void Initialize()
        {
            foreach (PropertyInfo c in GetType().GetProperties())
            {
                bool   valSet = false;
                object val    = null;
                foreach (XsElementAttribute e in CustomAttributeHelper.All <XsElementAttribute>(c))
                {
                    if (!valSet)
                    {
                        val    = c.GetValue(this, null);
                        valSet = true;
                    }
                    ScriptActionBase bl = val as ScriptActionBase;
                    if (bl != null && bl._elementName == null)
                    {
                        bl._elementName = e.Name;
                    }
                    continue;
                }

                if (CustomAttributeHelper.Has <XsRequiredAttribute>(c))
                {
                    if (!valSet)
                    {
                        if (c.GetIndexParameters().Length > 0)
                        {
                            continue;
                        }

                        val = c.GetValue(this, null);
                    }
                    if (val == null)
                    {
                        throw new ParsingException(string.Format("Required attribute '{0}' is not set", XsAttributeAttribute.GetNames(c, false)[0]));
                    }
                }
                if ((c.PropertyType == typeof(object) || c.PropertyType == typeof(string)) &&
                    !CustomAttributeHelper.Has <XsNotTransformed>(c) && !CustomAttributeHelper.Has <XmlIgnoreAttribute>(c) &&
                    c.GetSetMethod() != null)
                {
                    if (!valSet)
                    {
                        if (c.GetIndexParameters().Length > 0)
                        {
                            continue;
                        }

                        val = c.GetValue(this, null);
                    }
                    if (val != null)
                    {
                        if (val is string)
                        {
                            Context.AssertGoodTransform((string)val, Transform);
                        }
                        else
                        {
                            Context.AssertGoodTransform(val.ToString(), Transform);
                        }
                    }
                }
            }
        }
Exemple #11
0
        ///<summary>Generate XML schema for the given types
        ///</summary>
        ///<param name="ns">Default namespace</param>
        ///<param name="types">Types to include into schema</param>
        ///<param name="root">Root element</param>
        ///<param name="interfaces">Interface types</param>
        ///<returns>Built schema</returns>
        public static XmlSchema BuildSchema(string ns, Type[] types, Type root, Type[] interfaces)
        {
            XmlSchema xmlSchema = new XmlSchema();

            xmlSchema.Namespaces.Add("xsd", "http://www.w3.org/2001/XMLSchema");
            xmlSchema.Namespaces.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
            xmlSchema.ElementFormDefault   = XmlSchemaForm.Qualified;
            xmlSchema.AttributeFormDefault = XmlSchemaForm.Unqualified;
            xmlSchema.Namespaces.Add("ns", ns);
            xmlSchema.TargetNamespace = ns;

            // Comment
            XmlSchemaAnnotation    annotation     = new XmlSchemaAnnotation();
            XmlSchemaDocumentation documentation  = new XmlSchemaDocumentation();
            XmlDocument            helperDocument = new XmlDocument();
            string comment = String.Format("  XML schema for {0} , generated at {1}  ", ns, DateTime.Now.ToString());

            documentation.Markup = new XmlNode[1] {
                helperDocument.CreateComment(comment)
            };
            annotation.Items.Add(documentation);
            xmlSchema.Items.Add(annotation);

            // Create group "action" to refer to any action
            var ints = new Dictionary <Type, XmlSchemaGroup>();

            if (interfaces != null)
            {
                foreach (var intf in interfaces)
                {
                    var action = new XmlSchemaGroup();
                    action.Name     = getXmlTypeName(intf);
                    action.Particle = new XmlSchemaChoice();
                    xmlSchema.Items.Add(action);
                    ints.Add(intf, action);
                }
            }

            Dictionary <Type, XmlSchemaType> xmlTypes = new Dictionary <Type, XmlSchemaType>();

            foreach (var type in types)
            {
                // If it does not have our XML header - skip it
                var na = (CustomAttributeHelper.First <XsTypeAttribute>(type));
                if (na == null)
                {
                    continue;
                }


                // Check if it is complex or simple
                XmlSchemaComplexType ct = new XmlSchemaComplexType();
                ct.Name = getXmlTypeName(type);


                XmlSchemaObjectCollection attr = createComplexType(type, ct, ns, ints);

                // Add the new element as an option to the "action" group
                foreach (var i in ints)
                {
                    bool isAction = (type.FindInterfaces((tp, nu) => tp == i.Key, null).Length != 0);
                    if (isAction)
                    {
                        foreach (var tp in CustomAttributeHelper.All <XsTypeAttribute>(type))
                        {
                            if (!string.IsNullOrEmpty(tp.Name))
                            {
                                i.Value.Particle.Items.Add(new XmlSchemaElement
                                {
                                    Name           = tp.Name,
                                    MinOccurs      = 0,
                                    SchemaTypeName = new XmlQualifiedName(ct.Name, ns)
                                });
                            }
                        }
                    }
                }

                // Work with attributes
                foreach (var o in generateAttributes(xmlSchema, type, xmlTypes, ns))
                {
                    attr.Add(o);
                }

                if (na.AnyAttribute)
                {
                    ct.AnyAttribute = new XmlSchemaAnyAttribute
                    {
                        ProcessContents = XmlSchemaContentProcessing.Skip
                    };
                }


                // Add type to the list
                xmlTypes.Add(type, ct);
                xmlSchema.Items.Add(ct);

                if (root.IsAssignableFrom(type))
                {
                    // Add all variations of Script names as element
                    foreach (var o in CustomAttributeHelper.All <XsTypeAttribute>(root))
                    {
                        xmlSchema.Items.Add(new XmlSchemaElement
                        {
                            Name           = o.Name,
                            SchemaTypeName = new XmlQualifiedName(xmlTypes[typeof(Script)].Name, ns)
                        });
                    }
                }
            }
            return(xmlSchema);
        }