Пример #1
0
        /// <summary>Writes all used namespaces of the subtree in node to the output.</summary>
        /// <remarks>
        /// Writes all used namespaces of the subtree in node to the output.
        /// The subtree is recursively traversed.
        /// </remarks>
        /// <param name="node">the root node of the subtree</param>
        /// <param name="usedPrefixes">a set containing currently used prefixes</param>
        /// <param name="indent">the current indent level</param>
        /// <exception cref="System.IO.IOException">Forwards all writer exceptions.</exception>
        private void DeclareUsedNamespaces(XmpNode node, ICollection <object> usedPrefixes, int indent)
        {
            if (node.Options.IsSchemaNode)
            {
                // The schema node name is the URI, the value is the prefix.
                var prefix = node.Value.Substring(0, node.Value.Length - 1 - 0);
                DeclareNamespace(prefix, node.Name, usedPrefixes, indent);
            }
            else if (node.Options.IsStruct)
            {
                for (var it = node.IterateChildren(); it.HasNext();)
                {
                    var field = (XmpNode)it.Next();
                    DeclareNamespace(field.Name, null, usedPrefixes, indent);
                }
            }

            for (var it = node.IterateChildren(); it.HasNext();)
            {
                var child = (XmpNode)it.Next();
                DeclareUsedNamespaces(child, usedPrefixes, indent);
            }

            for (var it = node.IterateQualifier(); it.HasNext();)
            {
                var qualifier = (XmpNode)it.Next();
                DeclareNamespace(qualifier.Name, null, usedPrefixes, indent);
                DeclareUsedNamespaces(qualifier, usedPrefixes, indent);
            }
        }
Пример #2
0
 /// <summary>Make sure that the array is well-formed AltText.</summary>
 /// <remarks>
 /// Make sure that the array is well-formed AltText. Each item must be simple
 /// and have an "xml:lang" qualifier. If repairs are needed, keep simple
 /// non-empty items by adding the "xml:lang" with value "x-repair".
 /// </remarks>
 /// <param name="arrayNode">the property node of the array to repair.</param>
 /// <exception cref="XmpException">Forwards unexpected exceptions.</exception>
 private static void RepairAltText(XmpNode arrayNode)
 {
     if (arrayNode == null || !arrayNode.Options.IsArray)
     {
         // Already OK or not even an array.
         return;
     }
     // fix options
     arrayNode.Options.IsArrayOrdered   = true;
     arrayNode.Options.IsArrayAlternate = true;
     arrayNode.Options.IsArrayAltText   = true;
     for (var it = arrayNode.IterateChildren(); it.HasNext();)
     {
         var currChild = (XmpNode)it.Next();
         if (currChild.Options.IsCompositeProperty)
         {
             // Delete non-simple children.
             it.Remove();
         }
         else if (!currChild.Options.HasLanguage)
         {
             if (string.IsNullOrEmpty(currChild.Value))
             {
                 // Delete empty valued children that have no xml:lang.
                 it.Remove();
             }
             else
             {
                 // Add an xml:lang qualifier with the value "x-repair".
                 var repairLang = new XmpNode(XmpConstants.XmlLang, "x-repair", null);
                 currChild.AddQualifier(repairLang);
             }
         }
     }
 }
Пример #3
0
        /// <summary>The outermost call is special.</summary>
        /// <remarks>
        /// The outermost call is special. The names almost certainly differ. The
        /// qualifiers (and hence options) will differ for an alias to the x-default
        /// item of a langAlt array.
        /// </remarks>
        /// <param name="aliasNode">the alias node</param>
        /// <param name="baseNode">the base node of the alias</param>
        /// <param name="outerCall">marks the outer call of the recursion</param>
        /// <exception cref="XmpException">Forwards XMP errors</exception>
        private static void CompareAliasedSubtrees(XmpNode aliasNode, XmpNode baseNode, bool outerCall)
        {
            if (baseNode.Value != aliasNode.Value || aliasNode.GetChildrenLength() != baseNode.GetChildrenLength())
            {
                throw new XmpException("Mismatch between alias and base nodes", XmpErrorCode.BadXmp);
            }
            if (!outerCall && (baseNode.Name != aliasNode.Name || !aliasNode.Options.Equals(baseNode.Options) || aliasNode.GetQualifierLength() != baseNode.GetQualifierLength()))
            {
                throw new XmpException("Mismatch between alias and base nodes", XmpErrorCode.BadXmp);
            }

            for (IIterator an = aliasNode.IterateChildren(), bn = baseNode.IterateChildren(); an.HasNext() && bn.HasNext();)
            {
                var aliasChild = (XmpNode)an.Next();
                var baseChild  = (XmpNode)bn.Next();
                CompareAliasedSubtrees(aliasChild, baseChild, false);
            }

            for (IIterator an = aliasNode.IterateQualifier(), bn1 = baseNode.IterateQualifier(); an.HasNext() && bn1.HasNext();)
            {
                var aliasQual = (XmpNode)an.Next();
                var baseQual  = (XmpNode)bn1.Next();
                CompareAliasedSubtrees(aliasQual, baseQual, false);
            }
        }
