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.maven.model.transform.pull;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.ByteArrayOutputStream;
23  import java.io.IOException;
24  import java.io.OutputStreamWriter;
25  import java.io.UnsupportedEncodingException;
26  import java.io.Writer;
27  
28  import org.codehaus.plexus.util.xml.XmlStreamReader;
29  import org.codehaus.plexus.util.xml.pull.MXSerializer;
30  import org.codehaus.plexus.util.xml.pull.XmlPullParser;
31  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
32  import org.codehaus.plexus.util.xml.pull.XmlSerializer;
33  
34  public class XmlUtils {
35  
36      public static ByteArrayInputStream writeDocument(XmlStreamReader reader, XmlPullParser parser)
37              throws IOException, XmlPullParserException {
38          ByteArrayOutputStream baos = new ByteArrayOutputStream();
39          Writer writer = newWriter(reader, baos);
40          writeDocument(parser, writer);
41          return new ByteArrayInputStream(baos.toByteArray());
42      }
43  
44      public static void writeDocument(XmlPullParser parser, Writer writer) throws IOException, XmlPullParserException {
45          XmlSerializer serializer = new MXSerializer();
46          serializer.setOutput(writer);
47  
48          while (parser.nextToken() != XmlPullParser.END_DOCUMENT) {
49              switch (parser.getEventType()) {
50                  case XmlPullParser.START_DOCUMENT:
51                      serializer.startDocument(parser.getInputEncoding(), true);
52                      break;
53                  case XmlPullParser.END_DOCUMENT:
54                      serializer.endDocument();
55                  case XmlPullParser.START_TAG:
56                      int nsStart = parser.getNamespaceCount(parser.getDepth() - 1);
57                      int nsEnd = parser.getNamespaceCount(parser.getDepth());
58                      for (int i = nsStart; i < nsEnd; i++) {
59                          String prefix = parser.getNamespacePrefix(i);
60                          String ns = parser.getNamespaceUri(i);
61                          serializer.setPrefix(prefix, ns);
62                      }
63                      serializer.startTag(parser.getNamespace(), parser.getName());
64                      for (int i = 0; i < parser.getAttributeCount(); i++) {
65                          serializer.attribute(
66                                  parser.getAttributeNamespace(i),
67                                  parser.getAttributeName(i),
68                                  parser.getAttributeValue(i));
69                      }
70                      break;
71                  case XmlPullParser.END_TAG:
72                      serializer.endTag(parser.getNamespace(), parser.getName());
73                      break;
74                  case XmlPullParser.TEXT:
75                      serializer.text(normalize(parser.getText()));
76                      break;
77                  case XmlPullParser.CDSECT:
78                      serializer.cdsect(parser.getText());
79                      break;
80                  case XmlPullParser.ENTITY_REF:
81                      serializer.entityRef(parser.getName());
82                      break;
83                  case XmlPullParser.IGNORABLE_WHITESPACE:
84                      serializer.ignorableWhitespace(normalize(parser.getText()));
85                      break;
86                  case XmlPullParser.PROCESSING_INSTRUCTION:
87                      serializer.processingInstruction(parser.getText());
88                      break;
89                  case XmlPullParser.COMMENT:
90                      serializer.comment(normalize(parser.getText()));
91                      break;
92                  case XmlPullParser.DOCDECL:
93                      serializer.docdecl(normalize(parser.getText()));
94                      break;
95                  default:
96                      break;
97              }
98          }
99  
100         serializer.endDocument();
101     }
102 
103     private static OutputStreamWriter newWriter(XmlStreamReader reader, ByteArrayOutputStream baos)
104             throws UnsupportedEncodingException {
105         if (reader.getEncoding() != null) {
106             return new OutputStreamWriter(baos, reader.getEncoding());
107         } else {
108             return new OutputStreamWriter(baos);
109         }
110     }
111 
112     private static String normalize(String input) {
113         if (input.indexOf('\n') >= 0 && !"\n".equals(System.lineSeparator())) {
114             return input.replace("\n", System.lineSeparator());
115         }
116         return input;
117     }
118 }