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.codehaus.plexus.util.xml;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.Reader;
24  
25  import org.apache.maven.internal.xml.XmlNodeBuilder;
26  import org.codehaus.plexus.util.xml.pull.XmlPullParser;
27  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
28  
29  /**
30   * @version $Id$
31   */
32  public class Xpp3DomBuilder {
33      private static final boolean DEFAULT_TRIM = true;
34  
35      public static Xpp3Dom build(Reader reader) throws XmlPullParserException, IOException {
36          return build(reader, null);
37      }
38  
39      /**
40       * @since 3.2.0
41       */
42      public static Xpp3Dom build(Reader reader, InputLocationBuilder locationBuilder)
43              throws XmlPullParserException, IOException {
44          return build(reader, DEFAULT_TRIM, locationBuilder);
45      }
46  
47      public static Xpp3Dom build(InputStream is, String encoding) throws XmlPullParserException, IOException {
48          return build(is, encoding, DEFAULT_TRIM);
49      }
50  
51      public static Xpp3Dom build(InputStream is, String encoding, boolean trim)
52              throws XmlPullParserException, IOException {
53          try (InputStream closeMe = is) {
54              return new Xpp3Dom(XmlNodeBuilder.build(is, encoding, trim));
55          }
56      }
57  
58      public static Xpp3Dom build(Reader reader, boolean trim) throws XmlPullParserException, IOException {
59          return build(reader, trim, null);
60      }
61  
62      /**
63       * @since 3.2.0
64       */
65      public static Xpp3Dom build(Reader reader, boolean trim, InputLocationBuilder locationBuilder)
66              throws XmlPullParserException, IOException {
67          try (Reader closeMe = reader) {
68              return new Xpp3Dom(XmlNodeBuilder.build(
69                      reader, trim, locationBuilder != null ? locationBuilder::toInputLocation : null));
70          }
71      }
72  
73      public static Xpp3Dom build(XmlPullParser parser) throws XmlPullParserException, IOException {
74          return build(parser, DEFAULT_TRIM);
75      }
76  
77      public static Xpp3Dom build(XmlPullParser parser, boolean trim) throws XmlPullParserException, IOException {
78          return build(parser, trim, null);
79      }
80  
81      /**
82       * @since 3.2.0
83       */
84      public static Xpp3Dom build(XmlPullParser parser, boolean trim, InputLocationBuilder locationBuilder)
85              throws XmlPullParserException, IOException {
86          return new Xpp3Dom(
87                  XmlNodeBuilder.build(parser, trim, locationBuilder != null ? locationBuilder::toInputLocation : null));
88      }
89  
90      /**
91       * Input location builder interface, to be implemented to choose how to store data.
92       *
93       * @since 3.2.0
94       */
95      public interface InputLocationBuilder {
96          Object toInputLocation(XmlPullParser parser);
97      }
98  }