View Javadoc
1   package org.codehaus.plexus.util.xml;
2   
3   /*
4    * Copyright The Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * 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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.io.Reader;
22  
23  import org.apache.maven.internal.xml.XmlNodeBuilder;
24  import org.codehaus.plexus.util.xml.pull.XmlPullParser;
25  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
26  
27  /**
28   *
29   */
30  public class Xpp3DomBuilder {
31      private static final boolean DEFAULT_TRIM = true;
32  
33      public static Xpp3Dom build(Reader reader) throws XmlPullParserException, IOException {
34          return build(reader, null);
35      }
36  
37      /**
38       * @since 3.2.0
39       */
40      public static Xpp3Dom build(Reader reader, InputLocationBuilder locationBuilder)
41              throws XmlPullParserException, IOException {
42          return build(reader, DEFAULT_TRIM, locationBuilder);
43      }
44  
45      public static Xpp3Dom build(InputStream is, String encoding) throws XmlPullParserException, IOException {
46          return build(is, encoding, DEFAULT_TRIM);
47      }
48  
49      public static Xpp3Dom build(InputStream is, String encoding, boolean trim)
50              throws XmlPullParserException, IOException {
51          try (InputStream closeMe = is) {
52              return new Xpp3Dom(XmlNodeBuilder.build(is, encoding, trim));
53          }
54      }
55  
56      public static Xpp3Dom build(Reader reader, boolean trim) throws XmlPullParserException, IOException {
57          return build(reader, trim, null);
58      }
59  
60      /**
61       * @since 3.2.0
62       */
63      public static Xpp3Dom build(Reader reader, boolean trim, InputLocationBuilder locationBuilder)
64              throws XmlPullParserException, IOException {
65          try (Reader closeMe = reader) {
66              return new Xpp3Dom(XmlNodeBuilder.build(
67                      reader, trim, locationBuilder != null ? locationBuilder::toInputLocation : null));
68          }
69      }
70  
71      public static Xpp3Dom build(XmlPullParser parser) throws XmlPullParserException, IOException {
72          return build(parser, DEFAULT_TRIM);
73      }
74  
75      public static Xpp3Dom build(XmlPullParser parser, boolean trim) throws XmlPullParserException, IOException {
76          return build(parser, trim, null);
77      }
78  
79      /**
80       * @since 3.2.0
81       */
82      public static Xpp3Dom build(XmlPullParser parser, boolean trim, InputLocationBuilder locationBuilder)
83              throws XmlPullParserException, IOException {
84          return new Xpp3Dom(
85                  XmlNodeBuilder.build(parser, trim, locationBuilder != null ? locationBuilder::toInputLocation : null));
86      }
87  
88      /**
89       * Input location builder interface, to be implemented to choose how to store data.
90       *
91       * @since 3.2.0
92       */
93      public interface InputLocationBuilder {
94          Object toInputLocation(XmlPullParser parser);
95      }
96  }