Beispiel #1
0
 /// <summary>Sets an element or attribute value identified by an XPath-like query string.</summary>
 /// <remarks>
 ///   NOTE: This method makes calls to SIFXPathContext. If multiple calls to
 ///  <c>setElementOrAttribute</c> are being done, it is much more efficient to create
 ///  a new <c>SifXPathContext</c> by calling <c>SifXPathContext.NewInstance(sdo)</c> and then
 /// calling <c>.SetElementorAttributeon</c> on that SifXPathContext instance
 /// </remarks>
 /// <param name="xpath">An XPath-like query string that identifies the element or
 /// attribute to set. The string must reference elements and attributes
 /// by their <i>version-independent</i> names.
 /// </param>
 /// <param name="valu">The value to assign to the element or attribute if the
 /// query string does not set a value; may be null
 /// </param>
 /// <param name="valueBuilder">a ValueBuilder implementation that evaluates
 /// expressions in XPath-like query strings using name/value pairs in
 /// the <i>variables</i> map
 /// </param>
 public virtual void SetElementOrAttribute(string xpath,
                                           string valu,
                                           IValueBuilder valueBuilder)
 {
     valu = valueBuilder == null ? valu : valueBuilder.Evaluate(valu);
     SetElementOrAttribute(xpath, valu);
 }
 /// <summary>Sets an element or attribute value identified by an XPath-like query string.</summary>
 /// <remarks>
 ///   NOTE: This method makes calls to SIFXPathContext. If multiple calls to
 ///  <c>setElementOrAttribute</c> are being done, it is much more efficient to create
 ///  a new <c>SifXPathContext</c> by calling <c>SifXPathContext.NewInstance(sdo)</c> and then
 /// calling <c>.SetElementorAttributeon</c> on that SifXPathContext instance
 /// </remarks>
 /// <param name="xpath">An XPath-like query string that identifies the element or
 /// attribute to set. The string must reference elements and attributes
 /// by their <i>version-independent</i> names.
 /// </param>
 /// <param name="valu">The value to assign to the element or attribute if the
 /// query string does not set a value; may be null
 /// </param>
 /// <param name="valueBuilder">a ValueBuilder implementation that evaluates
 /// expressions in XPath-like query strings using name/value pairs in
 /// the <i>variables</i> map
 /// </param>
 public virtual void SetElementOrAttribute( string xpath,
                                            string valu,
                                            IValueBuilder valueBuilder )
 {
     valu = valueBuilder == null ? valu : valueBuilder.Evaluate( valu );
     SetElementOrAttribute( xpath, valu );
 }
        /// <summary>  Recursively builds elements and attributes relative to a SifElement.</summary>
        /// <param name="relativeTo">The SifElement this iteration is relative to. For the
        /// first call to this method, the <i>relativeTo</i> parameter is usually
        /// a SifDataObject such as StudentPersonal to which the XPath query
        /// string is relative. With each subsequent call it is the SifElement
        /// or SimpleField that was previously processed.</param>
        /// <param name="path">A running path of the segments processed thus far; an empty
        /// StringBuffer should be passed to this parameter the first time the
        /// method is called </param>
        /// <param name="curSegment">The current segment of the path that is being processed.
        /// For the first call to this method, the <i>curSegment</i> should be
        /// the portion of the XPath query string up to the first forward slash,
        /// exclusive.</param>
        /// <param name="nextSegment">The remaining portion of the path to be processed.
        /// For the first call to this method, the <i>nextSegment</i> should be
        /// the portion of the XPath query string following the first forward
        /// slash.</param>
        /// <param name="prevAttributes">An optional array of attribute values that were
        /// used to construct an Element in the processing of the last segment.
        /// The array is comprised of attribute name and value pairs such that
        /// element N is an attribute name and N+1 is its value. For the first
        /// call to this method, the array should be null. For subsequent calls,
        /// it should be null unless an Element was constructed from attribute
        /// values.</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>
        /// <param name="valueBuilder"></param>
        /// <returns> The Element satisfying the query, or <c>null</c> if no
        /// match was found (unless the <i>create</i> parameter is true). If the
        /// query resolves to an attribute, a SimpleField object is returned. If
        /// it resolves to an element, a SifElement object is returned. In both
        /// cases the caller can obtain the text value of the attribute or
        /// element by calling its <c>TextValue</c> Property.
        /// </returns>
        private Element _xpathBuild(
            SifElement relativeTo,
            StringBuilder path,
            string curSegment,
            string nextSegment,
            string[] prevAttributes,
            IValueBuilder valueBuilder,
            SifVersion version,
            SifFormatter textFormatter,
            SifFormatter pathFormatter)
        {
            string[] _prevAttributes = null;
            SifElement nextEle = null;

            int asgnEq = curSegment.LastIndexOf('=');
            int attr = curSegment.LastIndexOf('@', asgnEq == -1 ? curSegment.Length - 1 : asgnEq - 1);
            int bracket = curSegment.IndexOf('[');
            if (bracket != -1)
            {
                if (attr == -1)
                {
                    throw new AdkSchemaException("Invalid query: \"" + curSegment +
                                                  "\" must be in the form [@Attr='value1','value2',...]");
                }

                string subEleTag = curSegment.Substring(0, (bracket) - (0));

                int lastBracket = curSegment.LastIndexOf(']');
                string[] attrList = curSegment.Substring(bracket + 1, (lastBracket) - (bracket + 1)).Split(',');
                _prevAttributes = new string[attrList.Length * 2];
                int _prevI = 0;

                for (int a = 0; a < attrList.Length; a++)
                {
                    string _curSegment = attrList[a];

                    //  Determine the value of the attribute
                    int eq = _curSegment.IndexOf("=");
                    string val = _curSegment.Substring(eq + 1);
                    string v = null;
                    if (val[0] == '\'')
                    {
                        int end = val.IndexOf('\'', 1);
                        if (end != -1)
                        {
                            v = valueBuilder == null
                                    ? val.Substring(1, (end) - (1))
                                    : valueBuilder.Evaluate(val.Substring(1, (end) - (1)));
                        }
                    }

                    if (v == null)
                    {
                        throw new AdkSchemaException("Attribute value (" + val +
                                                      ") must be in the form @Attribute='value'");
                    }

                    string attrName = _curSegment.Substring(1, (eq) - (1));

                    _prevAttributes[_prevI++] = attrName;
                    _prevAttributes[_prevI++] = v;

                    if (nextEle == null)
                    {
                        //
                        //  Look at all of the peer elements to determine if any have the
                        //  attribute value set. If so, return it; otherwise create a new
                        //  instance of the element with the attribute set. For example, if
                        //  curSegment is "Address[@Type='M']", we must look at all of the
                        //  Address children of relativeTo in order to determine if any
                        //  currently exist with a Type field set to a value of 'M'. If one
                        //  does, then it already exists and there is nothing to do; if
                        //  not found, however, a new Address child must be added with a
                        //  Type field of 'M'.
                        //

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

                        bool repeatable = subEleDef.IsRepeatable(relativeTo.SifVersion);

                        SifElementList peers = relativeTo.GetChildList(subEleDef);

                        if (curSegment.IndexOf("+]") == -1)
                        {
                            //
                            //  Determine if relatSifElementListiveTo has any children that already
                            //  define this attribute/value; if not, create a new instance.
                            //  If subEleDef is not repeatable, however, we cannot add
                            //  another instance of it regardless.
                            //
                            for (int i = 0; i < peers.Count && nextEle == null; i++)
                            {
                                SimpleField ftest = peers[i].GetField(attrName);
                                if (ftest != null && ftest.TextValue.Equals(v))
                                {
                                    nextEle = peers[i];
                                }
                            }
                        }

                        if (nextEle == null)
                        {
                            if (!(peers.Count > 0 && !repeatable))
                            {
                                nextEle =
                                    _createChild(relativeTo, subEleTag, valueBuilder, version, textFormatter,
                                                  pathFormatter);
                            }
                            else
                            {
                                //
                                //  subEleDef is not repeatable, so we need to back up
                                //  and add this attribute/value to a fresh instance of
                                //  relativeTo if possible. First use _xpath(path) to try
                                //  to select that instance in case it already exists (otherwise
                                //  we'd create a new instance each iteration, which is
                                //  not the desired result.)
                                //

                                string _tmp;
                                if (path.Length > 0)
                                {
                                    _tmp = path + "/" + curSegment;
                                }
                                else
                                {
                                    _tmp = curSegment;
                                }
                                if (XPATH_DEBUG)
                                {
                                    Console.Out.Write("Searching for path relative to " +
                                                       relativeTo.Root.ElementDef.Name + ": " + _tmp);
                                }

                                int _del = _tmp.IndexOf('/');
                                nextEle =
                                    (SifElement)
                                    _xpath((SifElement)relativeTo.Root,
                                            _tmp.Substring(0, (_del == -1 ? _tmp.Length : _del) - (0)),
                                            _del == -1 ? null : _tmp.Substring(_del + 1));

                                if (XPATH_DEBUG)
                                {
                                    if (nextEle == null)
                                    {
                                        Console.Out.WriteLine("; not found, a new instance will be created");
                                    }
                                    else
                                    {
                                        Console.Out.WriteLine("; found");
                                    }
                                }

                                if (nextEle == null)
                                {
                                    if (relativeTo.ElementDef.IsRepeatable(relativeTo.SifVersion))
                                    {
                                        //  Clone relativeTo
                                        SifElement grandParent = (SifElement)relativeTo.Parent;
                                        nextEle = SifElement.Create(grandParent, relativeTo.ElementDef);
                                        pathFormatter.AddChild(grandParent, nextEle, version);

                                        //  Clone subEleDef; this now becomes nextEle
                                        SifElement newEle = SifElement.Create(nextEle, subEleDef);
                                        pathFormatter.AddChild(nextEle, newEle, version);
                                        _copyAttributes(nextEle, prevAttributes);
                                        nextEle = newEle;
                                    }
                                    else
                                    {
                                        throw new AdkSchemaException(
                                            "It is not possible to create the element or attribute identified by this path: " +
                                            _tmp + (nextSegment == null ? "" : "/" + nextSegment) +
                                            ". The element or attribute is either undefined in this version of SIF, " +
                                            "or an attempt is being made to create another instance of an element that is not Repeatable.");
                                    }
                                }
                            }
                        }
                    }

                    if (nextEle != null)
                    {
                        _createField(nextEle, attrName, v);
                    }

                    if (a == attrList.Length && nextEle == null)
                    {
                        return null;
                    }
                }
            }
            else
            {
                //  Search for the named attribute/element
                if (attr != -1)
                {
                    SimpleField ff = relativeTo.GetField(curSegment.Substring(1));
                    if (ff == null)
                    {
                        ff = _createField(relativeTo, curSegment.Substring(1), null);
                    }

                    return ff;
                }
                else
                {
                    string _tag = curSegment;
                    int eq = curSegment.IndexOf('=');
                    if (eq != -1)
                    {
                        _tag = curSegment.Substring(0, (eq) - (0));
                    }

                    nextEle = relativeTo.GetChild(_tag);
                    if (nextEle == null)
                    {
                        //  The curSegment element does not exist as a child of the relativeTo
                        //  object, so create it.
                        nextEle =
                            _createChild(relativeTo, curSegment, valueBuilder, version, textFormatter, pathFormatter);
                        if (nextEle == null)
                        {
                            if (nextSegment == null)
                            {
                                return relativeTo.GetField(_tag);
                            }
                            return null;
                        }
                    }
                }
            }

            //  Continue the search if nextSegment has a value
            if (nextEle != null && (nextSegment != null && nextSegment.Length > 0))
            {
                int i = nextSegment.IndexOf('/');

                if (path.Length > 0)
                {
                    path.Append("/");
                }
                path.Append(curSegment);

                return _xpathBuild(
                    nextEle,
                    path, i == -1 ? nextSegment : nextSegment.Substring(0, (i)),
                    i == -1 ? null : nextSegment.Substring(i + 1),
                    _prevAttributes,
                    valueBuilder,
                    version,
                    textFormatter,
                    pathFormatter);
            }

            if (nextSegment == null && nextEle != null && (nextEle.TextValue == null || nextEle.TextValue.Length == 0))
            {
                int eq2 = curSegment.LastIndexOf('=');
                if (eq2 != -1)
                {
                    if (bracket == -1 || (curSegment.LastIndexOf(']') < eq2))
                    {
                        //
                        //  An equals sign in the final segment indicates there is
                        //  a value constant or expression following the XPath
                        //  (e.g. "OtherId[@Type='06']=@pad($(PERMNUM),0,5)" ). Use
                        //  the user-supplied ValueBuilder to evaluate it.
                        //
                        string str = curSegment.Substring(eq2 + 1);
                        nextEle.TextValue = valueBuilder == null ? str : valueBuilder.Evaluate(str);
                    }
                }
            }

            return nextEle;
        }
        /// <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;
        }