Пример #4
0
        /// <summary>See if an array is an alt-text array.</summary>
        /// <remarks>
        /// See if an array is an alt-text array. If so, make sure the x-default item
        /// is first.
        /// </remarks>
        /// <param name="arrayNode">the array node to check if its an alt-text array</param>
        internal static void DetectAltText(XmpNode arrayNode)
        {
            if (!arrayNode.Options.IsArrayAlternate || !arrayNode.HasChildren)
            {
                return;
            }

            var isAltText = false;

            for (var it = arrayNode.IterateChildren(); it.HasNext();)
            {
                var child = (XmpNode)it.Next();
                if (child.Options.HasLanguage)
                {
                    isAltText = true;
                    break;
                }
            }

            if (isAltText)
            {
                arrayNode.Options.IsArrayAltText = true;
                NormalizeLangArray(arrayNode);
            }
        }
Пример #5
0
            /// <summary>Prepares the next node to return if not already done.</summary>
            public virtual bool HasNext()
            {
                if (_returnProperty != null)
                {
                    // hasNext has been called before
                    return(true);
                }

                // find next node
                switch (_state)
                {
                case IterateNode:
                    return(ReportNode());

                case IterateChildren:
                    if (_childrenIterator == null)
                    {
                        _childrenIterator = _visitedNode.IterateChildren();
                    }
                    var hasNext = IterateChildrenMethod(_childrenIterator);
                    if (!hasNext && _visitedNode.HasQualifier && !_enclosing.Options.IsOmitQualifiers)
                    {
                        _state            = IterateQualifier;
                        _childrenIterator = null;
                        hasNext           = HasNext();
                    }
                    return(hasNext);
                }

                if (_childrenIterator == null)
                {
                    _childrenIterator = _visitedNode.IterateQualifier();
                }
                return(IterateChildrenMethod(_childrenIterator));
            }
Пример #6
0
 /// <summary>
 /// Serializes one schema with all contained properties in pretty-printed manner.
 /// </summary>
 /// <remarks>
 /// Each schema's properties are written to a single
 /// rdf:Description element. All of the necessary namespaces are declared in
 /// the rdf:Description element. The baseIndent is the base level for the
 /// entire serialization, that of the x:xmpmeta element. An xml:lang
 /// qualifier is written as an attribute of the property start tag, not by
 /// itself forcing the qualified property form.
 /// <code>
 /// &lt;rdf:Description rdf:about=&quot;TreeName&quot; xmlns:ns=&quot;URI&quot; ... &gt;
 /// ... The actual properties of the schema, see SerializePrettyRDFProperty
 /// &lt;!-- ns1:Alias is aliased to ns2:Actual --&gt;  ... If alias comments are wanted
 /// &lt;/rdf:Description&gt;
 /// </code>
 /// </remarks>
 /// <param name="schemaNode">a schema node</param>
 /// <param name="level"></param>
 /// <exception cref="System.IO.IOException">Forwarded writer exceptions</exception>
 /// <exception cref="XmpException"></exception>
 private void SerializeCanonicalRdfSchema(XmpNode schemaNode, int level)
 {
     // Write each of the schema's actual properties.
     for (var it = schemaNode.IterateChildren(); it.HasNext();)
     {
         var propNode = (XmpNode)it.Next();
         SerializeCanonicalRdfProperty(propNode, _options.UseCanonicalFormat, false, level + 2);
     }
 }
Пример #7
0
 /// <summary>Constructor</summary>
 /// <param name="enclosing"></param>
 /// <param name="parentNode">the node which children shall be iterated.</param>
 /// <param name="parentPath">the full path of the former node without the leaf node.</param>
 public NodeIteratorChildren(XmpIterator enclosing, XmpNode parentNode, string parentPath)
     : base(enclosing)
 {
     _enclosing = enclosing;
     if (parentNode.Options.IsSchemaNode)
     {
         _enclosing.BaseNamespace = parentNode.Name;
     }
     _parentPath       = AccumulatePath(parentNode, parentPath, 1);
     _childrenIterator = parentNode.IterateChildren();
 }
Пример #8
0
 /// <summary>Remove all schema children according to the flag <c>doAllProperties</c>.</summary>
 /// <remarks>Empty schemas are automatically remove by <c>XMPNode</c>.</remarks>
 /// <param name="schemaNode">a schema node</param>
 /// <param name="doAllProperties">flag if all properties or only externals shall be removed.</param>
 /// <returns>Returns true if the schema is empty after the operation.</returns>
 private static bool RemoveSchemaChildren(XmpNode schemaNode, bool doAllProperties)
 {
     for (var it = schemaNode.IterateChildren(); it.HasNext();)
     {
         var currProp = (XmpNode)it.Next();
         if (doAllProperties || !Utils.IsInternalProperty(schemaNode.Name, currProp.Name))
         {
             it.Remove();
         }
     }
     return(!schemaNode.HasChildren);
 }
