ParserRDF.java 27.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
/*
 * Decompiled with CFR 0_118.
 */
package com.adobe.xmp.core.parser.impl;

import com.adobe.xmp.core.XMPArray;
import com.adobe.xmp.core.XMPException;
import com.adobe.xmp.core.XMPLanguageAlternative;
import com.adobe.xmp.core.XMPMetadata;
import com.adobe.xmp.core.XMPMetadataFactory;
import com.adobe.xmp.core.XMPNode;
import com.adobe.xmp.core.XMPQualifiers;
import com.adobe.xmp.core.XMPSimple;
import com.adobe.xmp.core.XMPStruct;
import com.adobe.xmp.core.parser.ParseRDFException;
import com.adobe.xmp.core.parser.ParseXMPException;
import com.adobe.xmp.core.parser.RDFXMLParserContext;
import com.adobe.xmp.core.parser.impl.ParserRDFUtils;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/*
 * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
 */
class ParserRDF {
    private static final String RDF_TEMP_NS = "RDF_TEMP_NS";

    ParserRDF() {
    }

    public static void parse(XMPMetadata xmp, Node xmlRoot, Map<String, Object> parseContext) throws XMPException {
        HashMap prefixMap;
        if (xmlRoot.hasAttributes()) {
            prefixMap = parseContext.get(RDFXMLParserContext.PREFIX_DEFINITIONS);
            if (prefixMap == null || !(prefixMap instanceof Map)) {
                prefixMap = new HashMap();
                parseContext.put(RDFXMLParserContext.PREFIX_DEFINITIONS, prefixMap);
            }
        } else {
            throw new ParseRDFException("Invalid attributes of rdf:RDF element");
        }
        ParserRDF.rdf_NodeElementList(xmp, xmlRoot, prefixMap);
    }

    private static void rdf_NodeElementList(XMPNode xmpNode, Node rdfRDFNode, Map<String, String> prefixMap) throws XMPException {
        for (int i = 0; i < rdfRDFNode.getChildNodes().getLength(); ++i) {
            Node child = rdfRDFNode.getChildNodes().item(i);
            if (ParserRDFUtils.isWhitespaceNode(child)) continue;
            ParserRDF.rdf_NodeElement(xmpNode, child, ParserRDFUtils.getRDFTermKind(child), prefixMap);
        }
    }

    private static void rdf_NodeElement(XMPNode xmpParent, Node xmlNode, ParserRDFUtils.RDFTerm xmlNodeTerm, Map<String, String> prefixMap) throws XMPException {
        if (xmlNodeTerm != ParserRDFUtils.RDFTerm.DESCRIPTION && !ParserRDFUtils.isOtherTerm(xmlNodeTerm)) {
            throw new ParseRDFException("Node element must be rdf:Description or typed node");
        }
        if (xmpParent instanceof XMPMetadata && ParserRDFUtils.isOtherTerm(xmlNodeTerm)) {
            throw new ParseRDFException("Top level typed node not allowed");
        }
        ParserRDF.rdf_NodeElementAttrs(xmpParent, xmlNode, prefixMap);
        ParserRDF.rdf_PropertyElementList(xmpParent, xmlNode, prefixMap);
    }

