XMPNodeUtils.java 16.9 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
/*
 * Decompiled with CFR 0_118.
 */
package com.adobe.internal.xmp.impl;

import com.adobe.internal.xmp.XMPConst;
import com.adobe.internal.xmp.XMPDateTime;
import com.adobe.internal.xmp.XMPDateTimeFactory;
import com.adobe.internal.xmp.XMPException;
import com.adobe.internal.xmp.XMPMetaFactory;
import com.adobe.internal.xmp.XMPUtils;
import com.adobe.internal.xmp.impl.Utils;
import com.adobe.internal.xmp.impl.XMPNode;
import com.adobe.internal.xmp.impl.xpath.XMPPath;
import com.adobe.internal.xmp.impl.xpath.XMPPathSegment;
import com.adobe.internal.xmp.options.PropertyOptions;
import java.util.GregorianCalendar;
import java.util.Iterator;

public class XMPNodeUtils
implements XMPConst {
    static final int CLT_NO_VALUES = 0;
    static final int CLT_SPECIFIC_MATCH = 1;
    static final int CLT_SINGLE_GENERIC = 2;
    static final int CLT_MULTIPLE_GENERIC = 3;
    static final int CLT_XDEFAULT = 4;
    static final int CLT_FIRST_ITEM = 5;

    private XMPNodeUtils() {
    }

    static XMPNode findSchemaNode(XMPNode tree, String namespaceURI, boolean createNodes) throws XMPException {
        return XMPNodeUtils.findSchemaNode(tree, namespaceURI, null, createNodes);
    }

    static XMPNode findSchemaNode(XMPNode tree, String namespaceURI, String suggestedPrefix, boolean createNodes) throws XMPException {
        assert (tree.getParent() == null);
        XMPNode schemaNode = tree.findChildByName(namespaceURI);
        if (schemaNode == null && createNodes) {
            schemaNode = new XMPNode(namespaceURI, new PropertyOptions().setSchemaNode(true));
            schemaNode.setImplicit(true);
            String prefix = XMPMetaFactory.getSchemaRegistry().getNamespacePrefix(namespaceURI);
            if (prefix == null) {
                if (suggestedPrefix != null && suggestedPrefix.length() != 0) {
                    prefix = XMPMetaFactory.getSchemaRegistry().registerNamespace(namespaceURI, suggestedPrefix);
                } else {
                    throw new XMPException("Unregistered schema namespace URI", 101);
                }
            }
            schemaNode.setValue(prefix);
            tree.addChild(schemaNode);
        }
        return schemaNode;
    }

    static XMPNode findChildNode(XMPNode parent, String childName, boolean createNodes) throws XMPException {
        XMPNode childNode;
        if (!parent.getOptions().isSchemaNode() && !parent.getOptions().isStruct()) {
            if (!parent.isImplicit()) {
                throw new XMPException("Named children only allowed for schemas and structs", 102);
            }
            if (parent.getOptions().isArray()) {
                throw new XMPException("Named children not allowed for arrays", 102);
            }
            if (createNodes) {
                parent.getOptions().setStruct(true);
            }
        }
        if ((childNode = parent.findChildByName(childName)) == null && createNodes) {
            PropertyOptions options = new PropertyOptions();
            childNode = new XMPNode(childName, options);
            childNode.setImplicit(true);
            parent.addChild(childNode);
        }
        assert (childNode != null || !createNodes);
        return childNode;
    }

    static XMPNode findNode(XMPNode xmpTree, XMPPath xpath, boolean createNodes, PropertyOptions leafOptions) throws XMPException {
        if (xpath == null || xpath.size() == 0) {
            throw new XMPException("Empty XMPPath", 102);
        }
        XMPNode rootImplicitNode = null;
        XMPNode currNode = null;
        currNode = XMPNodeUtils.findSchemaNode(xmpTree, xpath.getSegment(0).getName(), createNodes);
        if (currNode == null) {
            return null;
        }
        if (currNode.isImplicit()) {
            currNode.setImplicit(false);
            rootImplicitNode = currNode;
        }
        try {
            for (int i = 1; i < xpath.size(); ++i) {
                if ((currNode = XMPNodeUtils.followXPathStep(currNode, xpath.getSegment(i), createNodes)) == null) {
                    if (createNodes) {
                        XMPNodeUtils.deleteNode(rootImplicitNode);
                    }
                    return null;
                }
                if (!currNode.isImplicit()) continue;
                currNode.setImplicit(false);
                if (i == 1 && xpath.getSegment(i).isAlias() && xpath.getSegment(i).getAliasForm() != 0) {
                    currNode.getOptions().setOption(xpath.getSegment(i).getAliasForm(), true);
                } else if (i < xpath.size() - 1 && xpath.getSegment(i).getKind() == 1 && !currNode.getOptions().isCompositeProperty()) {
                    currNode.getOptions().setStruct(true);
                }
                if (rootImplicitNode != null) continue;
                rootImplicitNode = currNode;
            }
        }
        catch (XMPException e) {
            if (rootImplicitNode != null) {
                XMPNodeUtils.deleteNode(rootImplicitNode);
            }
            throw e;
        }
        if (rootImplicitNode != null) {
            currNode.getOptions().mergeWith(leafOptions);
            currNode.setOptions(currNode.getOptions());
        }
        return currNode;
    }

    static void deleteNode(XMPNode node) {
        XMPNode parent = node.getParent();
        if (node.getOptions().isQualifier()) {
            parent.removeQualifier(node);
        } else {
            parent.removeChild(node);
        }
        if (!parent.hasChildren() && parent.getOptions().isSchemaNode()) {
            parent.getParent().removeChild(parent);
        }
    }

    static void setNodeValue(XMPNode node, Object value) {
        String strValue = XMPNodeUtils.serializeNodeValue(value);
        if (!node.getOptions().isQualifier() || !"xml:lang".equals(node.getName())) {
            node.setValue(strValue);
        } else {
            node.setValue(Utils.normalizeLangValue(strValue));
        }
    }

    static PropertyOptions verifySetOptions(PropertyOptions options, Object itemValue) throws XMPException {
        if (options == null) {
            options = new PropertyOptions();
        }
        if (options.isArrayAltText()) {
            options.setArrayAlternate(true);
        }
        if (options.isArrayAlternate()) {
            options.setArrayOrdered(true);
        }
        if (options.isArrayOrdered()) {
            options.setArray(true);
        }
        if (options.isCompositeProperty() && itemValue != null && itemValue.toString().length() > 0) {
            throw new XMPException("Structs and arrays can't have values", 103);
        }
        options.assertConsistency(options.getOptions());
        return options;
    }

    static String serializeNodeValue(Object value) {
        String strValue;
        if (value == null) {
            strValue = null;
        } else if (value instanceof Boolean) {
            strValue = XMPUtils.convertFromBoolean((Boolean)value);
        } else if (value instanceof Integer) {
            strValue = XMPUtils.convertFromInteger((Integer)value);
        } else if (value instanceof Long) {
            strValue = XMPUtils.convertFromLong((Long)value);
        } else if (value instanceof Double) {
            strValue = XMPUtils.convertFromDouble((Double)value);
        } else if (value instanceof XMPDateTime) {
            strValue = XMPUtils.convertFromDate((XMPDateTime)value);
        } else if (value instanceof GregorianCalendar) {
            XMPDateTime dt = XMPDateTimeFactory.createFromCalendar((GregorianCalendar)value);
            strValue = XMPUtils.convertFromDate(dt);
        } else {
            strValue = value instanceof byte[] ? XMPUtils.encodeBase64((byte[])value) : value.toString();
        }
        return strValue != null ? Utils.removeControlChars(strValue) : null;
    }

    private static XMPNode followXPathStep(XMPNode parentNode, XMPPathSegment nextStep, boolean createNodes) throws XMPException {
        XMPNode nextNode = null;
        int index = 0;
        int stepKind = nextStep.getKind();
        if (stepKind == 1) {
            nextNode = XMPNodeUtils.findChildNode(parentNode, nextStep.getName(), createNodes);
        } else if (stepKind == 2) {
            nextNode = XMPNodeUtils.findQualifierNode(parentNode, nextStep.getName().substring(1), createNodes);
        } else {
            if (!parentNode.getOptions().isArray()) {
                throw new XMPException("Indexing applied to non-array", 102);
            }
            if (stepKind == 3) {
                index = XMPNodeUtils.findIndexedItem(parentNode, nextStep.getName(), createNodes);
            } else if (stepKind == 4) {
                index = parentNode.getChildrenLength();
            } else if (stepKind == 6) {
                String[] result = Utils.splitNameAndValue(nextStep.getName());
                String fieldName = result[0];
                String fieldValue = result[1];
                index = XMPNodeUtils.lookupFieldSelector(parentNode, fieldName, fieldValue);
            } else if (stepKind == 5) {
                String[] result = Utils.splitNameAndValue(nextStep.getName());
                String qualName = result[0];
                String qualValue = result[1];
                index = XMPNodeUtils.lookupQualSelector(parentNode, qualName, qualValue, nextStep.getAliasForm());
            } else {
                throw new XMPException("Unknown array indexing step in FollowXPathStep", 9);
            }
            if (1 <= index && index <= parentNode.getChildrenLength()) {
                nextNode = parentNode.getChild(index);
            }
        }
        return nextNode;
    }

    private static XMPNode findQualifierNode(XMPNode parent, String qualName, boolean createNodes) throws XMPException {
        assert (!qualName.startsWith("?"));
        XMPNode qualNode = parent.findQualifierByName(qualName);
        if (qualNode == null && createNodes) {
            qualNode = new XMPNode(qualName, null);
            qualNode.setImplicit(true);
            parent.addQualifier(qualNode);
        }
        return qualNode;
    }

    private static int findIndexedItem(XMPNode arrayNode, String segment, boolean createNodes) throws XMPException {
        int index = 0;
        try {
            segment = segment.substring(1, segment.length() - 1);
            index = Integer.parseInt(segment);
            if (index < 1) {
                throw new XMPException("Array index must be larger than zero", 102);
            }
        }
        catch (NumberFormatException e) {
            throw new XMPException("Array index not digits.", 102);
        }
        if (createNodes && index == arrayNode.getChildrenLength() + 1) {
            XMPNode newItem = new XMPNode("[]", null);
            newItem.setImplicit(true);
            arrayNode.addChild(newItem);
        }
        return index;
    }

    private static int lookupFieldSelector(XMPNode arrayNode, String fieldName, String fieldValue) throws XMPException {
        int result = -1;
        block0 : for (int index = 1; index <= arrayNode.getChildrenLength() && result < 0; ++index) {
            XMPNode currItem = arrayNode.getChild(index);
            if (!currItem.getOptions().isStruct()) {
                throw new XMPException("Field selector must be used on array of struct", 102);
            }
            for (int f = 1; f <= currItem.getChildrenLength(); ++f) {
                XMPNode currField = currItem.getChild(f);
                if (!fieldName.equals(currField.getName()) || !fieldValue.equals(currField.getValue())) continue;
                result = index;
                continue block0;
            }
        }
        return result;
    }

    private static int lookupQualSelector(XMPNode arrayNode, String qualName, String qualValue, int aliasForm) throws XMPException {
        if ("xml:lang".equals(qualName)) {
            int index = XMPNodeUtils.lookupLanguageItem(arrayNode, qualValue = Utils.normalizeLangValue(qualValue));
            if (index < 0 && (aliasForm & 4096) > 0) {
                XMPNode langNode = new XMPNode("[]", null);
                XMPNode xdefault = new XMPNode("xml:lang", "x-default", null);
                langNode.addQualifier(xdefault);
                arrayNode.addChild(1, langNode);
                return 1;
            }
            return index;
        }
        for (int index = 1; index < arrayNode.getChildrenLength(); ++index) {
            XMPNode currItem = arrayNode.getChild(index);
            Iterator it = currItem.iterateQualifier();
            while (it.hasNext()) {
                XMPNode qualifier = (XMPNode)it.next();
                if (!qualName.equals(qualifier.getName()) || !qualValue.equals(qualifier.getValue())) continue;
                return index;
            }
        }
        return -1;
    }

    static void normalizeLangArray(XMPNode arrayNode) {
        if (!arrayNode.getOptions().isArrayAltText()) {
            return;
        }
        for (int i = 2; i <= arrayNode.getChildrenLength(); ++i) {
            XMPNode child;
            block4 : {
                child = arrayNode.getChild(i);
                if (!child.hasQualifier() || !"x-default".equals(child.getQualifier(1).getValue())) continue;
                try {
                    arrayNode.removeChild(i);
                    arrayNode.addChild(1, child);
                }
                catch (XMPException e) {
                    if ($assertionsDisabled) break block4;
                    throw new AssertionError();
                }
            }
            if (i != 2) break;
            arrayNode.getChild(2).setValue(child.getValue());
            break;
        }
    }

    static void detectAltText(XMPNode arrayNode) {
        if (arrayNode.getOptions().isArrayAlternate() && arrayNode.hasChildren()) {
            boolean isAltText = false;
            Iterator it = arrayNode.iterateChildren();
            while (it.hasNext()) {
                XMPNode child = (XMPNode)it.next();
                if (!child.getOptions().getHasLanguage()) continue;
                isAltText = true;
                break;
            }
            if (isAltText) {
                arrayNode.getOptions().setArrayAltText(true);
                XMPNodeUtils.normalizeLangArray(arrayNode);
            }
        }
    }

    static void appendLangItem(XMPNode arrayNode, String itemLang, String itemValue) throws XMPException {
        XMPNode newItem = new XMPNode("[]", itemValue, null);
        XMPNode langQual = new XMPNode("xml:lang", itemLang, null);
        newItem.addQualifier(langQual);
        if (!"x-default".equals(langQual.getValue())) {
            arrayNode.addChild(newItem);
        } else {
            arrayNode.addChild(1, newItem);
        }
    }

    static Object[] chooseLocalizedText(XMPNode arrayNode, String genericLang, String specificLang) throws XMPException {
        if (!arrayNode.getOptions().isArrayAltText()) {
            throw new XMPException("Localized text array is not alt-text", 102);
        }
        if (!arrayNode.hasChildren()) {
            return new Object[]{new Integer(0), null};
        }
        int foundGenericMatches = 0;
        XMPNode resultNode = null;
        XMPNode xDefault = null;
        Iterator it = arrayNode.iterateChildren();
        while (it.hasNext()) {
            XMPNode currItem = (XMPNode)it.next();
            if (currItem.getOptions().isCompositeProperty()) {
                throw new XMPException("Alt-text array item is not simple", 102);
            }
            if (!currItem.hasQualifier() || !"xml:lang".equals(currItem.getQualifier(1).getName())) {
                throw new XMPException("Alt-text array item has no language qualifier", 102);
            }
            String currLang = currItem.getQualifier(1).getValue();
            if (specificLang.equals(currLang)) {
                return new Object[]{new Integer(1), currItem};
            }
            if (genericLang != null && currLang.startsWith(genericLang)) {
                if (resultNode == null) {
                    resultNode = currItem;
                }
                ++foundGenericMatches;
                continue;
            }
            if (!"x-default".equals(currLang)) continue;
            xDefault = currItem;
        }
        if (foundGenericMatches == 1) {
            return new Object[]{new Integer(2), resultNode};
        }
        if (foundGenericMatches > 1) {
            return new Object[]{new Integer(3), resultNode};
        }
        if (xDefault != null) {
            return new Object[]{new Integer(4), xDefault};
        }
        return new Object[]{new Integer(5), arrayNode.getChild(1)};
    }

    static int lookupLanguageItem(XMPNode arrayNode, String language) throws XMPException {
        if (!arrayNode.getOptions().isArray()) {
            throw new XMPException("Language item must be used on array", 102);
        }
        for (int index = 1; index <= arrayNode.getChildrenLength(); ++index) {
            XMPNode child = arrayNode.getChild(index);
            if (!child.hasQualifier() || !"xml:lang".equals(child.getQualifier(1).getName()) || !language.equals(child.getQualifier(1).getValue())) continue;
            return index;
        }
        return -1;
    }
}