Пример #9
0
 /// <summary>Remove all empty schemas from the metadata tree that were generated during the rdf parsing.</summary>
 /// <param name="tree">the root of the metadata tree</param>
 private static void DeleteEmptySchemas(XmpNode tree)
 {
     // Delete empty schema nodes. Do this last, other cleanup can make empty
     // schema.
     for (var it = tree.IterateChildren(); it.HasNext();)
     {
         var schema = (XmpNode)it.Next();
         if (!schema.HasChildren)
         {
             it.Remove();
         }
     }
 }
Пример #10
0
        /// <summary>Write each of the parent's simple unqualified properties as an attribute.</summary>
        /// <remarks>
        /// Write each of the parent's simple unqualified properties as an attribute. Returns true if all
        /// of the properties are written as attributes.
        /// </remarks>
        /// <param name="parentNode">the parent property node</param>
        /// <param name="indent">the current indent level</param>
        /// <returns>Returns true if all properties can be rendered as RDF attribute.</returns>
        /// <exception cref="System.IO.IOException" />
        private bool SerializeCompactRdfAttrProps(XmpNode parentNode, int indent)
        {
            var allAreAttrs = true;

            for (var it = parentNode.IterateChildren(); it.HasNext();)
            {
                var prop = (XmpNode)it.Next();
                if (CanBeRdfAttrProp(prop))
                {
                    WriteNewline();
                    WriteIndent(indent);
                    Write(prop.Name);
                    Write("=\"");
                    AppendNodeValue(prop.Value, true);
                    Write('"');
                }
                else
                {
                    allAreAttrs = false;
                }
            }
            return(allAreAttrs);
        }
Пример #11
0
        /// <summary>
        /// </summary>
        /// <remarks>
        /// <list>
        /// <item>Look for an exact match with the specific language.</item>
        /// <item>If a generic language is given, look for partial matches.</item>
        /// <item>Look for an "x-default"-item.</item>
        /// <item>Choose the first item.</item>
        /// </list>
        /// </remarks>
        /// <param name="arrayNode">the alt text array node</param>
        /// <param name="genericLang">the generic language</param>
        /// <param name="specificLang">the specific language</param>
        /// <returns>
        /// Returns the kind of match as an Integer and the found node in an
        /// array.
        /// </returns>
        /// <exception cref="XmpException"/>
        internal static object[] ChooseLocalizedText(XmpNode arrayNode, string genericLang, string specificLang)
        {
            // See if the array has the right form. Allow empty alt arrays,
            // that is what parsing returns.
            if (!arrayNode.Options.IsArrayAltText)
            {
                throw new XmpException("Localized text array is not alt-text", XmpErrorCode.BadXPath);
            }

            if (!arrayNode.HasChildren)
            {
                return new object[] { CltNoValues, null }
            }
            ;

            var     foundGenericMatches = 0;
            XmpNode resultNode          = null;
            XmpNode xDefault            = null;

            // Look for the first partial match with the generic language.
            for (var it = arrayNode.IterateChildren(); it.HasNext();)
            {
                var currItem = (XmpNode)it.Next();

                // perform some checks on the current item
                if (currItem.Options.IsCompositeProperty)
                {
                    throw new XmpException("Alt-text array item is not simple", XmpErrorCode.BadXPath);
                }

                if (!currItem.HasQualifier || currItem.GetQualifier(1).Name != XmpConstants.XmlLang)
                {
                    throw new XmpException("Alt-text array item has no language qualifier", XmpErrorCode.BadXPath);
                }

                var currLang = currItem.GetQualifier(1).Value;

                // Look for an exact match with the specific language.
                if (currLang == specificLang)
                {
                    return new object[] { CltSpecificMatch, currItem }
                }
                ;

                if (genericLang != null && currLang.StartsWith(genericLang))
                {
                    if (resultNode == null)
                    {
                        resultNode = currItem;
                    }

                    // ! Don't return/break, need to look for other matches.
                    foundGenericMatches++;
                }
                else if (currLang == XmpConstants.XDefault)
                {
                    xDefault = currItem;
                }
            }

            // evaluate loop
            if (foundGenericMatches == 1)
            {
                return new object[] { CltSingleGeneric, resultNode }
            }
            ;

            if (foundGenericMatches > 1)
            {
                return new object[] { CltMultipleGeneric, resultNode }
            }
            ;

            if (xDefault != null)
            {
                return new object[] { CltXDefault, xDefault }
            }
            ;

            // Everything failed, choose the first item.
            return(new object[] { CltFirstItem, arrayNode.GetChild(1) });
        }
