Example #1
0
        private SifElement GetContainer(SifElement contentParent, IElementDef childDef, SifVersion version)
        {
            IElementDef elementParentDef = childDef.Parent;

            if (elementParentDef != null && elementParentDef != contentParent.ElementDef)
            {
                // The element does not appear to belong to this parent. Attempt to look
                // for a container element that might be missing in between the two
                // If the parent of this element were collapsed in a previous version
                // of SIF, check for or re-add the parent element and add this new
                // child instead.
                //
                // For example, a child could be an Email element from the
                // common package that's being added to StudentPersonal. In this
                // case,
                // we need to actually find or create an instance of the new
                // EmailList
                // container element and add the child to it, instead of to "this"
                String      tag         = elementParentDef.Tag(Adk.SifVersion);
                IElementDef missingLink = Adk.Dtd.LookupElementDef(contentParent.ElementDef, tag);
                if (missingLink != null && missingLink.IsCollapsed(version))
                {
                    SifElement container = contentParent.GetChild(missingLink);
                    if (container == null)
                    {
                        container = SifElement.Create(contentParent, missingLink);
                    }
                    AddChild(contentParent, container, version);
                    return(container);
                }
            }

            return(contentParent);
        }
        public override INodePointer CreateChild(SifXPathContext context, string name, int i)
        {
            SifVersion   version    = Version;
            IElementDef  subEleDef  = GetChildDef(name);
            SifFormatter formatter  = Adk.Dtd.GetFormatter(version);
            SifElement   sifElement = (SifElement)fElement;

            // Check to see if this child has a render surrogate defined
            IRenderSurrogate rs = subEleDef.GetVersionInfo(version).GetSurrogate();

            if (rs != null)
            {
                return(rs.CreateChild(this, formatter, version, context));
            }

            if (subEleDef.Field)
            {
                SifSimpleType ssf = subEleDef.TypeConverter.GetSifSimpleType(null);
                SimpleField   sf  = formatter.SetField(sifElement, subEleDef, ssf, version);
                return(SimpleFieldPointer.Create(this, sf));
            }
            else
            {
                SifElement newEle = SifElement.Create(sifElement, subEleDef);
                formatter.AddChild(sifElement, newEle, version);
                return(new SifElementPointer(this, newEle, version));
            }
        }
Example #3
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));
        }
Example #4
0
        private SifElement ReadSifElementFromElementNode(
            IElementDef def,
            XmlReader reader,
            IDtd dtd,
            SifElement parent,
            SifFormatter formatter,
            SifVersion version,
            IZone zone)
        {
            SifElement element;

            try
            {
                element = SifElement.Create(parent, def);
            }
            catch (TypeLoadException tle)
            {
                throw new AdkParsingException
                          ("Could not create an instance of " + def.FQClassName + " to wrap a " +
                          reader.LocalName + " element because that class doesn't exist", zone, tle);
            }
            catch (Exception ex)
            {
                throw new AdkParsingException
                          ("Could not create an instance of " + def.FQClassName, zone, ex);
            }

            element.ElementDef = def;
            element.SifVersion = version;

            if (parent != null)
            {
                element = formatter.AddChild(parent, element, version);
            }

            // Set the attributes to fields of the SifElement
            while (reader.MoveToNextAttribute())
            {
                SetFieldValueFromAttribute(element, reader, dtd, version, formatter, zone);
            }

            return(element);
        }
Example #5
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);
        }