Beispiel #5
0
        internal void MapOutbound(
            SifXPathContext context,
            IFieldAdaptor adaptor,
            SifElement dataObject,
            ICollection<FieldMapping> fields,
            IValueBuilder valueBuilder,
            SifVersion version)
        {
            #if PROFILED
            if( BuildOptions.PROFILED ){
            ProfilerUtils.profileStart( String.valueOf( com.OpenADK.sifprofiler.api.OIDs.ADK_OUTBOUND_TRANSFORMATIONS ), dataObject.getElementDef(), null );
            }
            #endif

            SifFormatter textFormatter = Adk.TextFormatter;
            FieldMapping lastRule = null;
            try
            {
                foreach (FieldMapping fm in fields)
                {
                    lastRule = fm;
                    String fieldName = fm.Alias;
                    if (fieldName == null)
                    {
                        fieldName = fm.FieldName;
                    }

                    if (fieldName == null || fieldName.Length == 0)
                    {
                        throw new AdkMappingException(
                            "Mapping rule for " + dataObject.ElementDef.Name + "[" + fm.FieldName +
                            "] must specify a field name", null);
                    }

                    if (adaptor.HasField(fieldName) || fm.HasDefaultValue)
                    {
                        //
                        //  For outbound mapping operations, only process
                        //	XPathRules. All other rule types, like OtherIdRule,
                        //  are only intended to be used for inbound mappings.
                        //
                        if (fm.fRule is XPathRule)
                        {
                            XPathRule rule = (XPathRule) fm.fRule;
                            //  Lookup or create the element/attribute referenced by the rule
                            String ruledef = rule.XPath;
                            if (ruledef == null || ruledef.Trim().Length == 0)
                            {
                                throw new AdkMappingException(
                                    "Mapping rule for " + dataObject.ElementDef.Name + "[\"" + fieldName +
                                    "\"] must specify a path to an element or attribute", null);
                            }

                            // TT 199 If the FieldMapping has an "ifnull" value of "suppress",
                            // don't render a result

                            // Determine if this element should be created before attempting to create it
                            // If the value resolves to null and the IFNULL_SUPPRESS flag is set, the element
                            // should not be created. That's why we have to look up the ElementDef first
                            TypeConverter typeConverter = null;
                            IElementDef def = rule.LookupTargetDef(dataObject.ElementDef);
                            if (def != null)
                            {
                                typeConverter = def.TypeConverter;
                            }
                            if (typeConverter == null)
                            {
                                typeConverter = SifTypeConverters.STRING;
                                // TODO: Perhaps the following exception should be thrown when
                                // in STRICT mode
                                //	throw new ADKMappingException( "Element {" + def.name() +
                                //				"} from rule \"" + ruledef + "\" does not have a data type definition.", null );
                            }

                            SifSimpleType mappedValue;
                            mappedValue = adaptor.GetSifValue(fieldName, typeConverter, fm);

                            // Perform a valueset translation, if applicable
                            if (mappedValue != null &&
                                mappedValue is SifString &&
                                fm.ValueSetID != null)
                            {
                                String textValue = mappedValue.ToString();
                                //	Perform automatic ValueSet translation
                                ValueSet vs = GetValueSet(fm.ValueSetID, true);
                                if (vs != null)
                                {
                                    // TT 199. Perform a more detailed valueset translation.
                                    // If there is a default value for this field, use it if there is
                                    // no match found in the value set
                                    textValue = vs.Translate(textValue, fm.DefaultValue);
                                }
                                mappedValue = new SifString(textValue);
                            }

                            bool usedDefault = false;
                            if (mappedValue == null || mappedValue.RawValue == null)
                            {
                                // If the FieldMapping has a Default value, use that, unless
                                // it is explicitly suppressed
                                if (fm.NullBehavior != MappingBehavior.IfNullSuppress && fm.HasDefaultValue)
                                {
                                    mappedValue = fm.GetDefaultValue(typeConverter, textFormatter);
                                    usedDefault = true;
                                }
                                else
                                {
                                    continue;
                                }
                            }

                            if (!usedDefault)
                            {
                                String valueExpression = rule.ValueExpression;
                                if (valueExpression != null)
                                {
                                    // This XPath rule has a value assignment expression at the end of it
                                    String value = valueBuilder.Evaluate(valueExpression);
                                    mappedValue = typeConverter.Parse(textFormatter, value);
                                }
                            }

                            // If we have a null value to assign at this point, move on to the next rule
                            if (mappedValue == null || mappedValue.RawValue == null)
                            {
                                continue;
                            }

                            // At this point, mappedValue should not be null. We are committeed
                            // to building out the path and setting the value.
                            INodePointer pointer = rule.CreatePath(context, version);
                            //  If the element/attribute does not have a value, assign one.
                            //	If it does have a value, it was already assigned by the XPath
                            //	rule in the lookupByXPath method above and should not be
                            //	changed.
                            //
                            if (pointer != null)
                            {
                                Element pointedElement = (Element) pointer.Value;
                                SifSimpleType elementValue = pointedElement.SifValue;
                                if (elementValue == null || elementValue.RawValue == null)
                                {
                                    if (mappedValue is SifString)
                                    {
                                        // Now that we have the actual element, we may need to create convert the
                                        // data if we were unable to resolve the TypeConverter above. This only happens
                                        // in cases involving surrogates where the rule.lookupTargetDef( dataObject.getElementDef() );
                                        // fails to find the target ElementDef
                                        TypeConverter converter = pointedElement.ElementDef.TypeConverter;
                                        if (converter != null && converter.DataType != mappedValue.DataType)
                                        {
                                            mappedValue = converter.Parse(textFormatter, mappedValue.ToString());
                                        }
                                    }

                                    // This check for null should really not be necessary,
                                    // however keepingit in for now
                                    if (mappedValue != null)
                                    {
                                        pointedElement.SifValue = mappedValue;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                if (lastRule != null)
                {
                    throw new AdkMappingException(
                        "Unable to evaluate field rule: " + lastRule.fRule + " : " + e.Message, null, e);
                }
                throw new AdkMappingException(e.ToString(), null, e);
            }
            #if PROFILED
            finally
            {

            if( BuildOptions.PROFILED )
                ProfilerUtils.profileStop();
            }
            #endif
        }