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.io.xpp3;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.Reader;
24  import org.apache.maven.model.Model;
25  import org.codehaus.plexus.util.ReaderFactory;
26  import org.codehaus.plexus.util.xml.pull.EntityReplacementMap;
27  import org.codehaus.plexus.util.xml.pull.MXParser;
28  import org.codehaus.plexus.util.xml.pull.XmlPullParser;
29  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
30  
31  public class MavenXpp3Reader {
32      private boolean addDefaultEntities = true;
33  
34      private final ContentTransformer contentTransformer;
35  
36      public MavenXpp3Reader() {
37          this((source, fieldName) -> source);
38      }
39  
40      public MavenXpp3Reader(ContentTransformer contentTransformer) {
41          this.contentTransformer = contentTransformer;
42      }
43  
44      /**
45       * Returns the state of the "add default entities" flag.
46       *
47       * @return boolean
48       */
49      public boolean getAddDefaultEntities() {
50          return addDefaultEntities;
51      } // -- boolean getAddDefaultEntities()
52  
53      /**
54       * @see ReaderFactory#newXmlReader
55       *
56       * @param reader a reader object.
57       * @param strict a strict object.
58       * @throws IOException IOException if any.
59       * @throws XmlPullParserException XmlPullParserException if
60       * any.
61       * @return Model
62       */
63      public Model read(Reader reader, boolean strict) throws IOException, XmlPullParserException {
64          XmlPullParser parser =
65                  addDefaultEntities ? new MXParser(EntityReplacementMap.defaultEntityReplacementMap) : new MXParser();
66          parser.setInput(reader);
67          return read(parser, strict);
68      } // -- Model read( Reader, boolean )
69  
70      /**
71       * @see ReaderFactory#newXmlReader
72       *
73       * @param reader a reader object.
74       * @throws IOException IOException if any.
75       * @throws XmlPullParserException XmlPullParserException if
76       * any.
77       * @return Model
78       */
79      public Model read(Reader reader) throws IOException, XmlPullParserException {
80          return read(reader, true);
81      } // -- Model read( Reader )
82  
83      /**
84       * Method read.
85       *
86       * @param in a in object.
87       * @param strict a strict object.
88       * @throws IOException IOException if any.
89       * @throws XmlPullParserException XmlPullParserException if
90       * any.
91       * @return Model
92       */
93      public Model read(InputStream in, boolean strict) throws IOException, XmlPullParserException {
94          return read(ReaderFactory.newXmlReader(in), strict);
95      } // -- Model read( InputStream, boolean )
96  
97      /**
98       * Method read.
99       *
100      * @param in a in object.
101      * @throws IOException IOException if any.
102      * @throws XmlPullParserException XmlPullParserException if
103      * any.
104      * @return Model
105      */
106     public Model read(InputStream in) throws IOException, XmlPullParserException {
107         return read(ReaderFactory.newXmlReader(in));
108     } // -- Model read( InputStream )
109 
110     /**
111      * Method read.
112      *
113      * @param parser a parser object.
114      * @param strict a strict object.
115      * @throws IOException IOException if any.
116      * @throws XmlPullParserException XmlPullParserException if
117      * any.
118      * @return Model
119      */
120     public Model read(XmlPullParser parser, boolean strict) throws IOException, XmlPullParserException {
121         org.apache.maven.model.v4.MavenXpp3Reader reader = contentTransformer != null
122                 ? new org.apache.maven.model.v4.MavenXpp3Reader(contentTransformer::transform)
123                 : new org.apache.maven.model.v4.MavenXpp3Reader();
124         reader.setAddDefaultEntities(addDefaultEntities);
125         org.apache.maven.api.model.Model model = reader.read(parser, strict);
126         return new Model(model);
127     } // -- Model read( XmlPullParser, boolean )
128 
129     /**
130      * Sets the state of the "add default entities" flag.
131      *
132      * @param addDefaultEntities a addDefaultEntities object.
133      */
134     public void setAddDefaultEntities(boolean addDefaultEntities) {
135         this.addDefaultEntities = addDefaultEntities;
136     } // -- void setAddDefaultEntities( boolean )
137 
138     public interface ContentTransformer {
139         /**
140          * Interpolate the value read from the xpp3 document
141          * @param source The source value
142          * @param fieldName A description of the field being interpolated. The implementation may use this to
143          *                           log stuff.
144          * @return The interpolated value.
145          */
146         String transform(String source, String fieldName);
147     }
148 }