1
2
3
4
5
6 package org.apache.maven.doxia.document.io.xpp3;
7
8
9
10
11
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.io.Reader;
15 import java.text.DateFormat;
16 import org.apache.maven.doxia.document.DocumentAuthor;
17 import org.apache.maven.doxia.document.DocumentCover;
18 import org.apache.maven.doxia.document.DocumentHyperlinkBehaviour;
19 import org.apache.maven.doxia.document.DocumentMeta;
20 import org.apache.maven.doxia.document.DocumentModel;
21 import org.apache.maven.doxia.document.DocumentStatistic;
22 import org.apache.maven.doxia.document.DocumentTOC;
23 import org.apache.maven.doxia.document.DocumentTOCItem;
24 import org.apache.maven.doxia.document.DocumentTemplate;
25 import org.codehaus.plexus.util.ReaderFactory;
26 import org.codehaus.plexus.util.xml.pull.EntityReplacementMap;
27 import org.codehaus.plexus.util.xml.pull.MXParser;
28 import org.codehaus.plexus.util.xml.pull.XmlPullParser;
29 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
30
31
32
33
34
35
36 @SuppressWarnings( "all" )
37 public class DocumentXpp3Reader
38 {
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 private boolean addDefaultEntities = true;
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 private boolean checkFieldWithDuplicate( XmlPullParser parser, String tagName, String alias, java.util.Set parsed )
72 throws XmlPullParserException
73 {
74 if ( !( parser.getName().equals( tagName ) || parser.getName().equals( alias ) ) )
75 {
76 return false;
77 }
78 if ( !parsed.add( tagName ) )
79 {
80 throw new XmlPullParserException( "Duplicated tag: '" + tagName + "'", parser, null );
81 }
82 return true;
83 }
84
85
86
87
88
89
90
91
92
93
94
95 private void checkUnknownAttribute( XmlPullParser parser, String attribute, String tagName, boolean strict )
96 throws XmlPullParserException, IOException
97 {
98
99 if ( strict )
100 {
101 throw new XmlPullParserException( "Unknown attribute '" + attribute + "' for tag '" + tagName + "'", parser, null );
102 }
103 }
104
105
106
107
108
109
110
111
112
113 private void checkUnknownElement( XmlPullParser parser, boolean strict )
114 throws XmlPullParserException, IOException
115 {
116 if ( strict )
117 {
118 throw new XmlPullParserException( "Unrecognised tag: '" + parser.getName() + "'", parser, null );
119 }
120
121 for ( int unrecognizedTagCount = 1; unrecognizedTagCount > 0; )
122 {
123 int eventType = parser.next();
124 if ( eventType == XmlPullParser.START_TAG )
125 {
126 unrecognizedTagCount++;
127 }
128 else if ( eventType == XmlPullParser.END_TAG )
129 {
130 unrecognizedTagCount--;
131 }
132 }
133 }
134
135
136
137
138
139
140 public boolean getAddDefaultEntities()
141 {
142 return addDefaultEntities;
143 }
144
145
146
147
148
149
150
151
152
153
154 private boolean getBooleanValue( String s, String attribute, XmlPullParser parser )
155 throws XmlPullParserException
156 {
157 return getBooleanValue( s, attribute, parser, null );
158 }
159
160
161
162
163
164
165
166
167
168
169
170 private boolean getBooleanValue( String s, String attribute, XmlPullParser parser, String defaultValue )
171 throws XmlPullParserException
172 {
173 if ( s != null && s.length() != 0 )
174 {
175 return Boolean.valueOf( s ).booleanValue();
176 }
177 if ( defaultValue != null )
178 {
179 return Boolean.valueOf( defaultValue ).booleanValue();
180 }
181 return false;
182 }
183
184
185
186
187
188
189
190
191
192
193
194 private byte getByteValue( String s, String attribute, XmlPullParser parser, boolean strict )
195 throws XmlPullParserException
196 {
197 if ( s != null )
198 {
199 try
200 {
201 return Byte.valueOf( s ).byteValue();
202 }
203 catch ( NumberFormatException nfe )
204 {
205 if ( strict )
206 {
207 throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be a byte", parser, nfe );
208 }
209 }
210 }
211 return 0;
212 }
213
214
215
216
217
218
219
220
221
222
223 private char getCharacterValue( String s, String attribute, XmlPullParser parser )
224 throws XmlPullParserException
225 {
226 if ( s != null )
227 {
228 return s.charAt( 0 );
229 }
230 return 0;
231 }
232
233
234
235
236
237
238
239
240
241
242 private java.util.Date getDateValue( String s, String attribute, XmlPullParser parser )
243 throws XmlPullParserException
244 {
245 return getDateValue( s, attribute, null, parser );
246 }
247
248
249
250
251
252
253
254
255
256
257
258 private java.util.Date getDateValue( String s, String attribute, String dateFormat, XmlPullParser parser )
259 throws XmlPullParserException
260 {
261 if ( s != null )
262 {
263 String effectiveDateFormat = dateFormat;
264 if ( dateFormat == null )
265 {
266 effectiveDateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS";
267 }
268 if ( "long".equals( effectiveDateFormat ) )
269 {
270 try
271 {
272 return new java.util.Date( Long.parseLong( s ) );
273 }
274 catch ( NumberFormatException e )
275 {
276 throw new XmlPullParserException( e.getMessage(), parser, e );
277 }
278 }
279 else
280 {
281 try
282 {
283 DateFormat dateParser = new java.text.SimpleDateFormat( effectiveDateFormat, java.util.Locale.US );
284 return dateParser.parse( s );
285 }
286 catch ( java.text.ParseException e )
287 {
288 throw new XmlPullParserException( e.getMessage(), parser, e );
289 }
290 }
291 }
292 return null;
293 }
294
295
296
297
298
299
300
301
302
303
304
305 private double getDoubleValue( String s, String attribute, XmlPullParser parser, boolean strict )
306 throws XmlPullParserException
307 {
308 if ( s != null )
309 {
310 try
311 {
312 return Double.valueOf( s ).doubleValue();
313 }
314 catch ( NumberFormatException nfe )
315 {
316 if ( strict )
317 {
318 throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be a floating point number", parser, nfe );
319 }
320 }
321 }
322 return 0;
323 }
324
325
326
327
328
329
330
331
332
333
334
335 private float getFloatValue( String s, String attribute, XmlPullParser parser, boolean strict )
336 throws XmlPullParserException
337 {
338 if ( s != null )
339 {
340 try
341 {
342 return Float.valueOf( s ).floatValue();
343 }
344 catch ( NumberFormatException nfe )
345 {
346 if ( strict )
347 {
348 throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be a floating point number", parser, nfe );
349 }
350 }
351 }
352 return 0;
353 }
354
355
356
357
358
359
360
361
362
363
364
365 private int getIntegerValue( String s, String attribute, XmlPullParser parser, boolean strict )
366 throws XmlPullParserException
367 {
368 if ( s != null )
369 {
370 try
371 {
372 return Integer.valueOf( s ).intValue();
373 }
374 catch ( NumberFormatException nfe )
375 {
376 if ( strict )
377 {
378 throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be an integer", parser, nfe );
379 }
380 }
381 }
382 return 0;
383 }
384
385
386
387
388
389
390
391
392
393
394
395 private long getLongValue( String s, String attribute, XmlPullParser parser, boolean strict )
396 throws XmlPullParserException
397 {
398 if ( s != null )
399 {
400 try
401 {
402 return Long.valueOf( s ).longValue();
403 }
404 catch ( NumberFormatException nfe )
405 {
406 if ( strict )
407 {
408 throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be a long integer", parser, nfe );
409 }
410 }
411 }
412 return 0;
413 }
414
415
416
417
418
419
420
421
422
423
424
425 private String getRequiredAttributeValue( String s, String attribute, XmlPullParser parser, boolean strict )
426 throws XmlPullParserException
427 {
428 if ( s == null )
429 {
430 if ( strict )
431 {
432 throw new XmlPullParserException( "Missing required value for attribute '" + attribute + "'", parser, null );
433 }
434 }
435 return s;
436 }
437
438
439
440
441
442
443
444
445
446
447
448 private short getShortValue( String s, String attribute, XmlPullParser parser, boolean strict )
449 throws XmlPullParserException
450 {
451 if ( s != null )
452 {
453 try
454 {
455 return Short.valueOf( s ).shortValue();
456 }
457 catch ( NumberFormatException nfe )
458 {
459 if ( strict )
460 {
461 throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be a short integer", parser, nfe );
462 }
463 }
464 }
465 return 0;
466 }
467
468
469
470
471
472
473
474 private String getTrimmedValue( String s )
475 {
476 if ( s != null )
477 {
478 s = s.trim();
479 }
480 return s;
481 }
482
483
484
485
486
487
488
489
490
491 private int nextTag( XmlPullParser parser )
492 throws IOException, XmlPullParserException
493 {
494 int eventType = parser.next();
495 if ( eventType == XmlPullParser.TEXT )
496 {
497 eventType = parser.next();
498 }
499 if ( eventType != XmlPullParser.START_TAG && eventType != XmlPullParser.END_TAG )
500 {
501 throw new XmlPullParserException( "expected START_TAG or END_TAG not " + XmlPullParser.TYPES[eventType], parser, null );
502 }
503 return eventType;
504 }
505
506
507
508
509
510
511
512
513
514
515 public DocumentModel read( Reader reader, boolean strict )
516 throws IOException, XmlPullParserException
517 {
518 XmlPullParser parser = addDefaultEntities ? new MXParser(EntityReplacementMap.defaultEntityReplacementMap) : new MXParser( );
519
520 parser.setInput( reader );
521
522
523 return read( parser, strict );
524 }
525
526
527
528
529
530
531
532
533
534 public DocumentModel read( Reader reader )
535 throws IOException, XmlPullParserException
536 {
537 return read( reader, true );
538 }
539
540
541
542
543
544
545
546
547
548
549 public DocumentModel read( InputStream in, boolean strict )
550 throws IOException, XmlPullParserException
551 {
552 return read( ReaderFactory.newXmlReader( in ), strict );
553 }
554
555
556
557
558
559
560
561
562
563 public DocumentModel read( InputStream in )
564 throws IOException, XmlPullParserException
565 {
566 return read( ReaderFactory.newXmlReader( in ) );
567 }
568
569
570
571
572
573
574
575
576
577
578 private DocumentAuthor parseDocumentAuthor( XmlPullParser parser, boolean strict )
579 throws IOException, XmlPullParserException
580 {
581 String tagName = parser.getName();
582 DocumentAuthor documentAuthor = new DocumentAuthor();
583 for ( int i = parser.getAttributeCount() - 1; i >= 0; i-- )
584 {
585 String name = parser.getAttributeName( i );
586 String value = parser.getAttributeValue( i );
587
588 if ( name.indexOf( ':' ) >= 0 )
589 {
590
591 }
592 else
593 {
594 checkUnknownAttribute( parser, name, tagName, strict );
595 }
596 }
597 java.util.Set parsed = new java.util.HashSet();
598 while ( ( strict ? parser.nextTag() : nextTag( parser ) ) == XmlPullParser.START_TAG )
599 {
600 if ( checkFieldWithDuplicate( parser, "firstName", null, parsed ) )
601 {
602 documentAuthor.setFirstName( getTrimmedValue( parser.nextText() ) );
603 }
604 else if ( checkFieldWithDuplicate( parser, "lastName", null, parsed ) )
605 {
606 documentAuthor.setLastName( getTrimmedValue( parser.nextText() ) );
607 }
608 else if ( checkFieldWithDuplicate( parser, "name", null, parsed ) )
609 {
610 documentAuthor.setName( getTrimmedValue( parser.nextText() ) );
611 }
612 else if ( checkFieldWithDuplicate( parser, "initials", null, parsed ) )
613 {
614 documentAuthor.setInitials( getTrimmedValue( parser.nextText() ) );
615 }
616 else if ( checkFieldWithDuplicate( parser, "title", null, parsed ) )
617 {
618 documentAuthor.setTitle( getTrimmedValue( parser.nextText() ) );
619 }
620 else if ( checkFieldWithDuplicate( parser, "position", null, parsed ) )
621 {
622 documentAuthor.setPosition( getTrimmedValue( parser.nextText() ) );
623 }
624 else if ( checkFieldWithDuplicate( parser, "email", null, parsed ) )
625 {
626 documentAuthor.setEmail( getTrimmedValue( parser.nextText() ) );
627 }
628 else if ( checkFieldWithDuplicate( parser, "phoneNumber", null, parsed ) )
629 {
630 documentAuthor.setPhoneNumber( getTrimmedValue( parser.nextText() ) );
631 }
632 else if ( checkFieldWithDuplicate( parser, "faxNumber", null, parsed ) )
633 {
634 documentAuthor.setFaxNumber( getTrimmedValue( parser.nextText() ) );
635 }
636 else if ( checkFieldWithDuplicate( parser, "companyName", null, parsed ) )
637 {
638 documentAuthor.setCompanyName( getTrimmedValue( parser.nextText() ) );
639 }
640 else if ( checkFieldWithDuplicate( parser, "street", null, parsed ) )
641 {
642 documentAuthor.setStreet( getTrimmedValue( parser.nextText() ) );
643 }
644 else if ( checkFieldWithDuplicate( parser, "city", null, parsed ) )
645 {
646 documentAuthor.setCity( getTrimmedValue( parser.nextText() ) );
647 }
648 else if ( checkFieldWithDuplicate( parser, "postalCode", null, parsed ) )
649 {
650 documentAuthor.setPostalCode( getTrimmedValue( parser.nextText() ) );
651 }
652 else if ( checkFieldWithDuplicate( parser, "country", null, parsed ) )
653 {
654 documentAuthor.setCountry( getTrimmedValue( parser.nextText() ) );
655 }
656 else if ( checkFieldWithDuplicate( parser, "state", null, parsed ) )
657 {
658 documentAuthor.setState( getTrimmedValue( parser.nextText() ) );
659 }
660 else
661 {
662 checkUnknownElement( parser, strict );
663 }
664 }
665 return documentAuthor;
666 }
667
668
669
670
671
672
673
674
675
676
677 private DocumentCover parseDocumentCover( XmlPullParser parser, boolean strict )
678 throws IOException, XmlPullParserException
679 {
680 String tagName = parser.getName();
681 DocumentCover documentCover = new DocumentCover();
682 for ( int i = parser.getAttributeCount() - 1; i >= 0; i-- )
683 {
684 String name = parser.getAttributeName( i );
685 String value = parser.getAttributeValue( i );
686
687 if ( name.indexOf( ':' ) >= 0 )
688 {
689
690 }
691 else
692 {
693 checkUnknownAttribute( parser, name, tagName, strict );
694 }
695 }
696 java.util.Set parsed = new java.util.HashSet();
697 while ( ( strict ? parser.nextTag() : nextTag( parser ) ) == XmlPullParser.START_TAG )
698 {
699 if ( checkFieldWithDuplicate( parser, "coverTitle", null, parsed ) )
700 {
701 documentCover.setCoverTitle( getTrimmedValue( parser.nextText() ) );
702 }
703 else if ( checkFieldWithDuplicate( parser, "coverSubTitle", null, parsed ) )
704 {
705 documentCover.setCoverSubTitle( getTrimmedValue( parser.nextText() ) );
706 }
707 else if ( checkFieldWithDuplicate( parser, "coverVersion", null, parsed ) )
708 {
709 documentCover.setCoverVersion( getTrimmedValue( parser.nextText() ) );
710 }
711 else if ( checkFieldWithDuplicate( parser, "coverType", null, parsed ) )
712 {
713 documentCover.setCoverType( getTrimmedValue( parser.nextText() ) );
714 }
715 else if ( checkFieldWithDuplicate( parser, "coverDate", null, parsed ) )
716 {
717 String dateFormat = null;
718 documentCover.setCoverDate( getDateValue( getTrimmedValue( parser.nextText() ), "coverDate", dateFormat, parser ) );
719 }
720 else if ( checkFieldWithDuplicate( parser, "coverdate", null, parsed ) )
721 {
722 documentCover.setCoverdate( getTrimmedValue( parser.nextText() ) );
723 }
724 else if ( checkFieldWithDuplicate( parser, "authors", null, parsed ) )
725 {
726 java.util.List authors = new java.util.ArrayList();
727 documentCover.setAuthors( authors );
728 while ( parser.nextTag() == XmlPullParser.START_TAG )
729 {
730 if ( "author".equals( parser.getName() ) )
731 {
732 authors.add( parseDocumentAuthor( parser, strict ) );
733 }
734 else
735 {
736 checkUnknownElement( parser, strict );
737 }
738 }
739 }
740 else if ( checkFieldWithDuplicate( parser, "author", null, parsed ) )
741 {
742 documentCover.setAuthor( getTrimmedValue( parser.nextText() ) );
743 }
744 else if ( checkFieldWithDuplicate( parser, "projectName", null, parsed ) )
745 {
746 documentCover.setProjectName( getTrimmedValue( parser.nextText() ) );
747 }
748 else if ( checkFieldWithDuplicate( parser, "projectLogo", null, parsed ) )
749 {
750 documentCover.setProjectLogo( getTrimmedValue( parser.nextText() ) );
751 }
752 else if ( checkFieldWithDuplicate( parser, "companyName", null, parsed ) )
753 {
754 documentCover.setCompanyName( getTrimmedValue( parser.nextText() ) );
755 }
756 else if ( checkFieldWithDuplicate( parser, "companyLogo", null, parsed ) )
757 {
758 documentCover.setCompanyLogo( getTrimmedValue( parser.nextText() ) );
759 }
760 else
761 {
762 checkUnknownElement( parser, strict );
763 }
764 }
765 return documentCover;
766 }
767
768
769
770
771
772
773
774
775
776
777 private DocumentHyperlinkBehaviour parseDocumentHyperlinkBehaviour( XmlPullParser parser, boolean strict )
778 throws IOException, XmlPullParserException
779 {
780 String tagName = parser.getName();
781 DocumentHyperlinkBehaviour documentHyperlinkBehaviour = new DocumentHyperlinkBehaviour();
782 for ( int i = parser.getAttributeCount() - 1; i >= 0; i-- )
783 {
784 String name = parser.getAttributeName( i );
785 String value = parser.getAttributeValue( i );
786
787 if ( name.indexOf( ':' ) >= 0 )
788 {
789
790 }
791 else if ( "targetFrame".equals( name ) )
792 {
793 documentHyperlinkBehaviour.setTargetFrame( getTrimmedValue( value ) );
794 }
795 else
796 {
797 checkUnknownAttribute( parser, name, tagName, strict );
798 }
799 }
800 java.util.Set parsed = new java.util.HashSet();
801 while ( ( strict ? parser.nextTag() : nextTag( parser ) ) == XmlPullParser.START_TAG )
802 {
803 checkUnknownElement( parser, strict );
804 }
805 return documentHyperlinkBehaviour;
806 }
807
808
809
810
811
812
813
814
815
816
817 private DocumentMeta parseDocumentMeta( XmlPullParser parser, boolean strict )
818 throws IOException, XmlPullParserException
819 {
820 String tagName = parser.getName();
821 DocumentMeta documentMeta = new DocumentMeta();
822 for ( int i = parser.getAttributeCount() - 1; i >= 0; i-- )
823 {
824 String name = parser.getAttributeName( i );
825 String value = parser.getAttributeValue( i );
826
827 if ( name.indexOf( ':' ) >= 0 )
828 {
829
830 }
831 else
832 {
833 checkUnknownAttribute( parser, name, tagName, strict );
834 }
835 }
836 java.util.Set parsed = new java.util.HashSet();
837 while ( ( strict ? parser.nextTag() : nextTag( parser ) ) == XmlPullParser.START_TAG )
838 {
839 if ( checkFieldWithDuplicate( parser, "title", null, parsed ) )
840 {
841 documentMeta.setTitle( getTrimmedValue( parser.nextText() ) );
842 }
843 else if ( checkFieldWithDuplicate( parser, "author", null, parsed ) )
844 {
845 documentMeta.setAuthor( getTrimmedValue( parser.nextText() ) );
846 }
847 else if ( checkFieldWithDuplicate( parser, "authors", null, parsed ) )
848 {
849 java.util.List authors = new java.util.ArrayList();
850 documentMeta.setAuthors( authors );
851 while ( parser.nextTag() == XmlPullParser.START_TAG )
852 {
853 if ( "author".equals( parser.getName() ) )
854 {
855 authors.add( parseDocumentAuthor( parser, strict ) );
856 }
857 else
858 {
859 checkUnknownElement( parser, strict );
860 }
861 }
862 }
863 else if ( checkFieldWithDuplicate( parser, "subject", null, parsed ) )
864 {
865 documentMeta.setSubject( getTrimmedValue( parser.nextText() ) );
866 }
867 else if ( checkFieldWithDuplicate( parser, "keywords", null, parsed ) )
868 {
869 documentMeta.setKeywords( getTrimmedValue( parser.nextText() ) );
870 }
871 else if ( checkFieldWithDuplicate( parser, "keyWords", null, parsed ) )
872 {
873 java.util.List keyWords = new java.util.ArrayList();
874 documentMeta.setKeyWords( keyWords );
875 while ( parser.nextTag() == XmlPullParser.START_TAG )
876 {
877 if ( "keyWord".equals( parser.getName() ) )
878 {
879 keyWords.add( getTrimmedValue( parser.nextText() ) );
880 }
881 else
882 {
883 checkUnknownElement( parser, strict );
884 }
885 }
886 }
887 else if ( checkFieldWithDuplicate( parser, "pageSize", null, parsed ) )
888 {
889 documentMeta.setPageSize( getTrimmedValue( parser.nextText() ) );
890 }
891 else if ( checkFieldWithDuplicate( parser, "generator", null, parsed ) )
892 {
893 documentMeta.setGenerator( getTrimmedValue( parser.nextText() ) );
894 }
895 else if ( checkFieldWithDuplicate( parser, "description", null, parsed ) )
896 {
897 documentMeta.setDescription( getTrimmedValue( parser.nextText() ) );
898 }
899 else if ( checkFieldWithDuplicate( parser, "initialCreator", null, parsed ) )
900 {
901 documentMeta.setInitialCreator( getTrimmedValue( parser.nextText() ) );
902 }
903 else if ( checkFieldWithDuplicate( parser, "creator", null, parsed ) )
904 {
905 documentMeta.setCreator( getTrimmedValue( parser.nextText() ) );
906 }
907 else if ( checkFieldWithDuplicate( parser, "printedBy", null, parsed ) )
908 {
909 documentMeta.setPrintedBy( getTrimmedValue( parser.nextText() ) );
910 }
911 else if ( checkFieldWithDuplicate( parser, "creationDate", null, parsed ) )
912 {
913 String dateFormat = null;
914 documentMeta.setCreationDate( getDateValue( getTrimmedValue( parser.nextText() ), "creationDate", dateFormat, parser ) );
915 }
916 else if ( checkFieldWithDuplicate( parser, "creationdate", null, parsed ) )
917 {
918 documentMeta.setCreationdate( getTrimmedValue( parser.nextText() ) );
919 }
920 else if ( checkFieldWithDuplicate( parser, "date", null, parsed ) )
921 {
922 String dateFormat = null;
923 documentMeta.setDate( getDateValue( getTrimmedValue( parser.nextText() ), "date", dateFormat, parser ) );
924 }
925 else if ( checkFieldWithDuplicate( parser, "modifydate", null, parsed ) )
926 {
927 documentMeta.setModifydate( getTrimmedValue( parser.nextText() ) );
928 }
929 else if ( checkFieldWithDuplicate( parser, "printDate", null, parsed ) )
930 {
931 String dateFormat = null;
932 documentMeta.setPrintDate( getDateValue( getTrimmedValue( parser.nextText() ), "printDate", dateFormat, parser ) );
933 }
934 else if ( checkFieldWithDuplicate( parser, "printdate", null, parsed ) )
935 {
936 documentMeta.setPrintdate( getTrimmedValue( parser.nextText() ) );
937 }
938 else if ( checkFieldWithDuplicate( parser, "template", null, parsed ) )
939 {
940 documentMeta.setTemplate( parseDocumentTemplate( parser, strict ) );
941 }
942 else if ( checkFieldWithDuplicate( parser, "hyperlinkBehaviour", null, parsed ) )
943 {
944 documentMeta.setHyperlinkBehaviour( parseDocumentHyperlinkBehaviour( parser, strict ) );
945 }
946 else if ( checkFieldWithDuplicate( parser, "language", null, parsed ) )
947 {
948 documentMeta.setLanguage( getTrimmedValue( parser.nextText() ) );
949 }
950 else if ( checkFieldWithDuplicate( parser, "editingCycles", null, parsed ) )
951 {
952 documentMeta.setEditingCycles( getLongValue( getTrimmedValue( parser.nextText() ), "editingCycles", parser, strict ) );
953 }
954 else if ( checkFieldWithDuplicate( parser, "editingDuration", null, parsed ) )
955 {
956 documentMeta.setEditingDuration( getLongValue( getTrimmedValue( parser.nextText() ), "editingDuration", parser, strict ) );
957 }
958 else if ( checkFieldWithDuplicate( parser, "documentStatistic", null, parsed ) )
959 {
960 documentMeta.setDocumentStatistic( parseDocumentStatistic( parser, strict ) );
961 }
962 else if ( checkFieldWithDuplicate( parser, "confidential", null, parsed ) )
963 {
964 documentMeta.setConfidential( getBooleanValue( getTrimmedValue( parser.nextText() ), "confidential", parser, "false" ) );
965 }
966 else if ( checkFieldWithDuplicate( parser, "draft", null, parsed ) )
967 {
968 documentMeta.setDraft( getBooleanValue( getTrimmedValue( parser.nextText() ), "draft", parser, "false" ) );
969 }
970 else
971 {
972 checkUnknownElement( parser, strict );
973 }
974 }
975 return documentMeta;
976 }
977
978
979
980
981
982
983
984
985
986
987 private DocumentModel parseDocumentModel( XmlPullParser parser, boolean strict )
988 throws IOException, XmlPullParserException
989 {
990 String tagName = parser.getName();
991 DocumentModel documentModel = new DocumentModel();
992 for ( int i = parser.getAttributeCount() - 1; i >= 0; i-- )
993 {
994 String name = parser.getAttributeName( i );
995 String value = parser.getAttributeValue( i );
996
997 if ( name.indexOf( ':' ) >= 0 )
998 {
999
1000 }
1001 else if ( "xmlns".equals( name ) )
1002 {
1003
1004 }
1005 else if ( "outputName".equals( name ) )
1006 {
1007 documentModel.setOutputName( getTrimmedValue( value ) );
1008 }
1009 else
1010 {
1011 checkUnknownAttribute( parser, name, tagName, strict );
1012 }
1013 }
1014 java.util.Set parsed = new java.util.HashSet();
1015 while ( ( strict ? parser.nextTag() : nextTag( parser ) ) == XmlPullParser.START_TAG )
1016 {
1017 if ( checkFieldWithDuplicate( parser, "meta", null, parsed ) )
1018 {
1019 documentModel.setMeta( parseDocumentMeta( parser, strict ) );
1020 }
1021 else if ( checkFieldWithDuplicate( parser, "toc", null, parsed ) )
1022 {
1023 documentModel.setToc( parseDocumentTOC( parser, strict ) );
1024 }
1025 else if ( checkFieldWithDuplicate( parser, "cover", null, parsed ) )
1026 {
1027 documentModel.setCover( parseDocumentCover( parser, strict ) );
1028 }
1029 else
1030 {
1031 checkUnknownElement( parser, strict );
1032 }
1033 }
1034 return documentModel;
1035 }
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046 private DocumentStatistic parseDocumentStatistic( XmlPullParser parser, boolean strict )
1047 throws IOException, XmlPullParserException
1048 {
1049 String tagName = parser.getName();
1050 DocumentStatistic documentStatistic = new DocumentStatistic();
1051 for ( int i = parser.getAttributeCount() - 1; i >= 0; i-- )
1052 {
1053 String name = parser.getAttributeName( i );
1054 String value = parser.getAttributeValue( i );
1055
1056 if ( name.indexOf( ':' ) >= 0 )
1057 {
1058
1059 }
1060 else if ( "pageCount".equals( name ) )
1061 {
1062 documentStatistic.setPageCount( getLongValue( getTrimmedValue( value ), "pageCount", parser, strict ) );
1063 }
1064 else if ( "tableCount".equals( name ) )
1065 {
1066 documentStatistic.setTableCount( getLongValue( getTrimmedValue( value ), "tableCount", parser, strict ) );
1067 }
1068 else if ( "drawCount".equals( name ) )
1069 {
1070 documentStatistic.setDrawCount( getLongValue( getTrimmedValue( value ), "drawCount", parser, strict ) );
1071 }
1072 else if ( "imageCount".equals( name ) )
1073 {
1074 documentStatistic.setImageCount( getLongValue( getTrimmedValue( value ), "imageCount", parser, strict ) );
1075 }
1076 else if ( "objectCount".equals( name ) )
1077 {
1078 documentStatistic.setObjectCount( getLongValue( getTrimmedValue( value ), "objectCount", parser, strict ) );
1079 }
1080 else if ( "oleObjectCount".equals( name ) )
1081 {
1082 documentStatistic.setOleObjectCount( getLongValue( getTrimmedValue( value ), "oleObjectCount", parser, strict ) );
1083 }
1084 else if ( "paragraphCount".equals( name ) )
1085 {
1086 documentStatistic.setParagraphCount( getLongValue( getTrimmedValue( value ), "paragraphCount", parser, strict ) );
1087 }
1088 else if ( "wordCount".equals( name ) )
1089 {
1090 documentStatistic.setWordCount( getLongValue( getTrimmedValue( value ), "wordCount", parser, strict ) );
1091 }
1092 else if ( "characterCount".equals( name ) )
1093 {
1094 documentStatistic.setCharacterCount( getLongValue( getTrimmedValue( value ), "characterCount", parser, strict ) );
1095 }
1096 else if ( "rowCount".equals( name ) )
1097 {
1098 documentStatistic.setRowCount( getLongValue( getTrimmedValue( value ), "rowCount", parser, strict ) );
1099 }
1100 else if ( "frameCount".equals( name ) )
1101 {
1102 documentStatistic.setFrameCount( getLongValue( getTrimmedValue( value ), "frameCount", parser, strict ) );
1103 }
1104 else if ( "sentenceCount".equals( name ) )
1105 {
1106 documentStatistic.setSentenceCount( getLongValue( getTrimmedValue( value ), "sentenceCount", parser, strict ) );
1107 }
1108 else if ( "syllableCount".equals( name ) )
1109 {
1110 documentStatistic.setSyllableCount( getLongValue( getTrimmedValue( value ), "syllableCount", parser, strict ) );
1111 }
1112 else if ( "nonWhitespaceCharacterCount".equals( name ) )
1113 {
1114 documentStatistic.setNonWhitespaceCharacterCount( getLongValue( getTrimmedValue( value ), "nonWhitespaceCharacterCount", parser, strict ) );
1115 }
1116 else
1117 {
1118 checkUnknownAttribute( parser, name, tagName, strict );
1119 }
1120 }
1121 java.util.Set parsed = new java.util.HashSet();
1122 while ( ( strict ? parser.nextTag() : nextTag( parser ) ) == XmlPullParser.START_TAG )
1123 {
1124 checkUnknownElement( parser, strict );
1125 }
1126 return documentStatistic;
1127 }
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138 private DocumentTOC parseDocumentTOC( XmlPullParser parser, boolean strict )
1139 throws IOException, XmlPullParserException
1140 {
1141 String tagName = parser.getName();
1142 DocumentTOC documentTOC = new DocumentTOC();
1143 for ( int i = parser.getAttributeCount() - 1; i >= 0; i-- )
1144 {
1145 String name = parser.getAttributeName( i );
1146 String value = parser.getAttributeValue( i );
1147
1148 if ( name.indexOf( ':' ) >= 0 )
1149 {
1150
1151 }
1152 else if ( "name".equals( name ) )
1153 {
1154 documentTOC.setName( getTrimmedValue( value ) );
1155 }
1156 else if ( "depth".equals( name ) )
1157 {
1158 documentTOC.setDepth( getIntegerValue( getTrimmedValue( value ), "depth", parser, strict ) );
1159 }
1160 else
1161 {
1162 checkUnknownAttribute( parser, name, tagName, strict );
1163 }
1164 }
1165 java.util.Set parsed = new java.util.HashSet();
1166 while ( ( strict ? parser.nextTag() : nextTag( parser ) ) == XmlPullParser.START_TAG )
1167 {
1168 if ( "item".equals( parser.getName() ) )
1169 {
1170 java.util.List items = documentTOC.getItems();
1171 if ( items == null )
1172 {
1173 items = new java.util.ArrayList();
1174 documentTOC.setItems( items );
1175 }
1176 items.add( parseDocumentTOCItem( parser, strict ) );
1177 }
1178 else
1179 {
1180 checkUnknownElement( parser, strict );
1181 }
1182 }
1183 return documentTOC;
1184 }
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195 private DocumentTOCItem parseDocumentTOCItem( XmlPullParser parser, boolean strict )
1196 throws IOException, XmlPullParserException
1197 {
1198 String tagName = parser.getName();
1199 DocumentTOCItem documentTOCItem = new DocumentTOCItem();
1200 for ( int i = parser.getAttributeCount() - 1; i >= 0; i-- )
1201 {
1202 String name = parser.getAttributeName( i );
1203 String value = parser.getAttributeValue( i );
1204
1205 if ( name.indexOf( ':' ) >= 0 )
1206 {
1207
1208 }
1209 else if ( "name".equals( name ) )
1210 {
1211 documentTOCItem.setName( getTrimmedValue( value ) );
1212 }
1213 else if ( "ref".equals( name ) )
1214 {
1215 documentTOCItem.setRef( getTrimmedValue( value ) );
1216 }
1217 else if ( "collapse".equals( name ) )
1218 {
1219 documentTOCItem.setCollapse( getBooleanValue( getTrimmedValue( value ), "collapse", parser, "false" ) );
1220 }
1221 else
1222 {
1223 checkUnknownAttribute( parser, name, tagName, strict );
1224 }
1225 }
1226 java.util.Set parsed = new java.util.HashSet();
1227 while ( ( strict ? parser.nextTag() : nextTag( parser ) ) == XmlPullParser.START_TAG )
1228 {
1229 if ( "item".equals( parser.getName() ) )
1230 {
1231 java.util.List items = documentTOCItem.getItems();
1232 if ( items == null )
1233 {
1234 items = new java.util.ArrayList();
1235 documentTOCItem.setItems( items );
1236 }
1237 items.add( parseDocumentTOCItem( parser, strict ) );
1238 }
1239 else
1240 {
1241 checkUnknownElement( parser, strict );
1242 }
1243 }
1244 return documentTOCItem;
1245 }
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256 private DocumentTemplate parseDocumentTemplate( XmlPullParser parser, boolean strict )
1257 throws IOException, XmlPullParserException
1258 {
1259 String tagName = parser.getName();
1260 DocumentTemplate documentTemplate = new DocumentTemplate();
1261 for ( int i = parser.getAttributeCount() - 1; i >= 0; i-- )
1262 {
1263 String name = parser.getAttributeName( i );
1264 String value = parser.getAttributeValue( i );
1265
1266 if ( name.indexOf( ':' ) >= 0 )
1267 {
1268
1269 }
1270 else if ( "href".equals( name ) )
1271 {
1272 documentTemplate.setHref( getTrimmedValue( value ) );
1273 }
1274 else if ( "title".equals( name ) )
1275 {
1276 documentTemplate.setTitle( getTrimmedValue( value ) );
1277 }
1278 else if ( "date".equals( name ) )
1279 {
1280 String dateFormat = null;
1281 documentTemplate.setDate( getDateValue( getTrimmedValue( value ), "date", dateFormat, parser ) );
1282 }
1283 else if ( "modifydate".equals( name ) )
1284 {
1285 documentTemplate.setModifydate( getTrimmedValue( value ) );
1286 }
1287 else
1288 {
1289 checkUnknownAttribute( parser, name, tagName, strict );
1290 }
1291 }
1292 java.util.Set parsed = new java.util.HashSet();
1293 while ( ( strict ? parser.nextTag() : nextTag( parser ) ) == XmlPullParser.START_TAG )
1294 {
1295 checkUnknownElement( parser, strict );
1296 }
1297 return documentTemplate;
1298 }
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309 private DocumentModel read( XmlPullParser parser, boolean strict )
1310 throws IOException, XmlPullParserException
1311 {
1312 int eventType = parser.getEventType();
1313 while ( eventType != XmlPullParser.END_DOCUMENT )
1314 {
1315 if ( eventType == XmlPullParser.START_TAG )
1316 {
1317 if ( strict && ! "document".equals( parser.getName() ) )
1318 {
1319 throw new XmlPullParserException( "Expected root element 'document' but found '" + parser.getName() + "'", parser, null );
1320 }
1321 DocumentModel documentModel = parseDocumentModel( parser, strict );
1322 documentModel.setModelEncoding( parser.getInputEncoding() );
1323 return documentModel;
1324 }
1325 eventType = parser.next();
1326 }
1327 throw new XmlPullParserException( "Expected root element 'document' but found no element at all: invalid XML document", parser, null );
1328 }
1329
1330
1331
1332
1333
1334
1335 public void setAddDefaultEntities( boolean addDefaultEntities )
1336 {
1337 this.addDefaultEntities = addDefaultEntities;
1338 }
1339
1340 }