Пример #12
0
        /// <summary>Compares two nodes including its children and qualifier.</summary>
        /// <param name="leftNode">an <c>XMPNode</c></param>
        /// <param name="rightNode">an <c>XMPNode</c></param>
        /// <returns>Returns true if the nodes are equal, false otherwise.</returns>
        /// <exception cref="XmpException">Forwards exceptions to the calling method.</exception>
        private static bool ItemValuesMatch(XmpNode leftNode, XmpNode rightNode)
        {
            var leftForm  = leftNode.Options;
            var rightForm = rightNode.Options;

            if (leftForm.Equals(rightForm))
            {
                return(false);
            }

            if (leftForm.GetOptions() == 0)
            {
                // Simple nodes, check the values and xml:lang qualifiers.
                if (!leftNode.Value.Equals(rightNode.Value))
                {
                    return(false);
                }
                if (leftNode.Options.HasLanguage != rightNode.Options.HasLanguage)
                {
                    return(false);
                }
                if (leftNode.Options.HasLanguage && !leftNode.GetQualifier(1).Value.Equals(rightNode.GetQualifier(1).Value))
                {
                    return(false);
                }
            }
            else
            {
                if (leftForm.IsStruct)
                {
                    // Struct nodes, see if all fields match, ignoring order.
                    if (leftNode.GetChildrenLength() != rightNode.GetChildrenLength())
                    {
                        return(false);
                    }

                    for (var it = leftNode.IterateChildren(); it.HasNext();)
                    {
                        var leftField  = (XmpNode)it.Next();
                        var rightField = XmpNodeUtils.FindChildNode(rightNode, leftField.Name, false);
                        if (rightField == null || !ItemValuesMatch(leftField, rightField))
                        {
                            return(false);
                        }
                    }
                }
                else
                {
                    // Array nodes, see if the "leftNode" values are present in the
                    // "rightNode", ignoring order, duplicates,
                    // and extra values in the rightNode-> The rightNode is the
                    // destination for AppendProperties.
                    Debug.Assert(leftForm.IsArray);
                    for (var il = leftNode.IterateChildren(); il.HasNext();)
                    {
                        var leftItem = (XmpNode)il.Next();
                        var match    = false;

                        for (var ir = rightNode.IterateChildren(); ir.HasNext();)
                        {
                            var rightItem = (XmpNode)ir.Next();
                            if (ItemValuesMatch(leftItem, rightItem))
                            {
                                match = true;
                                break;
                            }
                        }

                        if (!match)
                        {
                            return(false);
                        }
                    }
                }
            }
            return(true);
        }
