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