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

import com.adobe.internal.xmp.XMPException;
import com.adobe.internal.xmp.options.PropertyOptions;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

class XMPNode
implements Comparable {
    private String name;
    private String value;
    private XMPNode parent;
    private List children = null;
    private List qualifier = null;
    private PropertyOptions options = null;
    private boolean implicit;
    private boolean hasAliases;
    private boolean alias;
    private boolean hasValueChild;

    public XMPNode(String name, String value, PropertyOptions options) {
        this.name = name;
        this.value = value;
        this.options = options;
    }

    public XMPNode(String name, PropertyOptions options) {
        this(name, null, options);
    }

    public void clear() {
        this.options = null;
        this.name = null;
        this.value = null;
        this.children = null;
        this.qualifier = null;
    }

    public XMPNode getParent() {
        return this.parent;
    }

    public XMPNode getChild(int index) {
        return (XMPNode)this.getChildren().get(index - 1);
    }

    public void addChild(XMPNode node) throws XMPException {
        this.assertChildNotExisting(node.getName());
        node.setParent(this);
        this.getChildren().add(node);
    }

    public void addChild(int index, XMPNode node) throws XMPException {
        this.assertChildNotExisting(node.getName());
        node.setParent(this);
        this.getChildren().add(index - 1, node);
    }

    public void replaceChild(int index, XMPNode node) {
        node.setParent(this);
        this.getChildren().set(index - 1, node);
    }

    public void removeChild(int itemIndex) {
        this.getChildren().remove(itemIndex - 1);
        this.cleanupChildren();
    }

    public void removeChild(XMPNode node) {
        this.getChildren().remove(node);
        this.cleanupChildren();
    }

    protected void cleanupChildren() {
        if (this.children.isEmpty()) {
            this.children = null;
        }
    }

    public void removeChildren() {
        this.children = null;
    }

    public int getChildrenLength() {
        return this.children != null ? this.children.size() : 0;
    }

    public XMPNode findChildByName(String expr) {
        return this.find(this.getChildren(), expr);
    }

    public XMPNode getQualifier(int index) {
        return (XMPNode)this.getQualifier().get(index - 1);
    }

    public int getQualifierLength() {
        return this.qualifier != null ? this.qualifier.size() : 0;
    }

    public void addQualifier(XMPNode qualNode) throws XMPException {
        this.assertQualifierNotExisting(qualNode.getName());
        qualNode.setParent(this);
        qualNode.getOptions().setQualifier(true);
        this.getOptions().setHasQualifiers(true);
        if (qualNode.isLanguageNode()) {
            this.options.setHasLanguage(true);
            this.getQualifier().add(0, qualNode);
        } else if (qualNode.isTypeNode()) {
            this.options.setHasType(true);
            this.getQualifier().add(!this.options.getHasLanguage() ? 0 : 1, qualNode);
        } else {
            this.getQualifier().add(qualNode);
        }
    }

    public void removeQualifier(XMPNode qualNode) {
        PropertyOptions opts = this.getOptions();
        if (qualNode.isLanguageNode()) {
            opts.setHasLanguage(false);
        } else if (qualNode.isTypeNode()) {
            opts.setHasType(false);
        }
        this.getQualifier().remove(qualNode);
        if (this.qualifier.isEmpty()) {
            opts.setHasQualifiers(false);
            this.qualifier = null;
        }
    }

    public void removeQualifiers() {
        PropertyOptions opts = this.getOptions();
        opts.setHasQualifiers(false);
        opts.setHasLanguage(false);
        opts.setHasType(false);
        this.qualifier = null;
    }

    public XMPNode findQualifierByName(String expr) {
        return this.find(this.qualifier, expr);
    }

    public boolean hasChildren() {
        return this.children != null && this.children.size() > 0;
    }

    public Iterator iterateChildren() {
        if (this.children != null) {
            return this.getChildren().iterator();
        }
        return Collections.EMPTY_LIST.listIterator();
    }

    public boolean hasQualifier() {
        return this.qualifier != null && this.qualifier.size() > 0;
    }

    public Iterator iterateQualifier() {
        if (this.qualifier != null) {
            final Iterator it = this.getQualifier().iterator();
            return new Iterator(){

                public boolean hasNext() {
                    return it.hasNext();
                }

                public Object next() {
                    return it.next();
                }

                public void remove() {
                    throw new UnsupportedOperationException("remove() is not allowed due to the internal contraints");
                }
            };
        }
        return Collections.EMPTY_LIST.iterator();
    }

    public Object clone() {
        PropertyOptions newOptions;
        try {
            newOptions = new PropertyOptions(this.getOptions().getOptions());
        }
        catch (XMPException e) {
            newOptions = new PropertyOptions();
        }
        XMPNode newNode = new XMPNode(this.name, this.value, newOptions);
        this.cloneSubtree(newNode);
        return newNode;
    }

    public void cloneSubtree(XMPNode destination) {
        block4 : {
            try {
                Iterator it = this.iterateChildren();
                while (it.hasNext()) {
                    XMPNode child = (XMPNode)it.next();
                    destination.addChild((XMPNode)child.clone());
                }
                it = this.iterateQualifier();
                while (it.hasNext()) {
                    XMPNode qualifier = (XMPNode)it.next();
                    destination.addQualifier((XMPNode)qualifier.clone());
                }
            }
            catch (XMPException e) {
                if ($assertionsDisabled) break block4;
                throw new AssertionError();
            }
        }
    }

    public String dumpNode(boolean recursive) {
        StringBuffer result = new StringBuffer(512);
        this.dumpNode(result, recursive, 0, 0);
        return result.toString();
    }

    public int compareTo(Object xmpNode) {
        if (this.getOptions().isSchemaNode()) {
            return this.value.compareTo(((XMPNode)xmpNode).getValue());
        }
        return this.name.compareTo(((XMPNode)xmpNode).getName());
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getValue() {
        return this.value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public PropertyOptions getOptions() {
        if (this.options == null) {
            this.options = new PropertyOptions();
        }
        return this.options;
    }

    public void setOptions(PropertyOptions options) {
        this.options = options;
    }

    public boolean isImplicit() {
        return this.implicit;
    }

    public void setImplicit(boolean implicit) {
        this.implicit = implicit;
    }

    public boolean getHasAliases() {
        return this.hasAliases;
    }

    public void setHasAliases(boolean hasAliases) {
        this.hasAliases = hasAliases;
    }

    public boolean isAlias() {
        return this.alias;
    }

    public void setAlias(boolean alias) {
        this.alias = alias;
    }

    public boolean getHasValueChild() {
        return this.hasValueChild;
    }

    public void setHasValueChild(boolean hasValueChild) {
        this.hasValueChild = hasValueChild;
    }

    public void sort() {
        if (this.hasQualifier()) {
            int sortFrom;
            Object[] quals = this.getQualifier().toArray(new XMPNode[this.getQualifierLength()]);
            for (sortFrom = 0; quals.length > sortFrom && ("xml:lang".equals(quals[sortFrom].getName()) || "rdf:type".equals(quals[sortFrom].getName())); ++sortFrom) {
                quals[sortFrom].sort();
            }
            Arrays.sort(quals, sortFrom, quals.length);
            ListIterator<Object> it = this.qualifier.listIterator();
            for (int j = 0; j < quals.length; ++j) {
                it.next();
                it.set(quals[j]);
                quals[j].sort();
            }
        }
        if (this.hasChildren()) {
            if (!this.getOptions().isArray()) {
                Collections.sort(this.children);
            }
            Iterator it = this.iterateChildren();
            while (it.hasNext()) {
                ((XMPNode)it.next()).sort();
            }
        }
    }

    private void dumpNode(StringBuffer result, boolean recursive, int indent, int index) {
        int i;
        for (int i2 = 0; i2 < indent; ++i2) {
            result.append('\t');
        }
        if (this.parent != null) {
            if (this.getOptions().isQualifier()) {
                result.append('?');
                result.append(this.name);
            } else if (this.getParent().getOptions().isArray()) {
                result.append('[');
                result.append(index);
                result.append(']');
            } else {
                result.append(this.name);
            }
        } else {
            result.append("ROOT NODE");
            if (this.name != null && this.name.length() > 0) {
                result.append(" (");
                result.append(this.name);
                result.append(')');
            }
        }
        if (this.value != null && this.value.length() > 0) {
            result.append(" = \"");
            result.append(this.value);
            result.append('\"');
        }
        if (this.getOptions().containsOneOf(-1)) {
            result.append("\t(");
            result.append(this.getOptions().toString());
            result.append(" : ");
            result.append(this.getOptions().getOptionsString());
            result.append(')');
        }
        result.append('\n');
        if (recursive && this.hasQualifier()) {
            Object[] quals = this.getQualifier().toArray(new XMPNode[this.getQualifierLength()]);
            for (i = 0; quals.length > i && ("xml:lang".equals(quals[i].getName()) || "rdf:type".equals(quals[i].getName())); ++i) {
            }
            Arrays.sort(quals, i, quals.length);
            for (i = 0; i < quals.length; ++i) {
                Object qualifier = quals[i];
                XMPNode.super.dumpNode(result, recursive, indent + 2, i + 1);
            }
        }
        if (recursive && this.hasChildren()) {
            Object[] children = this.getChildren().toArray(new XMPNode[this.getChildrenLength()]);
            if (!this.getOptions().isArray()) {
                Arrays.sort(children);
            }
            for (i = 0; i < children.length; ++i) {
                Object child = children[i];
                XMPNode.super.dumpNode(result, recursive, indent + 1, i + 1);
            }
        }
    }

    private boolean isLanguageNode() {
        return "xml:lang".equals(this.name);
    }

    private boolean isTypeNode() {
        return "rdf:type".equals(this.name);
    }

    private List getChildren() {
        if (this.children == null) {
            this.children = new ArrayList(0);
        }
        return this.children;
    }

    public List getUnmodifiableChildren() {
        return Collections.unmodifiableList(new ArrayList(this.getChildren()));
    }

    private List getQualifier() {
        if (this.qualifier == null) {
            this.qualifier = new ArrayList(0);
        }
        return this.qualifier;
    }

    protected void setParent(XMPNode parent) {
        this.parent = parent;
    }

    private XMPNode find(List list, String expr) {
        if (list != null) {
            for (XMPNode child : list) {
                if (!child.getName().equals(expr)) continue;
                return child;
            }
        }
        return null;
    }

    private void assertChildNotExisting(String childName) throws XMPException {
        if (!"[]".equals(childName) && this.findChildByName(childName) != null) {
            throw new XMPException("Duplicate property or field node '" + childName + "'", 203);
        }
    }

    private void assertQualifierNotExisting(String qualifierName) throws XMPException {
        if (!"[]".equals(qualifierName) && this.findQualifierByName(qualifierName) != null) {
            throw new XMPException("Duplicate '" + qualifierName + "' qualifier", 203);
        }
    }

}