This project has retired. For details please refer to its Attic page.
XMLUtils xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.commons.impl;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.OutputStream;
24  import java.math.BigDecimal;
25  import java.math.BigInteger;
26  import java.util.GregorianCalendar;
27  
28  import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
29  import org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException;
30  import org.w3c.dom.Document;
31  import org.xmlpull.v1.XmlPullParser;
32  import org.xmlpull.v1.XmlPullParserException;
33  import org.xmlpull.v1.XmlSerializer;
34  
35  import android.util.Xml;
36  
37  public final class XMLUtils {
38  
39      private XMLUtils() {
40      }
41  
42      // --------------
43      // --- writer ---
44      // --------------
45  
46      /**
47       * Creates a new XML writer.
48       */
49      public static XmlSerializer createWriter(OutputStream out) throws IOException {
50          assert out != null;
51  
52          XmlSerializer writer = Xml.newSerializer();
53          writer.setOutput(out, IOUtils.UTF8);
54          
55          return writer;
56      }
57  
58      /**
59       * Starts a XML document.
60       */
61      public static void startXmlDocument(XmlSerializer writer) throws IOException {
62          assert writer != null;
63          
64          writer.setPrefix(XMLConstants.PREFIX_ATOM, XMLConstants.NAMESPACE_ATOM);
65          writer.setPrefix(XMLConstants.PREFIX_CMIS, XMLConstants.NAMESPACE_CMIS);
66          writer.setPrefix(XMLConstants.PREFIX_RESTATOM, XMLConstants.NAMESPACE_RESTATOM);
67          writer.setPrefix(XMLConstants.PREFIX_APACHE_CHEMISTY, XMLConstants.NAMESPACE_APACHE_CHEMISTRY);
68  
69          writer.startDocument("UTF-8", false);
70      }
71  
72      /**
73       * Ends a XML document.
74       */
75      public static void endXmlDocument(XmlSerializer writer) throws IOException {
76          assert writer != null;
77          
78          // end document
79          writer.endDocument();
80          writer.flush();
81          writer.toString();
82      }
83  
84      /**
85       * Writes a String tag.
86       */
87      public static void write(XmlSerializer writer, String prefix, String namespace, String tag, String value)
88              throws IOException {
89          assert writer != null;
90  
91          if (value == null) {
92              return;
93          }
94  
95          if (namespace == null) {
96              writer.startTag(null, tag);
97          } else {
98              writer.startTag(namespace, tag);
99          }
100         writer.text(value);
101         writer.endTag(namespace, tag);
102     }
103 
104     /**
105      * Writes an Integer tag.
106      */
107     public static void write(XmlSerializer writer, String prefix, String namespace, String tag, BigInteger value)
108             throws IOException {
109         assert writer != null;
110 
111         if (value == null) {
112             return;
113         }
114 
115         write(writer, prefix, namespace, tag, value.toString());
116     }
117 
118     /**
119      * Writes a Decimal tag.
120      */
121     public static void write(XmlSerializer writer, String prefix, String namespace, String tag, BigDecimal value)
122             throws IOException {
123         assert writer != null;
124 
125         if (value == null) {
126             return;
127         }
128 
129         write(writer, prefix, namespace, tag, value.toString());
130     }
131 
132     /**
133      * Writes a DateTime tag.
134      */
135     public static void write(XmlSerializer writer, String prefix, String namespace, String tag, GregorianCalendar value)
136             throws IOException {
137         assert writer != null;
138 
139         if (value == null) {
140             return;
141         }
142 
143         write(writer, prefix, namespace, tag, DateTimeHelper.formatXmlDateTime(value));
144     }
145 
146     /**
147      * Writes a Boolean tag.
148      */
149     public static void write(XmlSerializer writer, String prefix, String namespace, String tag, Boolean value)
150             throws IOException {
151         assert writer != null;
152 
153         if (value == null) {
154             return;
155         }
156 
157         write(writer, prefix, namespace, tag, value ? "true" : "false");
158     }
159 
160     /**
161      * Writes an Enum tag.
162      */
163     public static void write(XmlSerializer writer, String prefix, String namespace, String tag, Enum<?> value)
164             throws IOException {
165         assert writer != null;
166 
167         if (value == null) {
168             return;
169         }
170 
171         Object enumValue;
172         try {
173             enumValue = value.getClass().getMethod("value", new Class[0]).invoke(value, new Object[0]);
174         } catch (Exception e) {
175             throw new IllegalStateException("Cannot get enum value", e);
176         }
177 
178         write(writer, prefix, namespace, tag, enumValue.toString());
179     }
180 
181     // ---------------
182     // ---- parser ---
183     // ---------------
184 
185     /**
186      * Creates a new XML parser with OpenCMIS default settings.
187      */
188     public static XmlPullParser createParser(InputStream stream) throws XmlPullParserException {
189         XmlPullParser parser = Xml.newPullParser();
190         parser.setInput(stream, IOUtils.UTF8);
191         return parser;
192     }
193 
194     /**
195      * Moves the parser to the next element.
196      */
197     public static boolean next(XmlPullParser parser) {
198         assert parser != null;
199 
200         try {
201             if (hasNext(parser)) {
202                 parser.next();
203                 return true;
204             }
205             return false;
206         } catch (Exception e) {
207             // EOF exceptions
208             return false;
209         }
210     }
211 
212     public static boolean hasNext(XmlPullParser parser) throws XmlPullParserException {
213         assert parser != null;
214 
215         return parser.getEventType() != XmlPullParser.END_DOCUMENT;
216     }
217 
218     /**
219      * Skips a tag or subtree.
220      */
221     public static void skip(XmlPullParser parser) throws XmlPullParserException {
222         assert parser != null;
223 
224         int level = 1;
225         while (next(parser)) {
226             int event = parser.getEventType();
227             if (event == XmlPullParser.START_TAG) {
228                 level++;
229             } else if (event == XmlPullParser.END_TAG) {
230                 level--;
231                 if (level == 0) {
232                     break;
233                 }
234             }
235         }
236 
237         next(parser);
238     }
239 
240     /**
241      * Moves the parser to the next start element.
242      * 
243      * @return <code>true</code> if another start element has been found,
244      *         <code>false</code> otherwise
245      */
246     public static boolean findNextStartElemenet(XmlPullParser parser) throws XmlPullParserException, IOException {
247         assert parser != null;
248 
249         while (true) {
250             int event = parser.getEventType();
251 
252             if (event == XmlPullParser.START_TAG) {
253                 return true;
254             }
255 
256             if (hasNext(parser)) {
257                 parser.next();
258             } else {
259                 return false;
260             }
261         }
262     }
263 
264     /**
265      * Parses a tag that contains text.
266      */
267     public static String readText(XmlPullParser parser, int maxLength) throws XmlPullParserException {
268         assert parser != null;
269         assert maxLength >= 0;
270 
271         StringBuilder sb = new StringBuilder(128);
272 
273         next(parser);
274 
275         while (true) {
276             int event = parser.getEventType();
277             if (event == XmlPullParser.END_TAG) {
278                 break;
279             } else if (event == XmlPullParser.TEXT) {
280                 int len = 0;
281                 if (parser.getText() != null) {
282                     len = parser.getText().length();
283                 }
284                 if (len > 0) {
285                     if (sb.length() + len > maxLength) {
286                         throw new CmisInvalidArgumentException("String limit exceeded!");
287                     }
288                     sb.append(parser.getText());
289                 }
290             } else if (event == XmlPullParser.START_TAG) {
291                 throw new XmlPullParserException("Unexpected tag: " + parser.getName());
292             }
293 
294             if (!next(parser)) {
295                 break;
296             }
297         }
298 
299         next(parser);
300 
301         return sb.toString();
302     }
303 
304     // ------------------
305     // ---- DOM stuff ---
306     // ------------------
307     
308     public static Document newDomDocument() {
309         throw new CmisRuntimeException("This method should never be used on Android!");
310     }
311 }