    private static void rdf_NodeElementAttrs(XMPNode xmpParent, Node xmlNode, Map<String, String> prefixMap) throws XMPException {
        int exclusiveAttrs = 0;
        block4 : for (int i = 0; i < xmlNode.getAttributes().getLength(); ++i) {
            Node attribute = xmlNode.getAttributes().item(i);
            if (ParserRDFUtils.isNSDeclaration(attribute)) continue;
            ParserRDFUtils.RDFTerm attrTerm = ParserRDFUtils.getRDFTermKind(attribute);
            switch (attrTerm) {
                case ID: 
                case NODE_ID: 
                case ABOUT: {
                    if (exclusiveAttrs > 0) {
                        throw new ParseRDFException("Mutally exclusive about, ID, nodeID attributes");
                    }
                    ++exclusiveAttrs;
                    if (!(xmpParent instanceof XMPMetadata) || attrTerm != ParserRDFUtils.RDFTerm.ABOUT) continue block4;
                    XMPMetadata xmp = (XMPMetadata)xmpParent;
                    if (xmp.getAboutURI() == null || xmp.getAboutURI().length() == 0) {
                        xmp.setAboutURI(attribute.getNodeValue());
                        continue block4;
                    }
                    if (attribute.getNodeValue().equals(xmp.getAboutURI())) continue block4;
                    throw new ParseXMPException("Mismatched top level rdf:about values");
                }
                case BAG: 
                case SEQ: 
                case ALT: 
                case VALUE: 
                case OTHER: {
                    ParserRDF.addSimpleChild(xmpParent, attribute, attribute.getNodeValue(), prefixMap);
                    continue block4;
                }
                default: {
                    throw new ParseRDFException("Invalid nodeElement attribute");
                }
            }
        }
    }

    private static void rdf_PropertyElementList(XMPNode xmpParent, Node xmlParent, Map<String, String> prefixMap) throws XMPException {
        XMPNode rdfValueStructContainer = null;
        for (int i = 0; i < xmlParent.getChildNodes().getLength(); ++i) {
            Node currChild = xmlParent.getChildNodes().item(i);
            if (currChild.getNodeType() == 1) {
                if (!ParserRDF.isRDFValue(currChild)) {
                    ParserRDF.rdf_PropertyElement(xmpParent, currChild, prefixMap);
                    continue;
                }
                if (rdfValueStructContainer == null) {
                    rdfValueStructContainer = XMPMetadataFactory.create();
                    if (!(xmpParent instanceof XMPMetadata)) {
                        rdfValueStructContainer = rdfValueStructContainer.setStruct("RDF_TEMP_NS", "value");
                    }
                }
                ParserRDF.rdf_PropertyElement(rdfValueStructContainer, currChild, prefixMap);
                continue;
            }
            if (ParserRDFUtils.isWhitespaceNode(currChild)) continue;
            throw new ParseRDFException("Expected property element node not found");
        }
        if (rdfValueStructContainer != null && rdfValueStructContainer.size() > 0 && xmpParent instanceof XMPStruct) {
            if (rdfValueStructContainer.size() > 1) {
                throw new ParseRDFException("Found more than 1 rdf:value elements for a property.");
            }
            XMPNode valueNode = rdfValueStructContainer.iterator().next();
            ParserRDF.fixupQualifiedNode((XMPStruct)xmpParent, valueNode, false);
        }
    }

    private static boolean isRDFValue(Node node) {
        return ParserRDFUtils.getRDFTermKind(node) == ParserRDFUtils.RDFTerm.VALUE;
    }

