Пример #1
0
        /**
         * Parses the root node of an XMP Path, checks if namespace and prefix fit together
         * and resolve the property to the base property if it is an alias.
         * @param schemaNs the root namespace
         * @param pos the parsing position helper
         * @param expandedXPath  the path to contribute to
         * @throws XmpException If the path is not valid.
         */

        internal static void ParseRootNode(string schemaNs, PathPosition pos, XmpPath expandedXPath)
        {
            while (pos.StepEnd < pos.Path.Length && "/[*".IndexOf(pos.Path[pos.StepEnd]) < 0)
            {
                pos.StepEnd++;
            }

            if (pos.StepEnd == pos.StepBegin)
            {
                throw new XmpException("Empty initial XmpPath step", XmpError.BADXPATH);
            }

            string        rootProp  = VerifyXPathRoot(schemaNs, pos.Path.Substring(pos.StepBegin, pos.StepEnd - pos.StepBegin));
            IXmpAliasInfo aliasInfo = XmpMetaFactory.SchemaRegistry.FindAlias(rootProp);

            if (aliasInfo == null)
            {
                // add schema xpath step
                expandedXPath.Add(new XmpPathSegment(schemaNs, XmpPath.SCHEMA_NODE));
                XmpPathSegment rootStep = new XmpPathSegment(rootProp, XmpPath.STRUCT_FIELD_STEP);
                expandedXPath.Add(rootStep);
            }
            else
            {
                // add schema xpath step and base step of alias
                expandedXPath.Add(new XmpPathSegment(aliasInfo.Namespace, XmpPath.SCHEMA_NODE));
                XmpPathSegment rootStep = new XmpPathSegment(VerifyXPathRoot(aliasInfo.Namespace, aliasInfo.PropName),
                                                             XmpPath.STRUCT_FIELD_STEP);
                rootStep.Alias     = true;
                rootStep.AliasForm = aliasInfo.AliasForm.Options;
                expandedXPath.Add(rootStep);

                if (aliasInfo.AliasForm.ArrayAltText)
                {
                    XmpPathSegment qualSelectorStep = new XmpPathSegment("[?xml:lang='x-default']",
                                                                         XmpPath.QUAL_SELECTOR_STEP);
                    qualSelectorStep.Alias     = true;
                    qualSelectorStep.AliasForm = aliasInfo.AliasForm.Options;
                    expandedXPath.Add(qualSelectorStep);
                }
                else if (aliasInfo.AliasForm.Array)
                {
                    XmpPathSegment indexStep = new XmpPathSegment("[1]", XmpPath.ARRAY_INDEX_STEP);
                    indexStep.Alias     = true;
                    indexStep.AliasForm = aliasInfo.AliasForm.Options;
                    expandedXPath.Add(indexStep);
                }
            }
        }
        public IXmpAliasInfo[] FindAliases(string aliasNs)
        {
            string prefix = GetNamespacePrefix(aliasNs);
            IList  result = new ArrayList();

            if (prefix != null)
            {
                for (IEnumerator it = _aliasMap.Keys.GetEnumerator(); it.MoveNext();)
                {
                    string qname = (string)it.Current;
                    if (qname != null && qname.StartsWith(prefix))
                    {
                        result.Add(FindAlias(qname));
                    }
                }
            }
            IXmpAliasInfo[] array = new IXmpAliasInfo[result.Count];
            result.CopyTo(array, 0);
            return(array);
        }
Пример #3
0
 public IXmpAliasInfo[] FindAliases(string aliasNs) {
     string prefix = GetNamespacePrefix(aliasNs);
     IList result = new ArrayList();
     if (prefix != null) {
         for (IEnumerator it = _aliasMap.Keys.GetEnumerator(); it.MoveNext();) {
             string qname = (string) it.Current;
             if (qname != null && qname.StartsWith(prefix)) {
                 result.Add(FindAlias(qname));
             }
         }
     }
     IXmpAliasInfo[] array = new IXmpAliasInfo[result.Count];
     result.CopyTo(array, 0);
     return array;
 }