Пример #13
0
        /// <param name="destXmp">The destination XMP object.</param>
        /// <param name="sourceNode">the source node</param>
        /// <param name="destParent">the parent of the destination node</param>
        /// <param name="replaceOldValues">Replace the values of existing properties.</param>
        /// <param name="deleteEmptyValues">flag if properties with empty values should be deleted in the destination object.</param>
        /// <exception cref="XmpException"/>
        private static void AppendSubtree(XmpMeta destXmp, XmpNode sourceNode, XmpNode destParent, bool replaceOldValues, bool deleteEmptyValues)
        {
            var destNode     = XmpNodeUtils.FindChildNode(destParent, sourceNode.Name, false);
            var valueIsEmpty = false;

            if (deleteEmptyValues)
            {
                valueIsEmpty = sourceNode.Options.IsSimple ? string.IsNullOrEmpty(sourceNode.Value) : !sourceNode.HasChildren;
            }

            if (deleteEmptyValues && valueIsEmpty)
            {
                if (destNode != null)
                {
                    destParent.RemoveChild(destNode);
                }
            }
            else
            {
                if (destNode == null)
                {
                    // The one easy case, the destination does not exist.
                    destParent.AddChild((XmpNode)sourceNode.Clone());
                }
                else
                {
                    if (replaceOldValues)
                    {
                        // The destination exists and should be replaced.
                        destXmp.SetNode(destNode, sourceNode.Value, sourceNode.Options, true);
                        destParent.RemoveChild(destNode);
                        destNode = (XmpNode)sourceNode.Clone();
                        destParent.AddChild(destNode);
                    }
                    else
                    {
                        // The destination exists and is not totally replaced. Structs and arrays are merged.
                        var sourceForm = sourceNode.Options;
                        var destForm   = destNode.Options;
                        if (sourceForm != destForm)
                        {
                            return;
                        }

                        if (sourceForm.IsStruct)
                        {
                            // To merge a struct process the fields recursively. E.g. add simple missing fields.
                            // The recursive call to AppendSubtree will handle deletion for fields with empty
                            // values.
                            for (var it = sourceNode.IterateChildren(); it.HasNext();)
                            {
                                var sourceField = (XmpNode)it.Next();
                                AppendSubtree(destXmp, sourceField, destNode, replaceOldValues, deleteEmptyValues);
                                if (deleteEmptyValues && !destNode.HasChildren)
                                {
                                    destParent.RemoveChild(destNode);
                                }
                            }
                        }
                        else if (sourceForm.IsArrayAltText)
                        {
                            // Merge AltText arrays by the "xml:lang" qualifiers. Make sure x-default is first.
                            // Make a special check for deletion of empty values. Meaningful in AltText arrays
                            // because the "xml:lang" qualifier provides unambiguous source/dest correspondence.
                            for (var it = sourceNode.IterateChildren(); it.HasNext();)
                            {
                                var sourceItem = (XmpNode)it.Next();

                                if (!sourceItem.HasQualifier || !XmpConstants.XmlLang.Equals(sourceItem.GetQualifier(1).Name))
                                {
                                    continue;
                                }

                                var destIndex = XmpNodeUtils.LookupLanguageItem(destNode, sourceItem.GetQualifier(1).Value);
                                if (deleteEmptyValues && string.IsNullOrEmpty(sourceItem.Value))
                                {
                                    if (destIndex != -1)
                                    {
                                        destNode.RemoveChild(destIndex);
                                        if (!destNode.HasChildren)
                                        {
                                            destParent.RemoveChild(destNode);
                                        }
                                    }
                                }
                                else if (destIndex == -1)
                                {
                                    // Not replacing, keep the existing item.
                                    if (!XmpConstants.XDefault.Equals(sourceItem.GetQualifier(1).Value) || !destNode.HasChildren)
                                    {
                                        sourceItem.CloneSubtree(destNode);
                                    }
                                    else
                                    {
                                        var destItem = new XmpNode(sourceItem.Name, sourceItem.Value, sourceItem.Options);
                                        sourceItem.CloneSubtree(destItem);
                                        destNode.AddChild(1, destItem);
                                    }
                                }
                            }
                        }
                        else if (sourceForm.IsArray)
                        {
                            // Merge other arrays by item values. Don't worry about order or duplicates. Source
                            // items with empty values do not cause deletion, that conflicts horribly with
                            // merging.
                            for (var children = sourceNode.IterateChildren(); children.HasNext();)
                            {
                                var sourceItem = (XmpNode)children.Next();

                                var match = false;
                                for (var id = destNode.IterateChildren(); id.HasNext();)
                                {
                                    var destItem = (XmpNode)id.Next();
                                    if (ItemValuesMatch(sourceItem, destItem))
                                    {
                                        match = true;
                                    }
                                }

                                if (!match)
                                {
                                    destNode = (XmpNode)sourceItem.Clone();
                                    destParent.AddChild(destNode);
                                }
                            }
                        }
                    }
                }
            }
        }
