XMPSerializerRDF.java 26.5 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 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
/*
 * Decompiled with CFR 0_118.
 */
package com.adobe.internal.xmp.impl;

import com.adobe.internal.xmp.XMPException;
import com.adobe.internal.xmp.XMPMeta;
import com.adobe.internal.xmp.XMPMetaFactory;
import com.adobe.internal.xmp.impl.CountOutputStream;
import com.adobe.internal.xmp.impl.QName;
import com.adobe.internal.xmp.impl.Utils;
import com.adobe.internal.xmp.impl.XMPMetaImpl;
import com.adobe.internal.xmp.impl.XMPNode;
import com.adobe.internal.xmp.impl.XMPNodeUtils;
import com.adobe.internal.xmp.options.PropertyOptions;
import com.adobe.internal.xmp.options.SerializeOptions;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class XMPSerializerRDF {
    private static final int DEFAULT_PAD = 2048;
    private static final String PACKET_HEADER = "<?xpacket begin=\"\ufeff\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>";
    private static final String PACKET_TRAILER = "<?xpacket end=\"";
    private static final String PACKET_TRAILER2 = "\"?>";
    private static final String RDF_XMPMETA_START = "<x:xmpmeta xmlns:x=\"adobe:ns:meta/\" x:xmptk=\"";
    private static final String RDF_XMPMETA_END = "</x:xmpmeta>";
    private static final String RDF_RDF_START = "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">";
    private static final String RDF_RDF_END = "</rdf:RDF>";
    private static final String RDF_SCHEMA_START = "<rdf:Description rdf:about=";
    private static final String RDF_SCHEMA_END = "</rdf:Description>";
    private static final String RDF_STRUCT_START = "<rdf:Description";
    private static final String RDF_STRUCT_END = "</rdf:Description>";
    private static final String RDF_EMPTY_STRUCT = "<rdf:Description/>";
    static final Set RDF_ATTR_QUALIFIER = new HashSet<String>(Arrays.asList("xml:lang", "rdf:resource", "rdf:ID", "rdf:bagID", "rdf:nodeID"));
    private XMPMetaImpl xmp;
    private CountOutputStream outputStream;
    private OutputStreamWriter writer;
    private SerializeOptions options;
    private int unicodeSize = 1;
    private int padding;

    public void serialize(XMPMeta xmp, OutputStream out, SerializeOptions options) throws XMPException {
        try {
            this.outputStream = new CountOutputStream(out);
            this.writer = new OutputStreamWriter((OutputStream)this.outputStream, options.getEncoding());
            this.xmp = (XMPMetaImpl)xmp;
            this.options = options;
            this.padding = options.getPadding();
            this.writer = new OutputStreamWriter((OutputStream)this.outputStream, options.getEncoding());
            this.checkOptionsConsistence();
            String tailStr = this.serializeAsRDF();
            this.writer.flush();
            this.addPadding(tailStr.length());
            this.write(tailStr);
            this.writer.flush();
            this.outputStream.close();
        }
        catch (IOException e) {
            throw new XMPException("Error writing to the OutputStream", 0);
        }
    }

    private void addPadding(int tailLength) throws XMPException, IOException {
        if (this.options.getExactPacketLength()) {
            int minSize = this.outputStream.getBytesWritten() + tailLength * this.unicodeSize;
            if (minSize > this.padding) {
                throw new XMPException("Can't fit into specified packet size", 107);
            }
            this.padding -= minSize;
        }
        this.padding /= this.unicodeSize;
        int newlineLen = this.options.getNewline().length();
        if (this.padding >= newlineLen) {
            this.padding -= newlineLen;
            while (this.padding >= 100 + newlineLen) {
                this.writeChars(100, ' ');
                this.writeNewline();
                this.padding -= 100 + newlineLen;
            }
            this.writeChars(this.padding, ' ');
            this.writeNewline();
        } else {
            this.writeChars(this.padding, ' ');
        }
    }

    protected void checkOptionsConsistence() throws XMPException {
        if (this.options.getEncodeUTF16BE() | this.options.getEncodeUTF16LE()) {
            this.unicodeSize = 2;
        }
        if (this.options.getExactPacketLength()) {
            if (this.options.getOmitPacketWrapper() | this.options.getIncludeThumbnailPad()) {
                throw new XMPException("Inconsistent options for exact size serialize", 103);
            }
            if ((this.options.getPadding() & this.unicodeSize - 1) != 0) {
                throw new XMPException("Exact size must be a multiple of the Unicode element", 103);
            }
        } else if (this.options.getReadOnlyPacket()) {
            if (this.options.getOmitPacketWrapper() | this.options.getIncludeThumbnailPad()) {
                throw new XMPException("Inconsistent options for read-only packet", 103);
            }
            this.padding = 0;
        } else if (this.options.getOmitPacketWrapper()) {
            if (this.options.getIncludeThumbnailPad()) {
                throw new XMPException("Inconsistent options for non-packet serialize", 103);
            }
            this.padding = 0;
        } else {
            if (this.padding == 0) {
                this.padding = 2048 * this.unicodeSize;
            }
            if (this.options.getIncludeThumbnailPad() && !this.xmp.doesPropertyExist("http://ns.adobe.com/xap/1.0/", "Thumbnails")) {
                this.padding += 10000 * this.unicodeSize;
            }
        }
    }

    private String serializeAsRDF() throws IOException, XMPException {
        int level = 0;
        if (!this.options.getOmitPacketWrapper()) {
            this.writeIndent(level);
            this.write("<?xpacket begin=\"\ufeff\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>");
            this.writeNewline();
        }
        if (!this.options.getOmitXmpMetaElement()) {
            this.writeIndent(level);
            this.write("<x:xmpmeta xmlns:x=\"adobe:ns:meta/\" x:xmptk=\"");
            if (!this.options.getOmitVersionAttribute()) {
                this.write(XMPMetaFactory.getVersionInfo().getMessage());
            }
            this.write("\">");
            this.writeNewline();
            ++level;
        }
        this.writeIndent(level);
        this.write("<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">");
        this.writeNewline();
        if (this.options.getUseCanonicalFormat()) {
            this.serializeCanonicalRDFSchemas(level);
        } else {
            this.serializeCompactRDFSchemas(level);
        }
        this.writeIndent(level);
        this.write("</rdf:RDF>");
        this.writeNewline();
        if (!this.options.getOmitXmpMetaElement()) {
            this.writeIndent(--level);
            this.write("</x:xmpmeta>");
            this.writeNewline();
        }
        String tailStr = "";
        if (!this.options.getOmitPacketWrapper()) {
            for (level = this.options.getBaseIndent(); level > 0; --level) {
                tailStr = tailStr + this.options.getIndent();
            }
            tailStr = tailStr + "<?xpacket end=\"";
            tailStr = tailStr + (this.options.getReadOnlyPacket() ? 'r' : 'w');
            tailStr = tailStr + "\"?>";
        }
        return tailStr;
    }

    private void serializeCanonicalRDFSchemas(int level) throws IOException, XMPException {
        if (this.xmp.getRoot().getChildrenLength() > 0) {
            this.startOuterRDFDescription(this.xmp.getRoot(), level);
            Iterator it = this.xmp.getRoot().iterateChildren();
            while (it.hasNext()) {
                XMPNode currSchema = (XMPNode)it.next();
                this.serializeCanonicalRDFSchema(currSchema, level);
            }
            this.endOuterRDFDescription(level);
        } else {
            this.writeIndent(level + 1);
            this.write("<rdf:Description rdf:about=");
            this.writeTreeName();
            this.write("/>");
            this.writeNewline();
        }
    }

    private void writeTreeName() throws IOException {
        this.write(34);
        String name = this.xmp.getRoot().getName();
        if (name != null) {
            this.appendNodeValue(name, true);
        }
        this.write(34);
    }

    private void serializeCompactRDFSchemas(int level) throws IOException, XMPException {
        XMPNode schema;
        this.writeIndent(level + 1);
        this.write("<rdf:Description rdf:about=");
        this.writeTreeName();
        HashSet<String> usedPrefixes = new HashSet<String>();
        usedPrefixes.add("xml");
        usedPrefixes.add("rdf");
        Iterator it = this.xmp.getRoot().iterateChildren();
        while (it.hasNext()) {
            XMPNode schema2 = (XMPNode)it.next();
            this.declareUsedNamespaces(schema2, usedPrefixes, level + 3);
        }
        boolean allAreAttrs = true;
        Iterator it2 = this.xmp.getRoot().iterateChildren();
        while (it2.hasNext()) {
            schema = (XMPNode)it2.next();
            allAreAttrs &= this.serializeCompactRDFAttrProps(schema, level + 2);
        }
        if (allAreAttrs) {
            this.write("/>");
            this.writeNewline();
            return;
        }
        this.write(62);
        this.writeNewline();
        it2 = this.xmp.getRoot().iterateChildren();
        while (it2.hasNext()) {
            schema = (XMPNode)it2.next();
            this.serializeCompactRDFElementProps(schema, level + 2);
        }
        this.writeIndent(level + 1);
        this.write("</rdf:Description>");
        this.writeNewline();
    }

    private boolean serializeCompactRDFAttrProps(XMPNode parentNode, int indent) throws IOException {
        boolean allAreAttrs = true;
        Iterator it = parentNode.iterateChildren();
        while (it.hasNext()) {
            XMPNode prop = (XMPNode)it.next();
            if (this.canBeRDFAttrProp(prop)) {
                this.writeNewline();
                this.writeIndent(indent);
                this.write(prop.getName());
                this.write("=\"");
                this.appendNodeValue(prop.getValue(), true);
                this.write(34);
                continue;
            }
            allAreAttrs = false;
        }
        return allAreAttrs;
    }

    private void serializeCompactRDFElementProps(XMPNode parentNode, int indent) throws IOException, XMPException {
        Iterator it = parentNode.iterateChildren();
        while (it.hasNext()) {
            XMPNode node = (XMPNode)it.next();
            if (this.canBeRDFAttrProp(node)) continue;
            boolean emitEndTag = true;
            boolean indentEndTag = true;
            String elemName = node.getName();
            if ("[]".equals(elemName)) {
                elemName = "rdf:li";
            }
            this.writeIndent(indent);
            this.write(60);
            this.write(elemName);
            boolean hasGeneralQualifiers = false;
            boolean hasRDFResourceQual = false;
            Iterator iq = node.iterateQualifier();
            while (iq.hasNext()) {
                XMPNode qualifier = (XMPNode)iq.next();
                if (!RDF_ATTR_QUALIFIER.contains(qualifier.getName())) {
                    hasGeneralQualifiers = true;
                    continue;
                }
                hasRDFResourceQual = "rdf:resource".equals(qualifier.getName());
                this.write(32);
                this.write(qualifier.getName());
                this.write("=\"");
                this.appendNodeValue(qualifier.getValue(), true);
                this.write(34);
            }
            if (hasGeneralQualifiers) {
                this.serializeCompactRDFGeneralQualifier(indent, node);
            } else if (!node.getOptions().isCompositeProperty()) {
                Object[] result = this.serializeCompactRDFSimpleProp(node);
                emitEndTag = (Boolean)result[0];
                indentEndTag = (Boolean)result[1];
            } else if (node.getOptions().isArray()) {
                this.serializeCompactRDFArrayProp(node, indent);
            } else {
                emitEndTag = this.serializeCompactRDFStructProp(node, indent, hasRDFResourceQual);
            }
            if (!emitEndTag) continue;
            if (indentEndTag) {
                this.writeIndent(indent);
            }
            this.write("</");
            this.write(elemName);
            this.write(62);
            this.writeNewline();
        }
    }

    private Object[] serializeCompactRDFSimpleProp(XMPNode node) throws IOException {
        Boolean emitEndTag = Boolean.TRUE;
        Boolean indentEndTag = Boolean.TRUE;
        if (node.getOptions().isURI()) {
            this.write(" rdf:resource=\"");
            this.appendNodeValue(node.getValue(), true);
            this.write("\"/>");
            this.writeNewline();
            emitEndTag = Boolean.FALSE;
        } else if (node.getValue() == null || node.getValue().length() == 0) {
            this.write("/>");
            this.writeNewline();
            emitEndTag = Boolean.FALSE;
        } else {
            this.write(62);
            this.appendNodeValue(node.getValue(), false);
            indentEndTag = Boolean.FALSE;
        }
        return new Object[]{emitEndTag, indentEndTag};
    }

    private void serializeCompactRDFArrayProp(XMPNode node, int indent) throws IOException, XMPException {
        this.write(62);
        this.writeNewline();
        this.emitRDFArrayTag(node, true, indent + 1);
        if (node.getOptions().isArrayAltText()) {
            XMPNodeUtils.normalizeLangArray(node);
        }
        this.serializeCompactRDFElementProps(node, indent + 2);
        this.emitRDFArrayTag(node, false, indent + 1);
    }

    private boolean serializeCompactRDFStructProp(XMPNode node, int indent, boolean hasRDFResourceQual) throws XMPException, IOException {
        boolean hasAttrFields = false;
        boolean hasElemFields = false;
        boolean emitEndTag = true;
        Iterator ic = node.iterateChildren();
        while (ic.hasNext()) {
            XMPNode field = (XMPNode)ic.next();
            if (this.canBeRDFAttrProp(field)) {
                hasAttrFields = true;
            } else {
                hasElemFields = true;
            }
            if (!hasAttrFields || !hasElemFields) continue;
            break;
        }
        if (hasRDFResourceQual && hasElemFields) {
            throw new XMPException("Can't mix rdf:resource qualifier and element fields", 202);
        }
        if (!node.hasChildren()) {
            this.write(" rdf:parseType=\"Resource\"/>");
            this.writeNewline();
            emitEndTag = false;
        } else if (!hasElemFields) {
            this.serializeCompactRDFAttrProps(node, indent + 1);
            this.write("/>");
            this.writeNewline();
            emitEndTag = false;
        } else if (!hasAttrFields) {
            this.write(" rdf:parseType=\"Resource\">");
            this.writeNewline();
            this.serializeCompactRDFElementProps(node, indent + 1);
        } else {
            this.write(62);
            this.writeNewline();
            this.writeIndent(indent + 1);
            this.write("<rdf:Description");
            this.serializeCompactRDFAttrProps(node, indent + 2);
            this.write(">");
            this.writeNewline();
            this.serializeCompactRDFElementProps(node, indent + 1);
            this.writeIndent(indent + 1);
            this.write("</rdf:Description>");
            this.writeNewline();
        }
        return emitEndTag;
    }

    private void serializeCompactRDFGeneralQualifier(int indent, XMPNode node) throws IOException, XMPException {
        this.write(" rdf:parseType=\"Resource\">");
        this.writeNewline();
        this.serializeCanonicalRDFProperty(node, false, true, indent + 1);
        Iterator iq = node.iterateQualifier();
        while (iq.hasNext()) {
            XMPNode qualifier = (XMPNode)iq.next();
            this.serializeCanonicalRDFProperty(qualifier, false, false, indent + 1);
        }
    }

    private void serializeCanonicalRDFSchema(XMPNode schemaNode, int level) throws IOException, XMPException {
        Iterator it = schemaNode.iterateChildren();
        while (it.hasNext()) {
            XMPNode propNode = (XMPNode)it.next();
            this.serializeCanonicalRDFProperty(propNode, this.options.getUseCanonicalFormat(), false, level + 2);
        }
    }

    private void declareUsedNamespaces(XMPNode node, Set usedPrefixes, int indent) throws IOException {
        Iterator it;
        if (node.getOptions().isSchemaNode()) {
            String prefix = node.getValue().substring(0, node.getValue().length() - 1);
            this.declareNamespace(prefix, node.getName(), usedPrefixes, indent);
        } else if (node.getOptions().isStruct()) {
            it = node.iterateChildren();
            while (it.hasNext()) {
                XMPNode field = (XMPNode)it.next();
                this.declareNamespace(field.getName(), null, usedPrefixes, indent);
            }
        }
        it = node.iterateChildren();
        while (it.hasNext()) {
            XMPNode child = (XMPNode)it.next();
            this.declareUsedNamespaces(child, usedPrefixes, indent);
        }
        it = node.iterateQualifier();
        while (it.hasNext()) {
            XMPNode qualifier = (XMPNode)it.next();
            this.declareNamespace(qualifier.getName(), null, usedPrefixes, indent);
            this.declareUsedNamespaces(qualifier, usedPrefixes, indent);
        }
    }

    private void declareNamespace(String prefix, String namespace, Set usedPrefixes, int indent) throws IOException {
        if (namespace == null) {
            QName qname = new QName(prefix);
            if (qname.hasPrefix()) {
                prefix = qname.getPrefix();
                namespace = XMPMetaFactory.getSchemaRegistry().getNamespaceURI(prefix + ":");
                this.declareNamespace(prefix, namespace, usedPrefixes, indent);
            } else {
                return;
            }
        }
        if (!usedPrefixes.contains(prefix)) {
            this.writeNewline();
            this.writeIndent(indent);
            this.write("xmlns:");
            this.write(prefix);
            this.write("=\"");
            this.write(namespace);
            this.write(34);
            usedPrefixes.add(prefix);
        }
    }

    private void startOuterRDFDescription(XMPNode schemaNode, int level) throws IOException {
        this.writeIndent(level + 1);
        this.write("<rdf:Description rdf:about=");
        this.writeTreeName();
        HashSet<String> usedPrefixes = new HashSet<String>();
        usedPrefixes.add("xml");
        usedPrefixes.add("rdf");
        this.declareUsedNamespaces(schemaNode, usedPrefixes, level + 3);
        this.write(62);
        this.writeNewline();
    }

    private void endOuterRDFDescription(int level) throws IOException {
        this.writeIndent(level + 1);
        this.write("</rdf:Description>");
        this.writeNewline();
    }

    private void serializeCanonicalRDFProperty(XMPNode node, boolean useCanonicalRDF, boolean emitAsRDFValue, int indent) throws IOException, XMPException {
        XMPNode child;
        XMPNode qualifier;
        boolean emitEndTag = true;
        boolean indentEndTag = true;
        String elemName = node.getName();
        if (emitAsRDFValue) {
            elemName = "rdf:value";
        } else if ("[]".equals(elemName)) {
            elemName = "rdf:li";
        }
        this.writeIndent(indent);
        this.write(60);
        this.write(elemName);
        boolean hasGeneralQualifiers = false;
        boolean hasRDFResourceQual = false;
        Iterator it = node.iterateQualifier();
        while (it.hasNext()) {
            qualifier = (XMPNode)it.next();
            if (!RDF_ATTR_QUALIFIER.contains(qualifier.getName())) {
                hasGeneralQualifiers = true;
                continue;
            }
            hasRDFResourceQual = "rdf:resource".equals(qualifier.getName());
            if (emitAsRDFValue) continue;
            this.write(32);
            this.write(qualifier.getName());
            this.write("=\"");
            this.appendNodeValue(qualifier.getValue(), true);
            this.write(34);
        }
        if (hasGeneralQualifiers && !emitAsRDFValue) {
            if (hasRDFResourceQual) {
                throw new XMPException("Can't mix rdf:resource and general qualifiers", 202);
            }
            if (useCanonicalRDF) {
                this.write(">");
                this.writeNewline();
                this.writeIndent(++indent);
                this.write("<rdf:Description");
                this.write(">");
            } else {
                this.write(" rdf:parseType=\"Resource\">");
            }
            this.writeNewline();
            this.serializeCanonicalRDFProperty(node, useCanonicalRDF, true, indent + 1);
            it = node.iterateQualifier();
            while (it.hasNext()) {
                qualifier = (XMPNode)it.next();
                if (RDF_ATTR_QUALIFIER.contains(qualifier.getName())) continue;
                this.serializeCanonicalRDFProperty(qualifier, useCanonicalRDF, false, indent + 1);
            }
            if (useCanonicalRDF) {
                this.writeIndent(indent);
                this.write("</rdf:Description>");
                this.writeNewline();
                --indent;
            }
        } else if (!node.getOptions().isCompositeProperty()) {
            if (node.getOptions().isURI()) {
                this.write(" rdf:resource=\"");
                this.appendNodeValue(node.getValue(), true);
                this.write("\"/>");
                this.writeNewline();
                emitEndTag = false;
            } else if (node.getValue() == null || "".equals(node.getValue())) {
                this.write("/>");
                this.writeNewline();
                emitEndTag = false;
            } else {
                this.write(62);
                this.appendNodeValue(node.getValue(), false);
                indentEndTag = false;
            }
        } else if (node.getOptions().isArray()) {
            this.write(62);
            this.writeNewline();
            this.emitRDFArrayTag(node, true, indent + 1);
            if (node.getOptions().isArrayAltText()) {
                XMPNodeUtils.normalizeLangArray(node);
            }
            it = node.iterateChildren();
            while (it.hasNext()) {
                child = (XMPNode)it.next();
                this.serializeCanonicalRDFProperty(child, useCanonicalRDF, false, indent + 2);
            }
            this.emitRDFArrayTag(node, false, indent + 1);
        } else if (!hasRDFResourceQual) {
            if (!node.hasChildren()) {
                if (useCanonicalRDF) {
                    this.write(">");
                    this.writeNewline();
                    this.writeIndent(indent + 1);
                    this.write("<rdf:Description/>");
                } else {
                    this.write(" rdf:parseType=\"Resource\"/>");
                    emitEndTag = false;
                }
                this.writeNewline();
            } else {
                if (useCanonicalRDF) {
                    this.write(">");
                    this.writeNewline();
                    this.writeIndent(++indent);
                    this.write("<rdf:Description");
                    this.write(">");
                } else {
                    this.write(" rdf:parseType=\"Resource\">");
                }
                this.writeNewline();
                it = node.iterateChildren();
                while (it.hasNext()) {
                    child = (XMPNode)it.next();
                    this.serializeCanonicalRDFProperty(child, useCanonicalRDF, false, indent + 1);
                }
                if (useCanonicalRDF) {
                    this.writeIndent(indent);
                    this.write("</rdf:Description>");
                    this.writeNewline();
                    --indent;
                }
            }
        } else {
            it = node.iterateChildren();
            while (it.hasNext()) {
                child = (XMPNode)it.next();
                if (!this.canBeRDFAttrProp(child)) {
                    throw new XMPException("Can't mix rdf:resource and complex fields", 202);
                }
                this.writeNewline();
                this.writeIndent(indent + 1);
                this.write(32);
                this.write(child.getName());
                this.write("=\"");
                this.appendNodeValue(child.getValue(), true);
                this.write(34);
            }
            this.write("/>");
            this.writeNewline();
            emitEndTag = false;
        }
        if (emitEndTag) {
            if (indentEndTag) {
                this.writeIndent(indent);
            }
            this.write("</");
            this.write(elemName);
            this.write(62);
            this.writeNewline();
        }
    }

    private void emitRDFArrayTag(XMPNode arrayNode, boolean isStartTag, int indent) throws IOException {
        if (isStartTag || arrayNode.hasChildren()) {
            this.writeIndent(indent);
            this.write(isStartTag ? "<rdf:" : "</rdf:");
            if (arrayNode.getOptions().isArrayAlternate()) {
                this.write("Alt");
            } else if (arrayNode.getOptions().isArrayOrdered()) {
                this.write("Seq");
            } else {
                this.write("Bag");
            }
            if (isStartTag && !arrayNode.hasChildren()) {
                this.write("/>");
            } else {
                this.write(">");
            }
            this.writeNewline();
        }
    }

    private void appendNodeValue(String value, boolean forAttribute) throws IOException {
        if (value == null) {
            value = "";
        }
        this.write(Utils.escapeXML(value, forAttribute, true));
    }

    private boolean canBeRDFAttrProp(XMPNode node) {
        return !node.hasQualifier() && !node.getOptions().isURI() && !node.getOptions().isCompositeProperty() && !"[]".equals(node.getName());
    }

    private void writeIndent(int times) throws IOException {
        for (int i = this.options.getBaseIndent() + times; i > 0; --i) {
            this.writer.write(this.options.getIndent());
        }
    }

    private void write(int c) throws IOException {
        this.writer.write(c);
    }

    private void write(String str) throws IOException {
        this.writer.write(str);
    }

    private void writeChars(int number, char c) throws IOException {
        while (number > 0) {
            this.writer.write(c);
            --number;
        }
    }

    private void writeNewline() throws IOException {
        this.writer.write(this.options.getNewline());
    }
}