Пример #4
0
        /// <seealso cref= XMPUtils#removeProperties(XMPMeta, String, String, boolean, boolean)
        /// </seealso>
        /// <param name="xmp">
        ///            The XMP object containing the properties to be removed.
        /// </param>
        /// <param name="schemaNs">
        ///            Optional schema namespace URI for the properties to be
        ///            removed.
        /// </param>
        /// <param name="propName">
        ///            Optional path expression for the property to be removed.
        /// </param>
        /// <param name="doAllProperties">
        ///            Option flag to control the deletion: do internal properties in
        ///            addition to external properties. </param>
        /// <param name="includeAliases">
        ///            Option flag to control the deletion: Include aliases in the
        ///            "named schema" case above. </param>
        /// <exception cref="XmpException"> If metadata processing fails </exception>
        public static void RemoveProperties(IXmpMeta xmp, string schemaNs, string propName, bool doAllProperties,
                                            bool includeAliases)
        {
            ParameterAsserts.AssertImplementation(xmp);
            XmpMetaImpl xmpImpl = (XmpMetaImpl)xmp;

            if (!string.IsNullOrEmpty(propName))
            {
                // Remove just the one indicated property. This might be an alias,
                // the named schema might not actually exist. So don't lookup the
                // schema node.

                if (string.IsNullOrEmpty(schemaNs))
                {
                    throw new XmpException("Property name requires schema namespace", XmpError.BADPARAM);
                }

                XmpPath expPath = XmpPathParser.ExpandXPath(schemaNs, propName);

                XmpNode propNode = XmpNodeUtils.FindNode(xmpImpl.Root, expPath, false, null);
                if (propNode != null)
                {
                    if (doAllProperties ||
                        !Utils.IsInternalProperty(expPath.GetSegment((int)XmpPath.STEP_SCHEMA).Name,
                                                  expPath.GetSegment((int)XmpPath.STEP_ROOT_PROP).Name))
                    {
                        XmpNode parent = propNode.Parent;
                        parent.RemoveChild(propNode);
                        if (parent.Options.SchemaNode && !parent.HasChildren())
                        {
                            // remove empty schema node
                            parent.Parent.RemoveChild(parent);
                        }
                    }
                }
            }
            else if (!string.IsNullOrEmpty(schemaNs))
            {
                // Remove all properties from the named schema. Optionally include
                // aliases, in which case
                // there might not be an actual schema node.

                // XMP_NodePtrPos schemaPos;
                XmpNode schemaNode = XmpNodeUtils.FindSchemaNode(xmpImpl.Root, schemaNs, false);
                if (schemaNode != null)
                {
                    if (RemoveSchemaChildren(schemaNode, doAllProperties))
                    {
                        xmpImpl.Root.RemoveChild(schemaNode);
                    }
                }

                if (includeAliases)
                {
                    // We're removing the aliases also. Look them up by their
                    // namespace prefix.
                    // But that takes more code and the extra speed isn't worth it.
                    // Lookup the XMP node
                    // from the alias, to make sure the actual exists.

                    IXmpAliasInfo[] aliases = XmpMetaFactory.SchemaRegistry.FindAliases(schemaNs);
                    for (int i = 0; i < aliases.Length; i++)
                    {
                        IXmpAliasInfo info       = aliases[i];
                        XmpPath       path       = XmpPathParser.ExpandXPath(info.Namespace, info.PropName);
                        XmpNode       actualProp = XmpNodeUtils.FindNode(xmpImpl.Root, path, false, null);
                        if (actualProp != null)
                        {
                            XmpNode parent = actualProp.Parent;
                            parent.RemoveChild(actualProp);
                        }
                    }
                }
            }
            else
            {
                // Remove all appropriate properties from all schema. In this case
                // we don't have to be
                // concerned with aliases, they are handled implicitly from the
                // actual properties.
                ArrayList schemasToRemove = new ArrayList();
                for (IEnumerator it = xmpImpl.Root.IterateChildren(); it.MoveNext();)
                {
                    XmpNode schema = (XmpNode)it.Current;
                    if (schema == null)
                    {
                        continue;
                    }
                    if (RemoveSchemaChildren(schema, doAllProperties))
                    {
                        schemasToRemove.Add(schema);
                    }
                }
                foreach (XmpNode xmpNode in schemasToRemove)
                {
                    xmpImpl.Root.Children.Remove(xmpNode);
                }
                schemasToRemove.Clear();
            }
        }