Пример #14
0
        /// <summary>Recursively handles the "value" for a node.</summary>
        /// <remarks>
        /// Recursively handles the "value" for a node. It does not matter if it is a
        /// top level property, a field of a struct, or an item of an array. The
        /// indent is that for the property element. An xml:lang qualifier is written
        /// as an attribute of the property start tag, not by itself forcing the
        /// qualified property form. The patterns below mostly ignore attribute
        /// qualifiers like xml:lang. Except for the one struct case, attribute
        /// qualifiers don't affect the output form.
        /// <code>
        /// &lt;ns:UnqualifiedSimpleProperty&gt;value&lt;/ns:UnqualifiedSimpleProperty&gt;
        /// &lt;ns:UnqualifiedStructProperty&gt; (If no rdf:resource qualifier)
        /// &lt;rdf:Description&gt;
        /// ... Fields, same forms as top level properties
        /// &lt;/rdf:Description&gt;
        /// &lt;/ns:UnqualifiedStructProperty&gt;
        /// &lt;ns:ResourceStructProperty rdf:resource=&quot;URI&quot;
        /// ... Fields as attributes
        /// &gt;
        /// &lt;ns:UnqualifiedArrayProperty&gt;
        /// &lt;rdf:Bag&gt; or Seq or Alt
        /// ... Array items as rdf:li elements, same forms as top level properties
        /// &lt;/rdf:Bag&gt;
        /// &lt;/ns:UnqualifiedArrayProperty&gt;
        /// &lt;ns:QualifiedProperty&gt;
        /// &lt;rdf:Description&gt;
        /// &lt;rdf:value&gt; ... Property &quot;value&quot; following the unqualified
        /// forms ... &lt;/rdf:value&gt;
        /// ... Qualifiers looking like named struct fields
        /// &lt;/rdf:Description&gt;
        /// &lt;/ns:QualifiedProperty&gt;
        /// </code>
        /// </remarks>
        /// <param name="node">the property node</param>
        /// <param name="emitAsRdfValue">property shall be rendered as attribute rather than tag</param>
        /// <param name="useCanonicalRdf">
        /// use canonical form with inner description tag or
        /// the compact form with rdf:ParseType=&quot;resource&quot; attribute.
        /// </param>
        /// <param name="indent">the current indent level</param>
        /// <exception cref="System.IO.IOException">Forwards all writer exceptions.</exception>
        /// <exception cref="XmpException">If &quot;rdf:resource&quot; and general qualifiers are mixed.</exception>
        private void SerializeCanonicalRdfProperty(XmpNode node, bool useCanonicalRdf, bool emitAsRdfValue, int indent)
        {
            var emitEndTag   = true;
            var indentEndTag = true;

            // Determine the XML element name. Open the start tag with the name and
            // attribute qualifiers.
            var elemName = node.Name;

            if (emitAsRdfValue)
            {
                elemName = "rdf:value";
            }
            else if (elemName == XmpConstants.ArrayItemName)
            {
                elemName = XmpConstants.RdfLi;
            }

            WriteIndent(indent);
            Write('<');
            Write(elemName);
            var hasGeneralQualifiers = false;
            var hasRdfResourceQual   = false;

            for (var it = node.IterateQualifier(); it.HasNext();)
            {
                var qualifier = (XmpNode)it.Next();
                if (!RdfAttrQualifier.Contains(qualifier.Name))
                {
                    hasGeneralQualifiers = true;
                }
                else
                {
                    hasRdfResourceQual = qualifier.Name == "rdf:resource";
                    if (!emitAsRdfValue)
                    {
                        Write(' ');
                        Write(qualifier.Name);
                        Write("=\"");
                        AppendNodeValue(qualifier.Value, true);
                        Write('"');
                    }
                }
            }
            // Process the property according to the standard patterns.
            if (hasGeneralQualifiers && !emitAsRdfValue)
            {
                // This node has general, non-attribute, qualifiers. Emit using the
                // qualified property form.
                // ! The value is output by a recursive call ON THE SAME NODE with
                // emitAsRDFValue set.
                if (hasRdfResourceQual)
                {
                    throw new XmpException("Can't mix rdf:resource and general qualifiers", XmpErrorCode.BadRdf);
                }

                // Change serialization to canonical format with inner rdf:Description-tag
                // depending on option
                if (useCanonicalRdf)
                {
                    Write(">");
                    WriteNewline();
                    indent++;
                    WriteIndent(indent);
                    Write(RdfStructStart);
                    Write(">");
                }
                else
                {
                    Write(" rdf:parseType=\"Resource\">");
                }

                WriteNewline();
                SerializeCanonicalRdfProperty(node, useCanonicalRdf, true, indent + 1);

                for (var it = node.IterateQualifier(); it.HasNext();)
                {
                    var qualifier = (XmpNode)it.Next();
                    if (!RdfAttrQualifier.Contains(qualifier.Name))
                    {
                        SerializeCanonicalRdfProperty(qualifier, useCanonicalRdf, false, indent + 1);
                    }
                }

                if (useCanonicalRdf)
                {
                    WriteIndent(indent);
                    Write(RdfStructEnd);
                    WriteNewline();
                    indent--;
                }
            }
            else
            {
                // This node has no general qualifiers. Emit using an unqualified form.
                if (!node.Options.IsCompositeProperty)
                {
                    // This is a simple property.
                    if (node.Options.IsUri)
                    {
                        Write(" rdf:resource=\"");
                        AppendNodeValue(node.Value, true);
                        Write("\"/>");
                        WriteNewline();
                        emitEndTag = false;
                    }
                    else if (string.IsNullOrEmpty(node.Value))
                    {
                        Write("/>");
                        WriteNewline();
                        emitEndTag = false;
                    }
                    else
                    {
                        Write('>');
                        AppendNodeValue(node.Value, false);
                        indentEndTag = false;
                    }
                }
                else
                {
                    if (node.Options.IsArray)
                    {
                        // This is an array.
                        Write('>');
                        WriteNewline();
                        EmitRdfArrayTag(node, true, indent + 1);

                        if (node.Options.IsArrayAltText)
                        {
                            XmpNodeUtils.NormalizeLangArray(node);
                        }

                        for (var it1 = node.IterateChildren(); it1.HasNext();)
                        {
                            var child = (XmpNode)it1.Next();
                            SerializeCanonicalRdfProperty(child, useCanonicalRdf, false, indent + 2);
                        }

                        EmitRdfArrayTag(node, false, indent + 1);
                    }
                    else if (!hasRdfResourceQual)
                    {
                        // This is a "normal" struct, use the rdf:parseType="Resource" form.
                        if (!node.HasChildren)
                        {
                            // Change serialization to canonical format with inner rdf:Description-tag
                            // if option is set
                            if (useCanonicalRdf)
                            {
                                Write(">");
                                WriteNewline();
                                WriteIndent(indent + 1);
                                Write(RdfEmptyStruct);
                            }
                            else
                            {
                                Write(" rdf:parseType=\"Resource\"/>");
                                emitEndTag = false;
                            }

                            WriteNewline();
                        }
                        else
                        {
                            // Change serialization to canonical format with inner rdf:Description-tag
                            // if option is set
                            if (useCanonicalRdf)
                            {
                                Write(">");
                                WriteNewline();
                                indent++;
                                WriteIndent(indent);
                                Write(RdfStructStart);
                                Write(">");
                            }
                            else
                            {
                                Write(" rdf:parseType=\"Resource\">");
                            }

                            WriteNewline();
                            for (var it = node.IterateChildren(); it.HasNext();)
                            {
                                var child = (XmpNode)it.Next();
                                SerializeCanonicalRdfProperty(child, useCanonicalRdf, false, indent + 1);
                            }

                            if (useCanonicalRdf)
                            {
                                WriteIndent(indent);
                                Write(RdfStructEnd);
                                WriteNewline();
                                indent--;
                            }
                        }
                    }
                    else
                    {
                        // This is a struct with an rdf:resource attribute, use the
                        // "empty property element" form.
                        for (var it1 = node.IterateChildren(); it1.HasNext();)
                        {
                            var child = (XmpNode)it1.Next();
                            if (!CanBeRdfAttrProp(child))
                            {
                                throw new XmpException("Can't mix rdf:resource and complex fields", XmpErrorCode.BadRdf);
                            }

                            WriteNewline();
                            WriteIndent(indent + 1);
                            Write(' ');
                            Write(child.Name);
                            Write("=\"");
                            AppendNodeValue(child.Value, true);
                            Write('"');
                        }

                        Write("/>");
                        WriteNewline();
                        emitEndTag = false;
                    }
                }
            }
            // Emit the property element end tag.
            if (emitEndTag)
            {
                if (indentEndTag)
                {
                    WriteIndent(indent);
                }
                Write("</");
                Write(elemName);
                Write('>');
                WriteNewline();
            }
        }