    private static void rdf_PropertyElement(XMPNode xmpParent, Node xmlNode, Map<String, String> prefixMap) throws XMPException {
        Node attribute;
        int i;
        ParserRDFUtils.RDFTerm nodeTerm = ParserRDFUtils.getRDFTermKind(xmlNode);
        if (!ParserRDFUtils.isPropertyElementName(nodeTerm)) {
            throw new ParseRDFException("Invalid property element name");
        }
        NamedNodeMap attributes = xmlNode.getAttributes();
        for (i = attributes.getLength() - 1; i >= 0; --i) {
            attribute = attributes.item(i);
            if (!ParserRDFUtils.isNSDeclaration(attribute)) continue;
            attributes.removeNamedItemNS(attribute.getNamespaceURI(), attribute.getLocalName());
        }
        if (attributes.getLength() > 3) {
            ParserRDF.rdf_EmptyPropertyElement(xmpParent, xmlNode, prefixMap);
        } else {
            for (i = 0; i < attributes.getLength(); ++i) {
                attribute = attributes.item(i);
                ParserRDFUtils.RDFTerm term = ParserRDFUtils.getRDFTermKind(attribute);
                String attrValue = attribute.getNodeValue();
                if (term == ParserRDFUtils.RDFTerm.XML_LANG || term == ParserRDFUtils.RDFTerm.ID) continue;
                if (term == ParserRDFUtils.RDFTerm.DATATYPE) {
                    ParserRDF.rdf_LiteralPropertyElement(xmpParent, xmlNode, prefixMap);
                } else if (term == ParserRDFUtils.RDFTerm.PARSE_TYPE) {
                    if ("Literal".equals(attrValue)) {
                        ParserRDF.rdf_ParseTypeLiteralPropertyElement();
                    } else if ("Resource".equals(attrValue)) {
                        ParserRDF.rdf_ParseTypeResourcePropertyElement(xmpParent, xmlNode, prefixMap);
                    } else if ("Collection".equals(attrValue)) {
                        ParserRDF.rdf_ParseTypeCollectionPropertyElement();
                    } else {
                        ParserRDF.rdf_ParseTypeOtherPropertyElement();
                    }
                } else {
                    ParserRDF.rdf_EmptyPropertyElement(xmpParent, xmlNode, prefixMap);
                }
                return;
            }
            if (xmlNode.hasChildNodes()) {
                for (i = 0; i < xmlNode.getChildNodes().getLength(); ++i) {
                    Node currChild = xmlNode.getChildNodes().item(i);
                    if (currChild.getNodeType() == 3) continue;
                    ParserRDF.rdf_ResourcePropertyElement(xmpParent, xmlNode, prefixMap);
                    return;
                }
                ParserRDF.rdf_LiteralPropertyElement(xmpParent, xmlNode, prefixMap);
            } else {
                ParserRDF.rdf_EmptyPropertyElement(xmpParent, xmlNode, prefixMap);
            }
        }
    }

