Ejemplo n.º 1
0
        public bool ReadRaw(XmlReader reader,
                            SifVersion version,
                            SifElement parent,
                            SifFormatter formatter)
        {
            string name = reader.LocalName;

            if (name.Equals(fDateElement))
            {
                String   dateValue = ConsumeElementTextValue(reader, version);
                DateTime?date      = formatter.ToDate(dateValue);
                if (date.HasValue)
                {
                    DateTime    tsValue  = date.Value;
                    SifDateTime dateTime = new SifDateTime(tsValue);
                    parent.SetField(dateTime.CreateField(parent, fElementDef));
                }
            }
            else if (name.Equals(fTimeElement))
            {
                String   timeValue = ConsumeElementTextValue(reader, version);
                DateTime?time      = formatter.ToTime(timeValue);
                if (time.HasValue)
                {
                    DateTime val = time.Value;
                    // See if the Timestamp field already exists on the parent
                    SimpleField timeStampField = parent.GetField(fElementDef);
                    if (timeStampField == null)
                    {
                        // Doesn't exist, create it
                        SifDateTime dateTime = new SifDateTime(val);
                        parent.SetField(dateTime.CreateField(parent, fElementDef));
                    }
                    else
                    {
                        // Exists, update the time portion of the date
                        SifDateTime sdt = (SifDateTime)timeStampField.SifValue;
                        if (sdt != null && sdt.Value.HasValue)
                        {
                            DateTime tsValue = sdt.Value.Value;
                            tsValue = tsValue.Add(val.TimeOfDay);
                            sdt     = new SifDateTime(tsValue);
                            // Overwrite the current value
                            parent.SetField(sdt.CreateField(parent, fElementDef));
                        }
                    }
                }
            }
            else
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 2
0
        private void SetFieldValueFromAttribute(
            SifElement element,
            XmlReader reader,
            IDtd dtd,
            SifVersion version,
            SifFormatter formatter,
            IZone zone)
        {
            IElementDef elementDef = element.ElementDef;
            IElementDef field      = dtd.LookupElementDef(element.ElementDef, reader.LocalName);

            if (field == null && reader.Prefix != null)
            {
                if (reader.LocalName == SifWriter.NIL && reader.NamespaceURI == XmlSchema.InstanceNamespace)
                {
                    TypeConverter converter = elementDef.TypeConverter;
                    if (converter != null)
                    {
                        SifSimpleType sst = converter.GetSifSimpleType(null);
                        element.SetField(elementDef, sst);
                    }
                    return;
                }
                else if (reader.Name.StartsWith("xmlns"))
                {
                    return;
                }
                else
                {
                    field = dtd.LookupElementDef(elementDef, reader.Prefix + ":" + reader.LocalName);
                }
            }

            if (field != null)
            {
                string        strVal = reader.Value.Trim();
                SifSimpleType val    = ParseValue(field, strVal, version, formatter, zone);
                element.SetField(field, val);
            }
            else if (element.ElementDef.EarliestVersion >= SifVersion.SIF20 && version < SifVersion.SIF20)
            {
                Adk.Log.Warn("Field " + element.ElementDef.ClassName + "." + (reader.Prefix == null ? reader.LocalName : reader.Prefix + ":" + reader.LocalName) + " does not exist in the sif 2.0 specification onwards.  It may or may not be valid in sif 1.5r1.  It will be ignored.");
            }
            else
            {
                // TODO: Log and gracefully ignore, depending on whether the ADK is set to strict or loose parsing
                throw new SifException
                          (SifErrorCategoryCode.Xml, SifErrorCodes.XML_GENERIC_VALIDATION_3,
                          "Unknown element or attribute",
                          reader.LocalName + " is not a recognized attribute of the " +
                          elementDef.Name + " element (SIF " +
                          element.EffectiveSIFVersion.ToString() + ")", zone);
            }
        }
Ejemplo n.º 3
0
        private void SetFieldValueFromElement(IElementDef def,
                                              SifElement element,
                                              XmlReader reader,
                                              SifVersion version,
                                              SifFormatter formatter,
                                              IZone zone)
        {
            // Check for xsi:nill
            if (reader.IsEmptyElement)
            {
                // no data to set
                return;
            }

            // Look for the xsi:nill attribute that signals a null value
            while (reader.MoveToNextAttribute())
            {
                if (reader.LocalName == SifWriter.NIL && reader.NamespaceURI == XmlSchema.InstanceNamespace)
                {
                    SifSimpleType val = def.TypeConverter.GetSifSimpleType(null);
                    element.SetField(def, val);
                    while (reader.NodeType != XmlNodeType.EndElement)
                    {
                        reader.Read();
                    }
                    return;
                }
                else
                {
                    // TODO: This is an unknown attribute. Log and continue
                }
            }

            while (reader.NodeType == XmlNodeType.Element)
            {
                reader.Read();
            }

            if (reader.NodeType == XmlNodeType.Text)
            {
                SifSimpleType val = ParseValue(def, reader.Value.Trim(), version, formatter, zone);
                element.SetField(def, val);
            }
            // TODO: Not sure if this will always advance as expected.
            while (reader.NodeType != XmlNodeType.EndElement)
            {
                reader.Read();
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Adds a SimpleField parsed from a specific version of SIF to the parent.
 /// </summary>
 /// <param name="contentParent">The element to add content to</param>
 /// <param name="fieldDef">The metadata definition of the field to set</param>
 /// <param name="data">The value to set to the field</param>
 /// <param name="version">The version of SIF that the SIFElement is being constructed
 /// from</param>
 /// <returns></returns>
 public virtual SimpleField SetField(SifElement contentParent,
                                     IElementDef fieldDef,
                                     SifSimpleType data,
                                     SifVersion version)
 {
     return(contentParent.SetField(fieldDef, data));
 }
Ejemplo n.º 5
0
        public override void SetValue(object rawValue)
        {
            SifElementPointer immediateParent = (SifElementPointer)Parent;
            SifElement        parentElement   = immediateParent.Element as SifElement;
            SifSimpleType     sst             = GetSIFSimpleTypeValue(fElementDef, rawValue);

            parentElement.SetField(fElementDef, sst);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Creates a child element, if supported by this node
        /// </summary>
        /// <param name="parentPointer"></param>
        /// <param name="formatter"></param>
        /// <param name="version"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public INodePointer CreateChild(INodePointer parentPointer, SifFormatter formatter, SifVersion version,
                                        SifXPathContext context)
        {
            // 1) Create an instance of the SimpleField with a null value (It's assigned later)

            //
            // STEP 2
            // Find the actual field to set the value to
            //
            SifElement parent        = (SifElement)((SifElementPointer)parentPointer).Element;
            SifElement targetElement = parent;

            if (!fElementDef.Field)
            {
                //	This indicates a child SifElement that needs to be created
                targetElement = SifElement.Create(parent, fElementDef);

                formatter.AddChild(parent, targetElement, version);
            }

            IElementDef fieldDef = null;

            if (fValueXpath.Equals("."))
            {
                fieldDef = fElementDef;
            }
            else
            {
                String fieldName = fValueXpath;
                if (fValueXpath.StartsWith("@"))
                {
                    fieldName = fValueXpath.Substring(1);
                }
                fieldDef = Adk.Dtd.LookupElementDef(fElementDef, fieldName);
            }


            if (fieldDef == null)
            {
                throw new ArgumentException("Support for value path {" + fValueXpath +
                                            "} is not supported by XPathSurrogate.");
            }

            SifSimpleType ssf = fieldDef.TypeConverter.GetSifSimpleType(null);
            SimpleField   sf  = ssf.CreateField(targetElement, fieldDef);

            targetElement.SetField(sf);


            // 2) built out a fake set of node pointers representing the SIF 1.5r1 path and
            //    return the root pointer from that stack
            return(BuildLegacyPointers(parentPointer, sf));
        }
Ejemplo n.º 7
0
        public override INodePointer CreateAttribute(SifXPathContext context, String name)
        {
            IElementDef subEleDef  = GetChildDef(name);
            SifElement  sifElement = (SifElement)fElement;

            if (subEleDef.Field)
            {
                SifSimpleType ssf = subEleDef.TypeConverter.GetSifSimpleType(null);
                SimpleField   sf  = ssf.CreateField(sifElement, subEleDef);
                sifElement.SetField(sf);
                return(SimpleFieldPointer.Create(this, sf));
            }

            throw new ArgumentException(
                      "Factory could not create a child node for path: " + Name
                      + "/" + name);
        }
Ejemplo n.º 8
0
        public bool ReadRaw(XmlReader reader,
                            SifVersion version,
                            SifElement parent,
                            SifFormatter formatter)
        {
            String elementName = fElementDef.Name;

            if (!reader.LocalName.Equals(elementName))
            {
                return(false);
            }

            String value = ConsumeElementTextValue(reader, version);

            if (value != null && value.Length > 0)
            {
                DateTime?time    = formatter.ToTime(value);
                SifTime  sifTime = new SifTime(time);
                parent.SetField(sifTime.CreateField(parent, fElementDef));
            }
            return(true);
        }
Ejemplo n.º 9
0
 /// <summary>  Assigns a list of attribute values to a destination SifElement.</summary>
 /// <param name="dst">The SifElement to copy the attributes to
 /// </param>
 /// <param name="attributes">An array where element N is an attribute name and
 /// element N+1 is the attribute value
 /// </param>
 private void _copyAttributes(SifElement dst, string[] attributes)
 {
     for (int i = 0; i < attributes.Length; i++)
     {
         IElementDef attrDef = Adk.Dtd.LookupElementDef(dst.ElementDef + "_" + attributes[i]);
         dst.SetField(attrDef, attributes[++i]);
     }
 }
Ejemplo n.º 10
0
        public bool ReadRaw(
            XmlReader reader,
            SifVersion version,
            SifElement parent,
            SifFormatter formatter)
        {
            String value = null;

            //
            // STEP 1
            // Determine if this surrogate can handle the parsing of this node.
            // Retrieve the node value as a string
            //

            String[]    xPathParts = fLegacyXpath.Split('/');
            XmlNodeType eventType  = reader.NodeType;
            String      localName  = reader.LocalName;

            if (eventType == XmlNodeType.Element &&
                localName.Equals(xPathParts[0]))
            {
                try
                {
                    int currentSegment = 0;
                    int lastSegment    = xPathParts.Length - 1;
                    if (xPathParts[lastSegment].StartsWith("@"))
                    {
                        lastSegment--;
                    }
                    while (currentSegment < lastSegment)
                    {
                        reader.Read();
                        currentSegment++;

                        if (!reader.LocalName.Equals(xPathParts[currentSegment]))
                        {
                            ThrowParseException
                                ("Element {" + reader.LocalName +
                                "} is not supported by XPathSurrogate path " + fLegacyXpath,
                                version);
                        }
                    }

                    // New we are at the last segment in the XPath, and the XMLStreamReader
                    // should be positioned on the proper node. The last segment is either
                    // an attribute or an element, which need to be read differently
                    String finalSegment = xPathParts[xPathParts.Length - 1];
                    if (finalSegment.StartsWith("@"))
                    {
                        value = reader.GetAttribute(finalSegment.Substring(1));
                    }
                    else
                    {
                        value = ReadElementTextValue(reader);
                    }

                    // Note: Unlike the Java ADK, Surrogates in the the .NET ADK do not have to worry about
                    // completely consuming the XMLElement and advancing to the next tag. The .NET
                    // Surrogates are handed a reader that only allows reading the current node and
                    // the parent reader is automatically advanced when the surrogate is done.
                }
                catch (Exception xse)
                {
                    ThrowParseException(xse, reader.LocalName, version);
                }
            }
            else
            {
                // No match was found
                return(false);
            }

            //
            // STEP 2
            // Find the actual field to set the value to
            //
            IElementDef fieldDef;
            SifElement  targetElement = parent;

            if (fValueXpath.Equals(".") && fElementDef.Field)
            {
                fieldDef = fElementDef;
            }
            else
            {
                //	This indicates a child SifElement that needs to be created
                try
                {
                    targetElement = SifElement.Create(parent, fElementDef);
                }
                catch (AdkSchemaException adkse)
                {
                    ThrowParseException(adkse, reader.LocalName, version);
                }

                formatter.AddChild(parent, targetElement, version);

                if (fValueXpath.Equals("."))
                {
                    fieldDef = fElementDef;
                }
                else
                {
                    String fieldName = fValueXpath;
                    if (fValueXpath.StartsWith("@"))
                    {
                        fieldName = fValueXpath.Substring(1);
                    }
                    fieldDef = Adk.Dtd.LookupElementDef(fElementDef, fieldName);
                }
            }

            if (fieldDef == null)
            {
                throw new InvalidOperationException
                          ("Support for value path {" + fValueXpath +
                          "} is not supported by XPathSurrogate.");
            }


            //
            // STEP 3
            // Set the value to the field
            //
            TypeConverter converter = fieldDef.TypeConverter;

            if (converter == null)
            {
                // TODO: Determine if we should be automatically creating a converter
                // for elementDefs that don't have one, or whether we should just throw the
                // spurious data away.
                converter = SifTypeConverters.STRING;
            }
            SifSimpleType data = converter.Parse(formatter, value);

            targetElement.SetField(fieldDef, data);

            return(true);
        }
Ejemplo n.º 11
0
        private void SetFieldValueFromAttribute(
            SifElement element,
            XmlReader reader,
            IDtd dtd,
            SifVersion version,
            SifFormatter formatter,
            IZone zone )
        {
            IElementDef elementDef = element.ElementDef;
            IElementDef field = dtd.LookupElementDef( element.ElementDef, reader.LocalName );
            if ( field == null && reader.Prefix != null )
            {
                if(reader.LocalName == SifWriter.NIL && reader.NamespaceURI == XmlSchema.InstanceNamespace )
                {
                    TypeConverter converter = elementDef.TypeConverter;
                    if ( converter != null )
                    {
                        SifSimpleType sst = converter.GetSifSimpleType( null );
                        element.SetField(elementDef, sst);
                    }
                    return;
                }
                else if( reader.Name.StartsWith( "xmlns" ) )
                {
                    return;
                }
                else
                {
                    field = dtd.LookupElementDef(elementDef, reader.Prefix + ":" + reader.LocalName);
                }
            }

            if( field != null )
            {
                string strVal = reader.Value.Trim();
                SifSimpleType val = ParseValue( field, strVal, version, formatter, zone );
                element.SetField( field, val );
            }
            else if (element.ElementDef.EarliestVersion >= SifVersion.SIF20 && version < SifVersion.SIF20)
            {
                Adk.Log.Warn("Field " + element.ElementDef.ClassName + "." + (reader.Prefix == null ? reader.LocalName : reader.Prefix + ":" + reader.LocalName ) + " does not exist in the sif 2.0 specification onwards.  It may or may not be valid in sif 1.5r1.  It will be ignored."  );
            }
            else
            {
                // TODO: Log and gracefully ignore, depending on whether the ADK is set to strict or loose parsing
                throw new SifException
                    (SifErrorCategoryCode.Xml, SifErrorCodes.XML_GENERIC_VALIDATION_3,
                      "Unknown element or attribute",
                      reader.LocalName + " is not a recognized attribute of the " +
                      elementDef.Name + " element (SIF " +
                      element.EffectiveSIFVersion.ToString() + ")", zone);
            }
        }
Ejemplo n.º 12
0
        private void SetFieldValueFromElement( IElementDef def,
                                               SifElement element,
                                               XmlReader reader,
                                               SifVersion version,
                                               SifFormatter formatter,
                                               IZone zone )
        {
            // Check for xsi:nill
            if ( reader.IsEmptyElement )
            {
                // no data to set
                return;
            }

            // Look for the xsi:nill attribute that signals a null value
            while ( reader.MoveToNextAttribute() )
            {
                if (reader.LocalName == SifWriter.NIL && reader.NamespaceURI == XmlSchema.InstanceNamespace)
                {
                    SifSimpleType val = def.TypeConverter.GetSifSimpleType( null );
                    element.SetField( def, val );
                    while (reader.NodeType != XmlNodeType.EndElement)
                    {
                        reader.Read();
                    }
                    return;
                }
                else
                {
                    // TODO: This is an unknown attribute. Log and continue
                }
            }

            while ( reader.NodeType == XmlNodeType.Element )
            {
                reader.Read();
            }

            if ( reader.NodeType == XmlNodeType.Text )
            {
                SifSimpleType val = ParseValue( def, reader.Value.Trim(), version, formatter, zone );
                element.SetField( def, val );
            }
            // TODO: Not sure if this will always advance as expected.
            while ( reader.NodeType != XmlNodeType.EndElement )
            {
                reader.Read();
            }
        }
Ejemplo n.º 13
0
 /// <summary>
 /// Adds a SimpleField parsed from a specific version of SIF to the parent.
 /// </summary>
 /// <param name="contentParent">The element to add content to</param>
 /// <param name="fieldDef">The metadata definition of the field to set</param>
 /// <param name="data">The value to set to the field</param>
 /// <param name="version">The version of SIF that the SIFElement is being constructed
 /// from</param>
 /// <returns></returns>
 public virtual SimpleField SetField(SifElement contentParent,
     IElementDef fieldDef,
     SifSimpleType data,
     SifVersion version)
 {
     return contentParent.SetField(fieldDef, data);
 }
Ejemplo n.º 14
0
        public bool ReadRaw( XmlReader reader,
                             SifVersion version,
                             SifElement parent,
                             SifFormatter formatter )
        {
            String elementName = fElementDef.Name;
            if ( !reader.LocalName.Equals( elementName ) )
            {
                return false;
            }

            String value = ConsumeElementTextValue( reader, version );

            if ( value != null && value.Length > 0 )
            {
                DateTime? time = formatter.ToTime( value );
                SifTime sifTime = new SifTime( time );
                parent.SetField( sifTime.CreateField( parent, fElementDef ) );
            }
            return true;
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Creates a child element and sets the text value
        /// </summary>
        /// <param name="relativeTo">The parent SIFElement to add the new element to</param>
        /// <param name="tag">The tag name of the element</param>
        /// <param name="valueBuilder">The ValueBuilder instance to use to evaluate macros</param>
        /// <param name="version">The version of SIF for which this mapping operation is being evaluated</param>
        /// <param name="textFormatter">The SIFFormatter instance used to parse strings into strongly-typed data values.
        /// For many uses of this API, this formatter is equivalent to Adk.TextFormatter</param>
        /// <param name="pathFormatter">The SIFFormatter instance used for setting child SIFElements on their parents.
        /// This formatter may be different than the text formatter. The text formatter is, for
        /// compatibility's sake defaulted to SIF 1.x. However, the path formatter must be 
        /// correct for the mappings path being evaluated.</param>
        /// <returns></returns>
        private SifElement _createChild(
            SifElement relativeTo,
            String tag,
            IValueBuilder valueBuilder,
            SifVersion version,
            SifFormatter textFormatter,
            SifFormatter pathFormatter)
        {
            string _tag = tag;
            string assignValue = null;
            int eq = tag.IndexOf('=');
            if (eq != -1)
            {
                _tag = tag.Substring(0, (eq) - (0));
                string str = tag.Substring(eq + 1);
                assignValue = valueBuilder == null ? str : valueBuilder.Evaluate(str);
            }

            //  Lookup the IElementDef
            IElementDef def = Adk.Dtd.LookupElementDef(relativeTo.ElementDef, _tag);
            if (def == null)
            {
                def = Adk.Dtd.LookupElementDef(_tag);
            }
            if (def == null)
            {
                throw new AdkSchemaException(_tag + " is not a recognized element or attribute of " + relativeTo.Tag);
            }

            try
            {
                TypeConverter defConverter = def.TypeConverter;
                if (defConverter == null)
                {
                    defConverter = SifTypeConverters.STRING;
                }
                if (def.Field)
                {
                    SimpleField field = defConverter.ParseField(relativeTo, def, textFormatter, assignValue);
                    relativeTo.SetField(field);
                }
                else
                {
                    //  Create element instance

                    SifElement ele = (SifElement)ClassFactory.CreateInstance(def.FQClassName);
                    ele.ElementDef = def;
                    pathFormatter.AddChild(relativeTo, ele, version);
                    if (assignValue != null)
                    {
                        // TODO: THis needs to be done using the type converter
                        ele.TextValue = assignValue;
                    }

                    return ele;
                }
            }
            catch (TypeLoadException tle)
            {
                throw new SystemException(
                    "The " + def.Package +
                    " Sdo module is not loaded (ensure the Adk is initialized to load this module)", tle);
            }
            catch (Exception thr)
            {
                throw new SystemException(
                    "Failed to create an instance of the " + def.ClassName + " class from the " + def.Package +
                    " Sdo module: ", thr);
            }

            return null;
        }
Ejemplo n.º 16
0
        /// <summary>  Set a field value on a SifElement</summary>
        /// <param name="parent">The SifElement on which to set the field
        /// </param>
        /// <param name="attr">The name of the attribute or simple child element
        /// </param>
        /// <param name="val">The text value to assign to the attribute or simple child element
        /// </param>
        private SimpleField _createField(SifElement parent, string attr, string val)
        {
            //  Lookup the IElementDef relative to the parent
            IElementDef def = Adk.Dtd.LookupElementDef(parent.ElementDef, attr);
            if (def == null)
            {
                throw new AdkSchemaException(attr + " is not a recognized attribute of " + parent.Tag);
            }
            if (!def.Field)
            {
                throw new AdkSchemaException("Query references a complex element ('" + attr +
                                              "') where an attribute or simple field was expected");
            }

            //  Set the field value on the parent
            return parent.SetField(def, val);
        }
Ejemplo n.º 17
0
        public bool ReadRaw( XmlReader reader,
                             SifVersion version,
                             SifElement parent,
                             SifFormatter formatter )
        {
            string name = reader.LocalName;

            if ( name.Equals( fDateElement ) )
            {
                String dateValue = ConsumeElementTextValue( reader, version );
                DateTime? date = formatter.ToDate( dateValue );
                if ( date.HasValue )
                {
                    DateTime tsValue = date.Value;
                    SifDateTime dateTime = new SifDateTime( tsValue );
                    parent.SetField( dateTime.CreateField( parent, fElementDef ) );
                }
            }
            else if ( name.Equals( fTimeElement ) )
            {
                String timeValue = ConsumeElementTextValue( reader, version );
                DateTime? time = formatter.ToTime( timeValue );
                if ( time.HasValue )
                {
                    DateTime val = time.Value;
                    // See if the Timestamp field already exists on the parent
                    SimpleField timeStampField = parent.GetField( fElementDef );
                    if ( timeStampField == null )
                    {
                        // Doesn't exist, create it
                        SifDateTime dateTime = new SifDateTime( val );
                        parent.SetField( dateTime.CreateField( parent, fElementDef ) );
                    }
                    else
                    {
                        // Exists, update the time portion of the date
                        SifDateTime sdt = (SifDateTime) timeStampField.SifValue;
                        if ( sdt != null && sdt.Value.HasValue )
                        {
                            DateTime tsValue = sdt.Value.Value;
                            tsValue = tsValue.Add( val.TimeOfDay );
                            sdt = new SifDateTime( tsValue );
                            // Overwrite the current value
                            parent.SetField( sdt.CreateField( parent, fElementDef ) );
                        }
                    }
                }
            }
            else
            {
                return false;
            }

            return true;
        }