Пример #15
0
        /// <summary>Serializes a struct property.</summary>
        /// <param name="node">an XMPNode</param>
        /// <param name="indent">the current indent level</param>
        /// <param name="hasRdfResourceQual">Flag if the element has resource qualifier</param>
        /// <returns>Returns true if an end flag shall be emitted.</returns>
        /// <exception cref="System.IO.IOException">Forwards the writer exceptions.</exception>
        /// <exception cref="XmpException">If qualifier and element fields are mixed.</exception>
        private bool SerializeCompactRdfStructProp(XmpNode node, int indent, bool hasRdfResourceQual)
        {
            // This must be a struct.
            var hasAttrFields = false;
            var hasElemFields = false;
            var emitEndTag    = true;

            for (var ic = node.IterateChildren(); ic.HasNext();)
            {
                var field = (XmpNode)ic.Next();

                if (CanBeRdfAttrProp(field))
                {
                    hasAttrFields = true;
                }
                else
                {
                    hasElemFields = true;
                }

                if (hasAttrFields && hasElemFields)
                {
                    break;
                }
            }

            // No sense looking further.
            if (hasRdfResourceQual && hasElemFields)
            {
                throw new XmpException("Can't mix rdf:resource qualifier and element fields", XmpErrorCode.BadRdf);
            }

            if (!node.HasChildren)
            {
                // Catch an empty struct as a special case. The case
                // below would emit an empty
                // XML element, which gets reparsed as a simple property
                // with an empty value.
                Write(" rdf:parseType=\"Resource\"/>");
                WriteNewline();
                emitEndTag = false;
            }
            else if (!hasElemFields)
            {
                // All fields can be attributes, use the
                // emptyPropertyElt form.
                SerializeCompactRdfAttrProps(node, indent + 1);
                Write("/>");
                WriteNewline();
                emitEndTag = false;
            }
            else if (!hasAttrFields)
            {
                // All fields must be elements, use the
                // parseTypeResourcePropertyElt form.
                Write(" rdf:parseType=\"Resource\">");
                WriteNewline();
                SerializeCompactRdfElementProps(node, indent + 1);
            }
            else
            {
                // Have a mix of attributes and elements, use an inner rdf:Description.
                Write('>');
                WriteNewline();
                WriteIndent(indent + 1);
                Write(RdfStructStart);
                SerializeCompactRdfAttrProps(node, indent + 2);
                Write(">");
                WriteNewline();
                SerializeCompactRdfElementProps(node, indent + 1);
                WriteIndent(indent + 1);
                Write(RdfStructEnd);
                WriteNewline();
            }

            return(emitEndTag);
        }