    private static void rdf_EmptyPropertyElement(XMPNode xmpParent, Node xmlNode, Map<String, String> prefixMap) throws XMPException {
        boolean hasPropertyAttrs = false;
        boolean hasResourceAttr = false;
        boolean hasNodeIDAttr = false;
        boolean hasValueAttr = false;
        Node valueNode = null;
        if (xmlNode.hasChildNodes()) {
            throw new ParseRDFException("Nested content not allowed with rdf:resource or property attributes");
        }
        block13 : for (int i = 0; i < xmlNode.getAttributes().getLength(); ++i) {
            Node attribute = xmlNode.getAttributes().item(i);
            if (ParserRDFUtils.isNSDeclaration(attribute)) continue;
            ParserRDFUtils.RDFTerm attrTerm = ParserRDFUtils.getRDFTermKind(attribute);
            switch (attrTerm) {
                case ID: {
                    continue block13;
                }
                case RESOURCE: {
                    if (hasNodeIDAttr) {
                        throw new ParseRDFException("Empty property element can't have both rdf:resource and rdf:nodeID");
                    }
                    if (hasValueAttr) {
                        throw new ParseXMPException("Empty property element can't have both rdf:value and rdf:resource");
                    }
                    hasResourceAttr = true;
                    if (hasValueAttr) continue block13;
                    valueNode = attribute;
                    continue block13;
                }
                case NODE_ID: {
                    if (hasResourceAttr) {
                        throw new ParseRDFException("Empty property element can't have both rdf:resource and rdf:nodeID");
                    }
                    hasNodeIDAttr = true;
                    continue block13;
                }
                case VALUE: {
                    if (hasResourceAttr) {
                        throw new ParseXMPException("Empty property element can't have both rdf:value and rdf:resource");
                    }
                    hasValueAttr = true;
                    valueNode = attribute;
                }
                case XML_LANG: {
                    continue block13;
                }
                case OTHER: {
                    hasPropertyAttrs = true;
                    continue block13;
                }
                default: {
                    throw new ParseRDFException("Unrecognized attribute of empty property element");
                }
            }
        }
        XMPNode childNode = null;
        boolean childIsStruct = false;
        if (hasValueAttr || hasResourceAttr) {
            String value = valueNode != null ? valueNode.getNodeValue() : "";
            childNode = ParserRDF.addSimpleChild(xmpParent, xmlNode, value, prefixMap);
            if (childNode != null && !hasValueAttr) {
                childNode.adaptTo(XMPSimple.class).setURI(true);
            }
        } else if (hasPropertyAttrs) {
            childNode = ParserRDF.addStructChild(xmpParent, xmlNode, prefixMap);
            childIsStruct = true;
        } else {
            childNode = ParserRDF.addSimpleChild(xmpParent, xmlNode, "", prefixMap);
        }
        block14 : for (int i2 = 0; i2 < xmlNode.getAttributes().getLength(); ++i2) {
            Node attribute = xmlNode.getAttributes().item(i2);
            if (attribute == valueNode || ParserRDFUtils.isNSDeclaration(attribute)) continue;
            ParserRDFUtils.RDFTerm attrTerm = ParserRDFUtils.getRDFTermKind(attribute);
            switch (attrTerm) {
                case ID: 
                case NODE_ID: {
                    continue block14;
                }
                case RESOURCE: {
                    childNode.accessQualifiers().setSimple("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "resource", attribute.getNodeValue());
                    continue block14;
                }
                case BAG: 
                case SEQ: 
                case ALT: 
                case VALUE: 
                case OTHER: 
                case XML_LANG: {
                    if (!childIsStruct || attrTerm == ParserRDFUtils.RDFTerm.XML_LANG) {
                        childNode.accessQualifiers().setSimple(attribute.getNamespaceURI(), attribute.getLocalName(), attribute.getNodeValue());
                        ParserRDF.collectPrefix(attribute, prefixMap);
                        continue block14;
                    }
                    ParserRDF.addSimpleChild(childNode, attribute, attribute.getNodeValue(), prefixMap);
                    continue block14;
                }
                default: {
                    throw new ParseRDFException("Unrecognized attribute of empty property element");
                }
            }
        }
    }

    private static void rdf_LiteralPropertyElement(XMPNode xmpParent, Node xmlNode, Map<String, String> prefixMap) throws XMPException {
        String textValue = "";
        for (int i = 0; i < xmlNode.getChildNodes().getLength(); ++i) {
            Node child = xmlNode.getChildNodes().item(i);
            if (child.getNodeType() != 3) {
                throw new ParseRDFException("Invalid child of literal property element");
            }
            textValue = textValue + child.getNodeValue();
        }
        XMPSimple newChild = ParserRDF.addSimpleChild(xmpParent, xmlNode, textValue, prefixMap);
        for (int i2 = 0; i2 < xmlNode.getAttributes().getLength(); ++i2) {
            Node attribute = xmlNode.getAttributes().item(i2);
            if (ParserRDFUtils.isNSDeclaration(attribute)) continue;
            ParserRDFUtils.RDFTerm attrTerm = ParserRDFUtils.getRDFTermKind(attribute);
            if (attrTerm == ParserRDFUtils.RDFTerm.XML_LANG) {
                newChild.accessQualifiers().setSimple(attribute.getNamespaceURI(), attribute.getLocalName(), attribute.getNodeValue());
                ParserRDF.collectPrefix(attribute, prefixMap);
                continue;
            }
            if (attrTerm == ParserRDFUtils.RDFTerm.ID || attrTerm == ParserRDFUtils.RDFTerm.DATATYPE) continue;
            throw new ParseRDFException("Invalid attribute for literal property element");
        }
    }

    private static void rdf_ParseTypeResourcePropertyElement(XMPNode xmpParent, Node xmlNode, Map<String, String> prefixMap) throws XMPException {
        XMPStruct newStruct = ParserRDF.addStructChild(xmpParent, xmlNode, prefixMap);
        for (int i = 0; i < xmlNode.getAttributes().getLength(); ++i) {
            Node attribute = xmlNode.getAttributes().item(i);
            if (ParserRDFUtils.isNSDeclaration(attribute)) continue;
            ParserRDFUtils.RDFTerm term = ParserRDFUtils.getRDFTermKind(attribute);
            if (term == ParserRDFUtils.RDFTerm.XML_LANG) {
                newStruct.accessQualifiers().setSimple(attribute.getNamespaceURI(), attribute.getLocalName(), attribute.getNodeValue());
                ParserRDF.collectPrefix(attribute, prefixMap);
                continue;
            }
            if (term == ParserRDFUtils.RDFTerm.ID || term == ParserRDFUtils.RDFTerm.PARSE_TYPE) continue;
            throw new ParseRDFException("Invalid attribute for ParseType Resource property element");
        }
        ParserRDF.rdf_PropertyElementList(newStruct, xmlNode, prefixMap);
    }

    private static void rdf_ResourcePropertyElement(XMPNode xmpParent, Node xmlNode, Map<String, String> prefixMap) throws XMPException {
        XMPNode rdfValueContainer = null;
        XMPNode newCompound = null;
        Node currChild = null;
        boolean found = false;
        for (int i = 0; i < xmlNode.getChildNodes().getLength(); ++i) {
            currChild = xmlNode.getChildNodes().item(i);
            if (ParserRDFUtils.isWhitespaceNode(currChild)) continue;
            if (currChild.getNodeType() == 1 && !found) {
                XMPLanguageAlternative langAlt;
                ParserRDFUtils.RDFTerm childTerm = ParserRDFUtils.getRDFTermKind(currChild);
                if (childTerm == ParserRDFUtils.RDFTerm.BAG) {
                    newCompound = ParserRDF.addArrayChild(xmpParent, xmlNode, XMPArray.Form.UNORDERED, prefixMap);
                } else if (childTerm == ParserRDFUtils.RDFTerm.SEQ) {
                    newCompound = ParserRDF.addArrayChild(xmpParent, xmlNode, XMPArray.Form.ORDERED, prefixMap);
                } else if (childTerm == ParserRDFUtils.RDFTerm.ALT) {
                    newCompound = ParserRDF.addArrayChild(xmpParent, xmlNode, XMPArray.Form.ALTERNATIVE, prefixMap);
                } else {
                    if (!ParserRDF.isRDFValue(xmlNode)) {
                        newCompound = ParserRDF.addStructChild(xmpParent, xmlNode, prefixMap);
                    } else {
                        if (rdfValueContainer == null) {
                            rdfValueContainer = XMPMetadataFactory.create();
                            if (!(xmpParent instanceof XMPMetadata)) {
                                rdfValueContainer = rdfValueContainer.setStruct("RDF_TEMP_NS", "value");
                            }
                        }
                        newCompound = ParserRDF.addStructChild(rdfValueContainer, xmlNode, prefixMap);
                    }
                    if (childTerm != ParserRDFUtils.RDFTerm.DESCRIPTION) {
                        String typeName = currChild.getNamespaceURI();
                        if (typeName == null) {
                            throw new ParseXMPException("All XML elements must be in a namespace");
                        }
                        typeName = typeName + ':' + currChild.getLocalName();
                    }
                }
                ParserRDF.rdf_NodeElement(newCompound, currChild, childTerm, prefixMap);
                if (rdfValueContainer != null && rdfValueContainer.size() > 0 && xmpParent instanceof XMPStruct) {
                    if (rdfValueContainer.size() > 1) {
                        throw new ParseRDFException("Found more than 1 rdf:value elements for a property.");
                    }
                    XMPNode valueNode = rdfValueContainer.iterator().next();
                    ParserRDF.fixupQualifiedNode((XMPStruct)xmpParent, valueNode, true);
                } else if (newCompound instanceof XMPArray && ((XMPArray)newCompound).getForm() == XMPArray.Form.ALTERNATIVE && (langAlt = XMPLanguageAlternative.newInstance((XMPArray)newCompound)) != null) {
                    langAlt.normalize();
                }
                found = true;
                continue;
            }
            if (found) {
                throw new ParseRDFException("Invalid child of resource property element");
            }
            throw new ParseRDFException("Children of resource property element must be XML elements");
        }
        for (int j = 0; j < xmlNode.getAttributes().getLength(); ++j) {
            Node attribute = xmlNode.getAttributes().item(j);
            if (ParserRDFUtils.isNSDeclaration(attribute)) continue;
            ParserRDFUtils.RDFTerm term = ParserRDFUtils.getRDFTermKind(attribute);
            if (term == ParserRDFUtils.RDFTerm.XML_LANG) {
                newCompound.accessQualifiers().setSimple(attribute.getNamespaceURI(), attribute.getLocalName(), attribute.getNodeValue());
                ParserRDF.collectPrefix(attribute, prefixMap);
                continue;
            }
            if (term == ParserRDFUtils.RDFTerm.ID) continue;
            throw new ParseRDFException("Invalid attribute for resource property element");
        }
        if (!found) {
            throw new ParseRDFException("Missing child of resource property element");
        }
    }

    private static void rdf_ParseTypeLiteralPropertyElement() throws XMPException {
        throw new ParseXMPException("ParseTypeLiteral property element not allowed");
    }

    private static void rdf_ParseTypeCollectionPropertyElement() throws XMPException {
        throw new ParseXMPException("ParseTypeCollection property element not allowed");
    }

    private static void rdf_ParseTypeOtherPropertyElement() throws XMPException {
        throw new ParseXMPException("ParseTypeOther property element not allowed");
    }

    private static void fixupQualifiedNode(XMPStruct xmpParent, XMPNode rdfValueNode, boolean addAsChild) throws XMPException {
        if (rdfValueNode == null) {
            return;
        }
        XMPStruct parent = addAsChild ? xmpParent : xmpParent.getParent();
        for (XMPNode newQualifier : xmpParent) {
            if (newQualifier instanceof XMPSimple) {
                rdfValueNode.accessQualifiers().copy((XMPSimple)newQualifier);
                continue;
            }
            if (newQualifier instanceof XMPStruct) {
                rdfValueNode.accessQualifiers().copy((XMPStruct)newQualifier);
                continue;
            }
            if (!(newQualifier instanceof XMPArray)) continue;
            rdfValueNode.accessQualifiers().copy((XMPArray)newQualifier);
        }
        if (parent instanceof XMPStruct) {
            if (rdfValueNode instanceof XMPSimple) {
                parent.copy((XMPSimple)rdfValueNode);
            } else if (rdfValueNode instanceof XMPStruct) {
                parent.copy((XMPStruct)rdfValueNode);
            } else if (rdfValueNode instanceof XMPArray) {
                parent.copy((XMPArray)rdfValueNode);
            }
            parent.renameField("RDF_TEMP_NS", "value", xmpParent.getNamespace(), xmpParent.getName());
        } else if (parent instanceof XMPArray) {
            XMPNode createdNode;
            if (rdfValueNode instanceof XMPSimple) {
                createdNode = ((XMPArray)((Object)parent)).appendSimple("");
                createdNode.copyReplace(rdfValueNode);
            } else if (rdfValueNode instanceof XMPStruct) {
                createdNode = ((XMPArray)((Object)parent)).appendStruct();
                createdNode.copyReplace(rdfValueNode);
            } else if (rdfValueNode instanceof XMPArray) {
                createdNode = ((XMPArray)((Object)parent)).appendArray(((XMPArray)rdfValueNode).getForm());
                createdNode.copyReplace(rdfValueNode);
            }
            XMPArray parentArray = (XMPArray)((Object)parent);
            for (int i = 0; i < parentArray.size(); ++i) {
                if (parentArray.get(i) != xmpParent) continue;
                parentArray.remove(i);
                break;
            }
        }
    }

    private static XMPSimple addSimpleChild(XMPNode xmpParent, Node xmlNode, String value, Map<String, String> prefixMap) throws XMPException {
        XMPSimple result = null;
        String namespace = xmlNode.getNamespaceURI();
        String localName = xmlNode.getLocalName();
        ParserRDF.collectPrefix(xmlNode, prefixMap);
        if (namespace == null || namespace.length() == 0) {
            throw new ParseRDFException("XML namespace required for all elements and attributes");
        }
        if (namespace.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#")) {
            if (!(xmpParent instanceof XMPArray) && ParserRDFUtils.RDF_LI.equals(localName)) {
                throw new ParseRDFException("Misplaced rdf:value element");
            }
            if (xmpParent instanceof XMPMetadata && "value".equals(localName)) {
                throw new ParseRDFException("Misplaced rdf:li element");
            }
        }
        if (xmpParent instanceof XMPStruct) {
            if (namespace.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#")) {
                namespace = "RDF_TEMP_NS";
            }
            result = ((XMPStruct)xmpParent).setSimple(namespace, localName, value);
        } else if (xmpParent instanceof XMPArray) {
            if (!ParserRDFUtils.isNumberedArrayItemName(namespace, localName)) {
                throw new ParseRDFException("Arrays cannot have arbitrary child names");
            }
            result = ((XMPArray)xmpParent).appendSimple(value);
        }
        return result;
    }

    private static XMPStruct addStructChild(XMPNode xmpParent, Node xmlNode, Map<String, String> prefixMap) throws XMPException {
        XMPStruct result = null;
        String namespace = xmlNode.getNamespaceURI();
        String localName = xmlNode.getLocalName();
        ParserRDF.collectPrefix(xmlNode, prefixMap);
        if (namespace == null || namespace.length() == 0) {
            throw new ParseRDFException("XML namespace required for all elements and attributes");
        }
        if (namespace.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#")) {
            namespace = "RDF_TEMP_NS";
        }
        if (xmpParent instanceof XMPStruct) {
            result = ((XMPStruct)xmpParent).setStruct(namespace, localName);
        } else if (xmpParent instanceof XMPArray) {
            result = ((XMPArray)xmpParent).appendStruct();
        }
        return result;
    }

    private static XMPArray addArrayChild(XMPNode parent, Node xmlNode, XMPArray.Form arrayForm, Map<String, String> prefixMap) throws XMPException {
        XMPArray result = null;
        String namespace = "RDF_TEMP_NS".equals(parent.getNamespace()) ? parent.getNamespace() : xmlNode.getNamespaceURI();
        String localName = xmlNode.getLocalName();
        ParserRDF.collectPrefix(xmlNode, prefixMap);
        if (namespace == null || namespace.length() == 0) {
            throw new ParseRDFException("XML namespace required for all elements and attributes");
        }
        if ("http://www.w3.org/1999/02/22-rdf-syntax-ns#".equals(namespace)) {
            throw new ParseRDFException("Misplaced rdf:* element");
        }
        if (parent instanceof XMPStruct) {
            result = ((XMPStruct)parent).setArray(namespace, localName, arrayForm);
        } else if (parent instanceof XMPArray) {
            result = ((XMPArray)parent).appendArray(arrayForm);
        }
        return result;
    }

    private static void removeNode(XMPNode nodeToDelete) {
        XMPNode parent = nodeToDelete.getParent();
        if (parent instanceof XMPStruct) {
            ((XMPStruct)parent).remove(nodeToDelete.getNamespace(), nodeToDelete.getName());
        } else if (parent instanceof XMPArray) {
            ((XMPArray)parent).remove(((XMPArray)parent).size() - 1);
        }
    }

    public static void collectPrefix(Node node, Map<String, String> prefixMap) {
        if (node.getNamespaceURI() != null && node.getPrefix() != null && node.getNamespaceURI().length() > 0 && node.getPrefix().length() > 0) {
            prefixMap.put(node.getPrefix(), node.getNamespaceURI());
        }
    }

}