1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
package org.apache.maven.doxia.docrenderer.document.io.xpp3; |
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
import java.io.Writer; |
12 | |
import java.text.DateFormat; |
13 | |
import java.util.Iterator; |
14 | |
import java.util.Locale; |
15 | |
import org.apache.maven.doxia.docrenderer.document.DocumentMeta; |
16 | |
import org.apache.maven.doxia.docrenderer.document.DocumentModel; |
17 | |
import org.apache.maven.doxia.docrenderer.document.DocumentTOC; |
18 | |
import org.apache.maven.doxia.docrenderer.document.DocumentTOCItem; |
19 | |
import org.codehaus.plexus.util.xml.pull.MXSerializer; |
20 | |
import org.codehaus.plexus.util.xml.pull.XmlSerializer; |
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | 0 | public class DocumentXpp3Writer { |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
private String NAMESPACE; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
public void write(Writer writer, DocumentModel documentModel) |
51 | |
throws java.io.IOException |
52 | |
{ |
53 | 0 | XmlSerializer serializer = new MXSerializer(); |
54 | 0 | serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-indentation", " " ); |
55 | 0 | serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-line-separator", "\n" ); |
56 | 0 | serializer.setOutput( writer ); |
57 | 0 | serializer.startDocument( documentModel.getModelEncoding(), null ); |
58 | 0 | writeDocumentModel( documentModel, "document", serializer ); |
59 | 0 | serializer.endDocument(); |
60 | 0 | } |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
private void writeDocumentMeta(DocumentMeta documentMeta, String tagName, XmlSerializer serializer) |
70 | |
throws java.io.IOException |
71 | |
{ |
72 | 0 | if ( documentMeta != null ) |
73 | |
{ |
74 | 0 | serializer.startTag( NAMESPACE, tagName ); |
75 | 0 | if ( documentMeta.getTitle() != null ) |
76 | |
{ |
77 | 0 | serializer.startTag( NAMESPACE, "title" ).text( documentMeta.getTitle() ).endTag( NAMESPACE, "title" ); |
78 | |
} |
79 | 0 | if ( documentMeta.getAuthor() != null ) |
80 | |
{ |
81 | 0 | serializer.startTag( NAMESPACE, "author" ).text( documentMeta.getAuthor() ).endTag( NAMESPACE, "author" ); |
82 | |
} |
83 | 0 | if ( documentMeta.getSubject() != null ) |
84 | |
{ |
85 | 0 | serializer.startTag( NAMESPACE, "subject" ).text( documentMeta.getSubject() ).endTag( NAMESPACE, "subject" ); |
86 | |
} |
87 | 0 | if ( documentMeta.getKeywords() != null ) |
88 | |
{ |
89 | 0 | serializer.startTag( NAMESPACE, "keywords" ).text( documentMeta.getKeywords() ).endTag( NAMESPACE, "keywords" ); |
90 | |
} |
91 | 0 | if ( documentMeta.getPageSize() != null ) |
92 | |
{ |
93 | 0 | serializer.startTag( NAMESPACE, "pageSize" ).text( documentMeta.getPageSize() ).endTag( NAMESPACE, "pageSize" ); |
94 | |
} |
95 | 0 | serializer.endTag( NAMESPACE, tagName ); |
96 | |
} |
97 | 0 | } |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
private void writeDocumentModel(DocumentModel documentModel, String tagName, XmlSerializer serializer) |
107 | |
throws java.io.IOException |
108 | |
{ |
109 | 0 | if ( documentModel != null ) |
110 | |
{ |
111 | 0 | serializer.startTag( NAMESPACE, tagName ); |
112 | 0 | if ( documentModel.getOutputName() != null ) |
113 | |
{ |
114 | 0 | serializer.attribute( NAMESPACE, "outputName", documentModel.getOutputName() ); |
115 | |
} |
116 | 0 | if ( documentModel.getMeta() != null ) |
117 | |
{ |
118 | 0 | writeDocumentMeta( (DocumentMeta) documentModel.getMeta(), "meta", serializer ); |
119 | |
} |
120 | 0 | if ( documentModel.getToc() != null ) |
121 | |
{ |
122 | 0 | writeDocumentTOC( (DocumentTOC) documentModel.getToc(), "toc", serializer ); |
123 | |
} |
124 | 0 | serializer.endTag( NAMESPACE, tagName ); |
125 | |
} |
126 | 0 | } |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
private void writeDocumentTOC(DocumentTOC documentTOC, String tagName, XmlSerializer serializer) |
136 | |
throws java.io.IOException |
137 | |
{ |
138 | 0 | if ( documentTOC != null ) |
139 | |
{ |
140 | 0 | serializer.startTag( NAMESPACE, tagName ); |
141 | 0 | if ( documentTOC.getName() != null ) |
142 | |
{ |
143 | 0 | serializer.attribute( NAMESPACE, "name", documentTOC.getName() ); |
144 | |
} |
145 | 0 | if ( documentTOC.getItems() != null && documentTOC.getItems().size() > 0 ) |
146 | |
{ |
147 | 0 | for ( Iterator iter = documentTOC.getItems().iterator(); iter.hasNext(); ) |
148 | |
{ |
149 | 0 | DocumentTOCItem o = (DocumentTOCItem) iter.next(); |
150 | 0 | writeDocumentTOCItem( o, "item", serializer ); |
151 | |
} |
152 | |
} |
153 | 0 | serializer.endTag( NAMESPACE, tagName ); |
154 | |
} |
155 | 0 | } |
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
private void writeDocumentTOCItem(DocumentTOCItem documentTOCItem, String tagName, XmlSerializer serializer) |
165 | |
throws java.io.IOException |
166 | |
{ |
167 | 0 | if ( documentTOCItem != null ) |
168 | |
{ |
169 | 0 | serializer.startTag( NAMESPACE, tagName ); |
170 | 0 | if ( documentTOCItem.getName() != null ) |
171 | |
{ |
172 | 0 | serializer.attribute( NAMESPACE, "name", documentTOCItem.getName() ); |
173 | |
} |
174 | 0 | if ( documentTOCItem.getRef() != null ) |
175 | |
{ |
176 | 0 | serializer.attribute( NAMESPACE, "ref", documentTOCItem.getRef() ); |
177 | |
} |
178 | 0 | serializer.endTag( NAMESPACE, tagName ); |
179 | |
} |
180 | 0 | } |
181 | |
|
182 | |
|
183 | |
} |