示例#1
0
        /// <summary>
        /// Sets the value of this field
        /// </summary>
        /// <param name="id"></param>
        /// <param name="value"></param>
        /// <returns></returns>

        public override SimpleField SetField(IElementDef id, SifSimpleType value)
        {
            // verify the parameter values
            if (id == null || !(CommonDTD.PARTIALDATETYPE.Name.Equals(id.Name)))
            {
                throw new ArgumentException("ElementDef must be CommonDTD.PARTIALDATETYPE");
            }
            if (value != null && value.DataType != SifDataType.String)
            {
                throw new ArgumentException("Value type must be SIFDataType.STRING");
            }

            // Allow any datatype to be set, but convert it to a string
            // This is important, because the value of this could be set to
            // an int from mappings, because it was an int in SIF 1.5
            if (value != null && value.DataType != SifDataType.String)
            {
                value = new SifString(value.ToString());
            }
            // Parse the text value into its representative parts
            SimpleField returnValue = base.SetField(id, value);

            parseTextValue();
            return(returnValue);
        }
示例#2
0
        public void TestxsiNill_SDOObjectXML()
        {
            LearnerPersonal lp = new LearnerPersonal();

            // Add a null UPN
            SifString str = new SifString(null);

            lp.SetField(LearnerDTD.LEARNERPERSONAL_UPN, str);

            // Add a null AlertMsg
            AlertMsg msg = new AlertMsg(AlertMsgType.DISCIPLINE, null);

            lp.AlertMsgList = new AlertMsgList(msg);
            msg.SetField(CommonDTD.ALERTMSG, new SifString(null));



            //  Write the object to a file
            Console.WriteLine("Writing to file...");
            using (Stream fos = File.Open("SifWriterTest.Temp.xml", FileMode.Create, FileAccess.Write))
            {
                SifWriter writer = new SifWriter(fos);
                lp.SetChanged(true);
                writer.Write(lp);
                writer.Flush();
                fos.Close();
            }

            //  Parse the object from the file
            Console.WriteLine("Parsing from file...");
            SifParser p = SifParser.NewInstance();

            using (Stream fis = File.OpenRead("SifWriterTest.Temp.xml"))
            {
                lp = (LearnerPersonal)p.Parse(fis, null);
            }


            SimpleField upn = lp.GetField(LearnerDTD.LEARNERPERSONAL_UPN);

            Assert.IsNotNull(upn);

            SifString rawValue = (SifString)upn.SifValue;

            Assert.IsNotNull(rawValue);
            Assert.IsNull(rawValue.Value);
            Assert.IsNull(upn.Value);

            AlertMsgList alertMsgs = lp.AlertMsgList;

            Assert.IsNotNull(alertMsgs);
            Assert.IsTrue(alertMsgs.Count == 1);
            msg = (AlertMsg)alertMsgs.GetChildList()[0];

            Assert.IsNull(msg.Value);
            SifSimpleType msgValue = msg.SifValue;

            Assert.IsNotNull(msgValue);
            Assert.IsNull(msgValue.RawValue);
        }