Пример #16
0
        /// <summary>
        /// Recursively handles the "value" for a node that must be written as an RDF
        /// property element.
        /// </summary>
        /// <remarks>
        /// Recursively handles the "value" for a node that must be written as an RDF
        /// property element. It does not matter if it is a top level property, a
        /// field of a struct, or an item of an array. The indent is that for the
        /// property element. The patterns below ignore attribute qualifiers such as
        /// xml:lang, they don't affect the output form.
        /// <code>
        /// &lt;ns:UnqualifiedStructProperty-1
        /// ... The fields as attributes, if all are simple and unqualified
        /// /&gt;
        /// &lt;ns:UnqualifiedStructProperty-2 rdf:parseType=&quot;Resource&quot;&gt;
        /// ... The fields as elements, if none are simple and unqualified
        /// &lt;/ns:UnqualifiedStructProperty-2&gt;
        /// &lt;ns:UnqualifiedStructProperty-3&gt;
        /// &lt;rdf:Description
        /// ... The simple and unqualified fields as attributes
        /// &gt;
        /// ... The compound or qualified fields as elements
        /// &lt;/rdf:Description&gt;
        /// &lt;/ns:UnqualifiedStructProperty-3&gt;
        /// &lt;ns:UnqualifiedArrayProperty&gt;
        /// &lt;rdf:Bag&gt; or Seq or Alt
        /// ... Array items as rdf:li elements, same forms as top level properties
        /// &lt;/rdf:Bag&gt;
        /// &lt;/ns:UnqualifiedArrayProperty&gt;
        /// &lt;ns:QualifiedProperty rdf:parseType=&quot;Resource&quot;&gt;
        /// &lt;rdf:value&gt; ... Property &quot;value&quot;
        /// following the unqualified forms ... &lt;/rdf:value&gt;
        /// ... Qualifiers looking like named struct fields
        /// &lt;/ns:QualifiedProperty&gt;
        /// </code>
        /// *** Consider numbered array items, but has compatibility problems.
        /// Consider qualified form with rdf:Description and attributes.
        /// </remarks>
        /// <param name="parentNode">the parent node</param>
        /// <param name="indent">the current indent level</param>
        /// <exception cref="System.IO.IOException">Forwards writer exceptions</exception>
        /// <exception cref="XmpException">If qualifier and element fields are mixed.</exception>
        private void SerializeCompactRdfElementProps(XmpNode parentNode, int indent)
        {
            for (var it = parentNode.IterateChildren(); it.HasNext();)
            {
                var node = (XmpNode)it.Next();

                if (CanBeRdfAttrProp(node))
                {
                    continue;
                }

                var emitEndTag   = true;
                var indentEndTag = true;
                // Determine the XML element name, write the name part of the start tag. Look over the
                // qualifiers to decide on "normal" versus "rdf:value" form. Emit the attribute
                // qualifiers at the same time.
                var elemName = node.Name;
                if (elemName == XmpConstants.ArrayItemName)
                {
                    elemName = XmpConstants.RdfLi;
                }

                WriteIndent(indent);
                Write('<');
                Write(elemName);
                var hasGeneralQualifiers = false;
                var hasRdfResourceQual   = false;
                for (var iq = node.IterateQualifier(); iq.HasNext();)
                {
                    var qualifier = (XmpNode)iq.Next();
                    if (!RdfAttrQualifier.Contains(qualifier.Name))
                    {
                        hasGeneralQualifiers = true;
                    }
                    else
                    {
                        hasRdfResourceQual = qualifier.Name == "rdf:resource";
                        Write(' ');
                        Write(qualifier.Name);
                        Write("=\"");
                        AppendNodeValue(qualifier.Value, true);
                        Write('"');
                    }
                }

                // Process the property according to the standard patterns.
                if (hasGeneralQualifiers)
                {
                    SerializeCompactRdfGeneralQualifier(indent, node);
                }
                else
                {
                    // This node has only attribute qualifiers. Emit as a property element.
                    if (!node.Options.IsCompositeProperty)
                    {
                        var result = SerializeCompactRdfSimpleProp(node);
                        emitEndTag   = (bool)result[0];
                        indentEndTag = (bool)result[1];
                    }
                    else
                    {
                        if (node.Options.IsArray)
                        {
                            SerializeCompactRdfArrayProp(node, indent);
                        }
                        else
                        {
                            emitEndTag = SerializeCompactRdfStructProp(node, indent, hasRdfResourceQual);
                        }
                    }
                }

                // Emit the property element end tag.
                if (emitEndTag)
                {
                    if (indentEndTag)
                    {
                        WriteIndent(indent);
                    }

                    Write("</");
                    Write(elemName);
                    Write('>');
                    WriteNewline();
                }
            }
        }