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.internal.xml;
20  
21  import java.io.Reader;
22  import org.codehaus.plexus.util.xml.pull.MXParser;
23  import org.codehaus.plexus.util.xml.pull.XmlPullParser;
24  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
25  
26  public class DomBuilder {
27  
28      public static Xpp3Dom build(Reader reader) throws MavenXmlException {
29          return build(reader, true);
30      }
31  
32      public static Xpp3Dom build(Reader reader, boolean trim) throws MavenXmlException {
33          try {
34              MXParser parser = new MXParser();
35              parser.setInput(reader);
36              return build(parser, trim);
37          } catch (XmlPullParserException e) {
38              throw new MavenXmlException("Unable to build DOM", e);
39          }
40      }
41  
42      public static Xpp3Dom build(XmlPullParser parser) {
43          return build(parser, true, null);
44      }
45  
46      public static Xpp3Dom build(XmlPullParser parser, boolean trim) {
47          return build(parser, trim, null);
48      }
49  
50      public static Xpp3Dom build(XmlPullParser parser, boolean trim, LocationBuilder locationBuilder) {
51          try {
52              Xpp3DomBuilder.InputLocationBuilder ilb =
53                      locationBuilder != null ? (p -> locationBuilder.getLocation()) : null;
54              return Xpp3DomBuilder.build(parser, trim, ilb);
55          } catch (Exception e) {
56              throw new MavenXmlException("Unable to build DOM", e);
57          }
58      }
59  
60      public static class LocationBuilder {
61  
62          private final Object location;
63  
64          public LocationBuilder(Object location) {
65              this.location = location;
66          }
67  
68          public Object getLocation() {
69              return location;
70          }
71      }
72  }