示例#3
0
        /**
         * Sets the text value of this element from its representative date parts.
         * This is called any time one of the integral components of the date is set,
         * overwriting any previous text value
         */
        private void calculateTextValue()
        {
            // Do not call setField( ElementDef, SIFSimplyType ) because it would cause
            // the integral components to have to be re-parsed
            if (fDate[0] == null)
            {
                // No year component
                RemoveField(CommonDTD.PARTIALDATETYPE);
            }
            else
            {
                String textValue = null;
                if (fDate[1] == null)
                {
                    // Year only
                    textValue = String.Format("{0:0000}", fDate[0].Value);
                }
                else if (fDate[2] == null)
                {
                    // Year-month only
                    textValue = String.Format("{0:0000}-{1:00}", fDate[0].Value, fDate[1].Value);
                }
                else
                {
                    textValue = String.Format("{0:0000}-{1:00}-{2:00}", fDate[0].Value, fDate[1].Value, fDate[2].Value);
                }

                SifString   value = new SifString(textValue);
                SimpleField field = value.CreateField(this, CommonDTD.PARTIALDATETYPE);
                SetField(field);
            }
        }
        /**
         * Sets the text value of this element from its representative date parts.
         * This is called any time one of the integral components of the date is set,
         * overwriting any previous text value
         */
        private void calculateTextValue()
        {
            // Do not call setField( ElementDef, SIFSimplyType ) because it would cause
            // the integral components to have to be re-parsed
            if( fDate[0] == null  )
            {
            // No year component
            RemoveField( CommonDTD.PARTIALDATETYPE );
            }
            else
            {
            String textValue = null;
            if( fDate[1] == null ){
                // Year only
                textValue = String.Format( "{0:0000}", fDate[0].Value );
            } else if ( fDate[2] == null ){
                // Year-month only
                textValue = String.Format( "{0:0000}-{1:00}", fDate[ 0 ].Value, fDate[ 1 ].Value );
            } else {
                textValue = String.Format( "{0:0000}-{1:00}-{2:00}", fDate[ 0 ].Value, fDate[ 1 ].Value, fDate[ 2 ].Value );
            }

            SifString value = new SifString( textValue );
            SimpleField field = value.CreateField( this, CommonDTD.PARTIALDATETYPE );
            SetField( field );
            }
        }
        /// <summary>
        /// Sets the value of this field
        /// </summary>
        /// <param name="id"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public override SimpleField SetField( IElementDef id, SifSimpleType value)
        {
            // verify the parameter values
            if( id == null || !(CommonDTD.PARTIALDATETYPE.Name.Equals( id.Name ))){
            throw new ArgumentException("ElementDef must be CommonDTD.PARTIALDATETYPE" );
            }
            if( value != null && value.DataType != SifDataType.String ){
            throw new ArgumentException( "Value type must be SIFDataType.STRING" );
            }

            // Allow any datatype to be set, but convert it to a string
            // This is important, because the value of this could be set to
            // an int from mappings, because it was an int in SIF 1.5
            if( value != null && value.DataType != SifDataType.String ){
            value = new SifString( value.ToString() );
            }
            // Parse the text value into its representative parts
            SimpleField returnValue = base.SetField(id, value);
            parseTextValue();
            return returnValue;
        }
示例#6
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
        }
示例#7
0
        internal void MapInbound(
            SifXPathContext xpathContext,
            IFieldAdaptor results,
            SifElement inboundObject,
            ICollection<FieldMapping> fields,
            SifVersion version)
        {
            #if PROFILED
            if( BuildOptions.PROFILED ){
            ProfilerUtils.profileStart( String.valueOf( com.OpenADK.sifprofiler.api.OIDs.ADK_INBOUND_TRANSFORMATIONS ), inboundObject.getElementDef(), null );
            }
            #endif

            FieldMapping lastRule = null;
            try
            {
                foreach (FieldMapping rule in fields)
                {
                    lastRule = rule;
                    if (!results.HasField(rule.FieldName))
                    {
                        SifSimpleType val = rule.Evaluate(xpathContext, version, true);
                        if (val != null)
                        {
                            if (rule.ValueSetID != null &&
                                val is SifString)
                            {
                                String currentValue = val.ToString();
                                //	Perform automatic ValueSet translation
                                // TT 199. Perform a more detailed valueset translation.
                                // If there is a default value set, use it if there is
                                // no match found in the value set
                                ValueSet vs = GetValueSet(rule.ValueSetID, true);
                                if (vs != null)
                                {
                                    currentValue = vs.TranslateReverse(currentValue, rule.DefaultValue);
                                }
                                val = new SifString(currentValue);
                            }
                            results.SetSifValue(rule.FieldName, val, rule);
                        }
                    }
                }
            }
            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);
            }
        }