Debian Codegraph @

src/com/adobe/internal/xmp/impl/XMPSerializerPlain.java

raw symbols SCIP
1 // The Plain XMP format is disabled
2 //
3 // =================================================================================================
4 // ADOBE SYSTEMS INCORPORATED
5 // Copyright 2006 Adobe Systems Incorporated
6 // All Rights Reserved
7 //
8 // NOTICE: Adobe permits you to use, modify, and distribute this file in accordance with the terms
9 // of the Adobe license agreement accompanying it.
10 // =================================================================================================
11 //
12 //package com.adobe.internal.xmp.impl;
13 //
14 //import java.io.IOException;
15 //import java.io.OutputStream;
16 //import java.util.ArrayList;
17 //import java.util.Arrays;
18 //import java.util.Collections;
19 //import java.util.Iterator;
20 //import java.util.List;
21 //
22 //import com.adobe.internal.xmp.XMPConst;
23 //import com.adobe.internal.xmp.XMPError;
24 //import com.adobe.internal.xmp.XMPException;
25 //import com.adobe.internal.xmp.XMPMetaFactory;
26 //import com.adobe.internal.xmp.options.PropertyOptions;
27 //import com.adobe.internal.xmp.options.SerializeOptions;
28 //import com.adobe.internal.xmp.utils.XMLStreamWriterFactory;
29 //import com.adobe.internal.xmp.utils.XMLStreamWriterImpl;
30 //
31 //
32 ///**
33 // * Serializes the <code>XMPMeta</code>-object using the Plain RDF serialization format.
34 // * The output is written to an <code>OutputStream</code>
35 // * according to the <code>SerializeOptions</code>.
36 // *
37 // * @author Stefan Makswit
38 // * @version $Revision$
39 // * @since 07.11.2006
40 // */
41 //public class XMPSerializerPlain
42 //{
43 // /** default padding */
44 // private static final int DEFAULT_PAD = 2048;
45 // /** */
46 // private static final String PACKET_HEADER =
47 // "xpacket begin=\"\uFEFF\" id=\"W5M0MpCehiHzreSzNTczkc9d\"";
48 // /** The w/r is missing inbetween */
49 // private static final String PACKET_TRAILER = "xpacket end=\"";
50 // /** */
51 // private static final String PACKET_TRAILER2 = "\"";
52 //
53 // /** the XMLStreamWriter */
54 // private XMLStreamWriterImpl writer;
55 // /** the metadata object to be serialized. */
56 // private XMPMetaImpl xmp;
57 // /** the size of one unicode char, for UTF-8 set to 1
58 // * (Note: only valid for ASCII chars lower than 0x80),
59 // * set to 2 in case of UTF-16 */
60 // private int unicodeSize = 1; // UTF-8
61 // /** the padding in the XMP Packet, or the length of the complete packet in
62 // * case of option <em>exactPacketLength</em>. */
63 // private int padding;
64 // /** the stored serialisation options */
65 // private SerializeOptions options;
66 // /** the output stream to serialize to */
67 // private CountOutputStream outputStream;
68 //
69 //
70 // /**
71 // * The actual serialisation.
72 // *
73 // * @param xmp the metadata object to be serialized
74 // * @param out outputStream the output stream to serialize to
75 // * @param options the serialization options
76 // *
77 // * @throws XMPException If case of wrong options or any other serialisaton error.
78 // */
79 // public void serialize(XMPMetaImpl xmp, OutputStream out, SerializeOptions options)
80 // throws XMPException
81 // {
82 // try
83 // {
84 // this.xmp = xmp;
85 // this.options = options;
86 // this.padding = options.getPadding();
87 //
88 // outputStream = new CountOutputStream(out);
89 // // need to add serialize options
90 // writer = XMLStreamWriterFactory.create(outputStream, options);
91 //
92 // checkOptionsConsistence();
93 //
94 // // Write the packet header PI.
95 // if (!options.getOmitPacketWrapper())
96 // {
97 // writer.writeProcessingInstruction(PACKET_HEADER);
98 // }
99 //
100 // // serializes the whole packet without the ending PI
101 // serializeAsPlain();
102 // writer.flush();
103 //
104 // // Prepares the packet trailer PI.
105 // String tailStr = "";
106 // if (!options.getOmitPacketWrapper())
107 // {
108 // tailStr += PACKET_TRAILER;
109 // tailStr += options.getReadOnlyPacket() ? 'r' : 'w';
110 // tailStr += PACKET_TRAILER2;
111 //
112 // writer.setEscapeWhitespaces(false);
113 //
114 // // adds padding according to length of tail string (UTF-8, one byte per char)
115 // addPadding(tailStr.length() + 4 + // +4 for <? ... ?>
116 // options.getBaseIndent() * options.getIndent().length() +
117 // options.getNewline().length());
118 //
119 // // writes the tail
120 // writer.writeProcessingInstruction(tailStr);
121 // }
122 // writer.close();
123 // }
124 // catch (IOException e)
125 // {
126 // throw new XMPException(e.getMessage(), XMPError.INTERNALFAILURE);
127 // }
128 // }
129 //
130 //
131 // /**
132 // * Does the main serialisaton of the metadata object.
133 // * @throws IOException Forwards stream writer exceptions
134 // */
135 // private void serializeAsPlain() throws IOException
136 // {
137 // // Write the pxmp:XMP_Packet start tag, optitional name and version string.
138 // writer.writeStartElement("pxmp:XMP_Packet");
139 // writeNameAndVersion();
140 //
141 // declareUsedNamespaces (xmp.getRoot());
142 //
143 // // Write all of the properties.
144 // for (Iterator it = xmp.getRoot().iterateChildren(); it.hasNext(); )
145 // {
146 // XMPNode schema = (XMPNode) it.next();
147 // serializePlainXMPSchema(schema);
148 // }
149 //
150 // // Write the pxmp:XMP_Packet end tag.
151 // writer.writeEndElement();
152 // }
153 //
154 //
155 // /**
156 // * Writes the metadata object name, the toolkit version and the rdf namespace.
157 // * @throws IOException Forwards writer exceptions
158 // */
159 // private void writeNameAndVersion() throws IOException
160 // {
161 // if (xmp.getObjectName().length() > 0)
162 // {
163 // writer.writeAttribute("pxmp:about", xmp.getObjectName());
164 // }
165 //
166 // if (!options.getOmitVersionAttribute())
167 // {
168 // writer.writeAttribute("pxmp:xmptk", XMPMetaFactory.getVersionInfo().getMessage());
169 // }
170 // else
171 // {
172 // writer.writeAttribute("pxmp:xmptk", "");
173 // }
174 // writer.writeNamespace("pxmp", XMPConst.NS_PXMP); // pxmp-namespace first
175 // }
176 //
177 //
178 // /**
179 // * Serializes one xmp schema with all of its properties.
180 // * @param schema the schema to serialize
181 // * @throws IOException Forwards writer exceptions
182 // */
183 // private void serializePlainXMPSchema(XMPNode schema) throws IOException
184 // {
185 // // Write the pxmp:SchemaGroup start tag.
186 // writer.writeStartElement(schema.getValue() + "XMP_SchemaGroup");
187 //
188 // // Write each of the schema's actual properties.
189 // for (Iterator it = schema.iterateChildren(); it.hasNext();)
190 // {
191 // XMPNode propNode = (XMPNode) it.next();
192 // serializePlainXMPProperty(propNode, options.getBaseIndent() + 1);
193 // }
194 //
195 // // Write the pxmp:SchemaGroup end tag.
196 // writer.writeEndElement();
197 // }
198 //
199 //
200 //
201 // /**
202 // * Serialize any property or qualifier recursively in plain XMP format.
203 // *
204 // * @param propNode the current property node
205 // * @param indent the current indent
206 // * @throws IOException Forward output exceptions
207 // */
208 // private void serializePlainXMPProperty(XMPNode propNode, int indent) throws IOException
209 // {
210 // PropertyOptions propOpts = propNode.getOptions();
211 // boolean isSimple = propOpts.isSimple();
212 // boolean isCompact = propOpts.isCompact();
213 //
214 // String propName = propNode.getName();
215 // if (XMPConst.ARRAY_ITEM_NAME.equals(propName))
216 // {
217 // // rename "[]" to "item" for array items
218 // propName = "item";
219 // }
220 //
221 // // Write the property name as the beginning of an element start tag.
222 // writer.writeStartElement(propName);
223 //
224 // // Write kind attribute and simple qualifiers.
225 // writeKindAttribute(propNode);
226 // List remainingQuals = writeSimpleQualifier(propNode);
227 //
228 // // Write value or compact property as attribute or compound properties recursively.
229 // if (isSimple)
230 // {
231 // writer.writeAttribute("value", propNode.getValue());
232 // }
233 // else if (isCompact)
234 // {
235 // writeCompactProperty(propNode);
236 // }
237 // else
238 // {
239 // // Write the element children for struct fields and array items.
240 // for (Iterator it = propNode.iterateChildren(); it.hasNext();)
241 // {
242 // XMPNode child = (XMPNode) it.next();
243 // serializePlainXMPProperty(child, indent + 1);
244 // }
245 // }
246 //
247 // writeCompoundQualifier(remainingQuals, propOpts, indent);
248 //
249 // // Close the start tag or write an end tag, if needed.
250 // writer.writeEndElement();
251 // }
252 //
253 //
254 // /**
255 // * Write kind-attribute for tag if needed.
256 // *
257 // * @param propNode a property or qualifier node.
258 // * @throws IOException Forwards the writer exceptions.
259 // */
260 // private void writeKindAttribute(XMPNode propNode) throws IOException
261 // {
262 // PropertyOptions opts = propNode.getOptions();
263 // if (opts.isSimple() && opts.isQualifier())
264 // {
265 // writer.writeAttribute("kind", "qual");
266 // }
267 // else if (!opts.isSimple())
268 // {
269 // String kindStr = opts.isCompact() ? "c-" : "";
270 //
271 // if (opts.isStruct())
272 // {
273 // kindStr += "struct";
274 // }
275 // else
276 // {
277 // if (opts.isArrayAlternate())
278 // {
279 // kindStr += "alt";
280 // if (opts.isArrayAltText())
281 // {
282 // XMPNodeUtils.normalizeLangArray(propNode);
283 // }
284 // }
285 // else if (opts.isArrayOrdered())
286 // {
287 // kindStr += "seq";
288 // }
289 // else
290 // {
291 // kindStr += "bag";
292 // }
293 //
294 // }
295 //
296 // if (opts.isQualifier())
297 // {
298 // kindStr += "-qual";
299 // }
300 //
301 // // write the kind attribute
302 // writer.writeAttribute("kind", kindStr);
303 // }
304 // }
305 //
306 //
307 // /**
308 // * Writes a compact property as attribute(s)
309 // * @param propNode a property node
310 // * @throws IOException Forwards the writer exceptions.
311 // */
312 // private void writeCompactProperty(XMPNode propNode) throws IOException
313 // {
314 // if (propNode.getOptions().isStruct())
315 // {
316 // for (Iterator it = propNode.iterateChildren(); it.hasNext();)
317 // {
318 // XMPNode child = (XMPNode) it.next();
319 // assert !child.getOptions().isCompositeProperty() &&
320 // !child.getOptions().getHasQualifiers();
321 // writer.writeAttribute(child.getName(), child.getValue());
322 // }
323 //
324 // }
325 // else
326 // {
327 // assert propNode.getOptions().isArray();
328 //
329 // // Pick the separator. Defaults to " ", use "; " if any item
330 // // value is empty or has XML whitespace.
331 // String separator = " ";
332 // boolean whiteSpaceFound = false;
333 //
334 // for (Iterator it = propNode.iterateChildren(); it.hasNext();)
335 // {
336 // XMPNode child = (XMPNode) it.next();
337 // if (child.getValue() == null || child.getValue().length() == 0)
338 // {
339 // separator = "; ";
340 // break;
341 // }
342 //
343 // for (int i = 0; i < child.getValue().length(); i++)
344 // {
345 // char c = child.getValue().charAt(i);
346 // if (c <= ' ' && c != 0x09 && c != 0x0A && c != 0x0D)
347 // {
348 // whiteSpaceFound = true;
349 // break;
350 // }
351 // }
352 //
353 // if (whiteSpaceFound)
354 // {
355 // separator = "; ";
356 // break;
357 // }
358 // }
359 //
360 // // Catenate the item values and write the value attribute.
361 //
362 // StringBuffer arrayValue = new StringBuffer();
363 //
364 // if (!whiteSpaceFound)
365 // {
366 // for (Iterator it = propNode.iterateChildren(); it.hasNext();)
367 // {
368 // XMPNode child = (XMPNode) it.next();
369 // assert !child.getOptions().isCompositeProperty() &&
370 // !child.getOptions().getHasQualifiers();
371 // arrayValue.append(child.getValue());
372 // if (it.hasNext())
373 // {
374 // arrayValue.append(separator);
375 // }
376 // }
377 // }
378 // else
379 // {
380 // writer.writeAttribute("sep", "; ");
381 //
382 // for (Iterator it = propNode.iterateChildren(); it.hasNext();)
383 // {
384 // XMPNode child = (XMPNode) it.next();
385 // assert !child.getOptions().isCompositeProperty() &&
386 // !child.hasQualifier();
387 //
388 // String value = propNode.getValue();
389 // int colonPos = 0;
390 // for (int i = 0; i < value.length(); i++)
391 // {
392 // char ch = value.charAt(i);
393 // if (ch == ';')
394 // {
395 // arrayValue.append(value.substring(colonPos, i));
396 // arrayValue.append(';'); // append double semicolon
397 // colonPos = i;
398 // }
399 // }
400 // arrayValue.append(value.substring(colonPos, value.length()));
401 //
402 // if (it.hasNext())
403 // {
404 // arrayValue.append(separator);
405 // }
406 // }
407 // }
408 //
409 // writer.writeAttribute("value", arrayValue.toString());
410 // }
411 // }
412 //
413 //
414 //
415 //
416 // /**
417 // * Write the simple qualifiers.<br>
418 // * <em>Note:</em> Simple qualifiers on a compact struct are contained elements, not attributes.
419 // *
420 // * @param propNode a property node
421 // * @return Returns the remaining, compound qualifier.
422 // * @throws IOException Forwards the writer exceptions.
423 // */
424 // private List writeSimpleQualifier(XMPNode propNode) throws IOException
425 // {
426 // PropertyOptions opts = propNode.getOptions();
427 //
428 // List remainingQuals = null;
429 // if (!opts.isCompact() && !opts.isStruct())
430 // {
431 // for (Iterator it = propNode.iterateQualifier(); it.hasNext();)
432 // {
433 // XMPNode qualifier = (XMPNode) it.next();
434 // if (!qualifier.getOptions().isCompositeProperty() &&
435 // !qualifier.getOptions().getHasQualifiers())
436 // {
437 // writer.writeAttribute(qualifier.getName(), qualifier.getValue());
438 // }
439 // else
440 // {
441 // if (remainingQuals == null)
442 // {
443 // remainingQuals = new ArrayList();
444 // }
445 // remainingQuals.add(qualifier);
446 // }
447 // }
448 // }
449 // return remainingQuals;
450 // }
451 //
452 //
453 // /**
454 // * Write the compound qualifiers. Sort them if the property is a regular
455 // * (non-compact) array. It is OK to sort all of the qualifiers. We've
456 // * already output the simple ones, so their order is fixed.
457 // * <p>
458 // * <em>Note:</em> Don't sort the real qualifier vector, sort a copy. Need
459 // * to keep "xml:lang" and "rdf:type" in front!
460 // * <p>
461 // * <em>Note:</em> This includes the simple qualifiers for a compact
462 // * struct.
463 // *
464 // * @param remainingQuals a list of remaining qualifiers that could not be written as attribute.
465 // * @param propOpts the parent property options
466 // * @param indent the current indent
467 // * @throws IOException Forwards the writer exceptions.
468 // */
469 // private void writeCompoundQualifier(List remainingQuals, PropertyOptions propOpts, int indent)
470 // throws IOException
471 // {
472 //
473 // if (remainingQuals != null)
474 // {
475 // if (!propOpts.isArray() && !propOpts.isCompact())
476 // {
477 // Collections.sort(remainingQuals);
478 // }
479 //
480 // for (Iterator it = remainingQuals.iterator(); it.hasNext();)
481 // {
482 // XMPNode qualifier = (XMPNode) it.next();
483 // serializePlainXMPProperty(qualifier, indent + 1);
484 //
485 //// <#AdobePrivate>
486 //// if ((isCompact && isStruct) ||
487 //// qualifier.getOptions().isCompositeProperty() ||
488 //// qualifier.getOptions().hasQualifiers())
489 //// </#AdobePrivate>
490 // }
491 //
492 // }
493 // }
494 //
495 // /**
496 // * Checks if the supplied options are consistent.
497 // * @throws XMPException Thrown if options are conflicting
498 // */
499 // protected void checkOptionsConsistence() throws XMPException
500 // {
501 // if (options.getEncodeUTF16BE() | options.getEncodeUTF16LE())
502 // {
503 // unicodeSize = 2;
504 // }
505 //
506 // if (options.getExactPacketLength())
507 // {
508 // if (options.getOmitPacketWrapper() | options.getIncludeThumbnailPad())
509 // {
510 // throw new XMPException("Inconsistent options for exact size serialize",
511 // XMPError.BADOPTIONS);
512 // }
513 // if ((options.getPadding() & (unicodeSize - 1)) != 0)
514 // {
515 // throw new XMPException("Exact size must be a multiple of the Unicode element",
516 // XMPError.BADOPTIONS);
517 // }
518 // }
519 // else if (options.getReadOnlyPacket())
520 // {
521 // if (options.getOmitPacketWrapper() | options.getIncludeThumbnailPad())
522 // {
523 // throw new XMPException("Inconsistent options for read-only packet",
524 // XMPError.BADOPTIONS);
525 // }
526 // padding = 0;
527 // }
528 // else if (options.getOmitPacketWrapper())
529 // {
530 // if (options.getIncludeThumbnailPad())
531 // {
532 // throw new XMPException("Inconsistent options for non-packet serialize",
533 // XMPError.BADOPTIONS);
534 // }
535 // padding = 0;
536 // }
537 // else
538 // {
539 // if (padding == 0)
540 // {
541 // padding = DEFAULT_PAD * unicodeSize;
542 // }
543 //
544 // if (options.getIncludeThumbnailPad())
545 // {
546 // if (!xmp.doesPropertyExist(XMPConst.NS_XMP, "Thumbnails"))
547 // {
548 // padding += 10000 * unicodeSize;
549 // }
550 // }
551 // }
552 // }
553 //
554 //
555 // /**
556 // * Writes all used namespaces of the subtree in node to the output.
557 // * The subtree is recursivly traversed.
558 // * @param node the root node of the subtree
559 // * @throws IOException Forwards all writer exceptions.
560 // */
561 // private void declareUsedNamespaces(XMPNode node)
562 // throws IOException
563 // {
564 // if (node.getOptions().isSchemaNode())
565 // {
566 // // The schema node name is the URI, the value is the prefix.
567 // String prefix = node.getValue().substring(0, node.getValue().length() - 1);
568 // String namespace = node.getName();
569 //// writeIndent(1);
570 // writer.writeNamespace(prefix, namespace);
571 // }
572 // // not the root node and direct schema children
573 // else if (node.getParent() != null &&
574 // !node.getParent().getOptions().isSchemaNode())
575 // {
576 // QName qname = new QName(node.getName());
577 // String ns = XMPMetaFactory.getSchemaRegistry().getNamespaceURI(qname.getPrefix());
578 // writer.writeNamespace(qname.getPrefix(), ns);
579 // }
580 //
581 // // iterate children except array items
582 // if (!node.getOptions().isArray())
583 // {
584 // for (Iterator it = node.iterateChildren(); it.hasNext();)
585 // {
586 // XMPNode child = (XMPNode) it.next();
587 // declareUsedNamespaces(child);
588 // }
589 // }
590 //
591 // for (Iterator it = node.iterateQualifier(); it.hasNext();)
592 // {
593 // XMPNode qualifier = (XMPNode) it.next();
594 // declareUsedNamespaces(qualifier);
595 // }
596 // }
597 //
598 //
599 // /**
600 // * Calulates the padding according to the options and write it to the stream.
601 // * @param tailLength the length of the tail string
602 // * @throws XMPException thrown if packet size is to small to fit the padding
603 // * @throws IOException forwards writer errors
604 // * @throws IOException
605 // */
606 // private void addPadding(int tailLength) throws XMPException, IOException
607 // {
608 // if (options.getExactPacketLength())
609 // {
610 // // the string length is equal to the length of the UTF-8 encoding
611 // int minSize = outputStream.getBytesWritten() + tailLength * unicodeSize;
612 // if (minSize > padding)
613 // {
614 // throw new XMPException("Can't fit into specified packet size",
615 // XMPError.BADSERIALIZE);
616 // }
617 // padding -= minSize; // Now the actual amount of padding to add.
618 // }
619 //
620 // // fix rest of the padding according to Unicode unit size.
621 // padding /= unicodeSize;
622 //
623 // int newlineLen = options.getNewline().length();
624 // if (padding >= newlineLen)
625 // {
626 // padding -= newlineLen; // Write this newline last.
627 // while (padding >= (100 + newlineLen))
628 // {
629 // writeNewline();
630 // writeChars(100, ' ');
631 // padding -= (100 + newlineLen);
632 // }
633 // writeNewline();
634 // writeChars(padding, ' ');
635 // }
636 // else
637 // {
638 // writeChars(padding, ' ');
639 // }
640 // }
641 //
642 //
643 // /**
644 // * Writes a newline according to the options.
645 // * @throws IOException Forwards exception
646 // */
647 // private void writeNewline() throws IOException
648 // {
649 // writer.writeCharacters(options.getNewline());
650 // }
651 //
652 //
653 // /**
654 // * Writes an amount of chars, mostly spaces
655 // * @param number number of chars
656 // * @param c a char
657 // * @throws IOException
658 // */
659 // private void writeChars(int number, char c) throws IOException
660 // {
661 // char[] buf = new char[number];
662 // Arrays.fill(buf, c);
663 // writer.writeCharacters(buf, 0, number);
664 // }
665 //}