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