View Javadoc
1   package org.apache.maven.model.transform.pull;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.ByteArrayInputStream;
23  import java.io.ByteArrayOutputStream;
24  import java.io.IOException;
25  import java.io.OutputStreamWriter;
26  import java.io.UnsupportedEncodingException;
27  import java.io.Writer;
28  
29  import org.codehaus.plexus.util.xml.XmlStreamReader;
30  import org.codehaus.plexus.util.xml.pull.MXSerializer;
31  import org.codehaus.plexus.util.xml.pull.XmlPullParser;
32  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
33  import org.codehaus.plexus.util.xml.pull.XmlSerializer;
34  
35  public class XmlUtils
36  {
37  
38      public static ByteArrayInputStream writeDocument( XmlStreamReader reader, XmlPullParser parser )
39              throws IOException, XmlPullParserException
40      {
41          ByteArrayOutputStream baos = new ByteArrayOutputStream();
42          Writer writer = newWriter( reader, baos );
43          writeDocument( parser, writer );
44          return new ByteArrayInputStream( baos.toByteArray() );
45      }
46  
47      public static void writeDocument( XmlPullParser parser, Writer writer )
48              throws IOException, XmlPullParserException
49      {
50          XmlSerializer serializer = new MXSerializer();
51          serializer.setOutput( writer );
52  
53          while ( parser.nextToken() != XmlPullParser.END_DOCUMENT )
54          {
55              switch ( parser.getEventType() )
56              {
57                  case XmlPullParser.START_DOCUMENT:
58                      serializer.startDocument( parser.getInputEncoding(), true );
59                      break;
60                  case XmlPullParser.END_DOCUMENT:
61                      serializer.endDocument();
62                  case XmlPullParser.START_TAG:
63                      int nsStart = parser.getNamespaceCount( parser.getDepth() - 1 );
64                      int nsEnd = parser.getNamespaceCount( parser.getDepth() );
65                      for ( int i = nsStart; i < nsEnd; i++ )
66                      {
67                          String prefix = parser.getNamespacePrefix( i );
68                          String ns = parser.getNamespaceUri( i );
69                          serializer.setPrefix( prefix, ns );
70                      }
71                      serializer.startTag( parser.getNamespace(), parser.getName() );
72                      for ( int i = 0; i < parser.getAttributeCount(); i++ )
73                      {
74                          serializer.attribute( parser.getAttributeNamespace( i ),
75                                                parser.getAttributeName( i ),
76                                                parser.getAttributeValue( i ) );
77                      }
78                      break;
79                  case XmlPullParser.END_TAG:
80                      serializer.endTag( parser.getNamespace(), parser.getName() );
81                      break;
82                  case XmlPullParser.TEXT:
83                      serializer.text( normalize( parser.getText() ) );
84                      break;
85                  case XmlPullParser.CDSECT:
86                      serializer.cdsect( parser.getText() );
87                      break;
88                  case XmlPullParser.ENTITY_REF:
89                      serializer.entityRef( parser.getName() );
90                      break;
91                  case XmlPullParser.IGNORABLE_WHITESPACE:
92                      serializer.ignorableWhitespace( normalize( parser.getText() ) );
93                      break;
94                  case XmlPullParser.PROCESSING_INSTRUCTION:
95                      serializer.processingInstruction( parser.getText() );
96                      break;
97                  case XmlPullParser.COMMENT:
98                      serializer.comment( normalize( parser.getText() ) );
99                      break;
100                 case XmlPullParser.DOCDECL:
101                     serializer.docdecl( normalize( parser.getText() ) );
102                     break;
103                 default:
104                     break;
105             }
106         }
107 
108         serializer.endDocument();
109     }
110 
111     private static OutputStreamWriter newWriter( XmlStreamReader reader, ByteArrayOutputStream baos )
112             throws UnsupportedEncodingException
113     {
114         if ( reader.getEncoding() != null )
115         {
116             return new OutputStreamWriter( baos, reader.getEncoding() );
117         }
118         else
119         {
120             return new OutputStreamWriter( baos );
121         }
122     }
123 
124     private static String normalize( String input )
125     {
126         if ( input.indexOf( '\n' ) >= 0 && !"\n".equals( System.lineSeparator() ) )
127         {
128             return input.replace( "\n", System.lineSeparator() );
129         }
130         return input;
131     }
132 }