1 package org.apache.maven.model.io.xpp3;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.Reader;
25
26 import org.apache.maven.model.InputSource;
27 import org.apache.maven.model.Model;
28 import org.codehaus.plexus.util.ReaderFactory;
29 import org.codehaus.plexus.util.xml.pull.EntityReplacementMap;
30 import org.codehaus.plexus.util.xml.pull.MXParser;
31 import org.codehaus.plexus.util.xml.pull.XmlPullParser;
32 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
33
34 public class MavenXpp3ReaderEx
35 {
36 private boolean addDefaultEntities = true;
37
38 private final ContentTransformer contentTransformer;
39
40 public MavenXpp3ReaderEx()
41 {
42 this( ( source, fieldName ) -> source );
43 }
44
45 public MavenXpp3ReaderEx( ContentTransformer contentTransformer )
46 {
47 this.contentTransformer = contentTransformer;
48 }
49
50 /**
51 * Returns the state of the "add default entities" flag.
52 *
53 * @return boolean
54 */
55 public boolean getAddDefaultEntities()
56 {
57 return addDefaultEntities;
58 } //-- boolean getAddDefaultEntities()
59
60 /**
61 * @see ReaderFactory#newXmlReader
62 *
63 * @param reader a reader object.
64 * @param strict a strict object.
65 * @throws IOException IOException if any.
66 * @throws XmlPullParserException XmlPullParserException if
67 * any.
68 * @return Model
69 */
70 public Model read( Reader reader, boolean strict, InputSource source )
71 throws IOException, XmlPullParserException
72 {
73 XmlPullParser parser = addDefaultEntities
74 ? new MXParser( EntityReplacementMap.defaultEntityReplacementMap ) : new MXParser( );
75 parser.setInput( reader );
76 return read( parser, strict, source );
77 } //-- Model read( Reader, boolean )
78
79 /**
80 * @see ReaderFactory#newXmlReader
81 *
82 * @param reader a reader object.
83 * @throws IOException IOException if any.
84 * @throws XmlPullParserException XmlPullParserException if
85 * any.
86 * @return Model
87 */
88 public Model read( Reader reader, InputSource source )
89 throws IOException, XmlPullParserException
90 {
91 return read( reader, true, source );
92 } //-- Model read( Reader )
93
94 /**
95 * Method read.
96 *
97 * @param in a in object.
98 * @param strict a strict object.
99 * @throws IOException IOException if any.
100 * @throws XmlPullParserException XmlPullParserException if
101 * any.
102 * @return Model
103 */
104 public Model read( InputStream in, boolean strict, InputSource source )
105 throws IOException, XmlPullParserException
106 {
107 return read( ReaderFactory.newXmlReader( in ), strict, source );
108 } //-- Model read( InputStream, boolean )
109
110 /**
111 * Method read.
112 *
113 * @param in a in object.
114 * @throws IOException IOException if any.
115 * @throws XmlPullParserException XmlPullParserException if
116 * any.
117 * @return Model
118 */
119 public Model read( InputStream in, InputSource source )
120 throws IOException, XmlPullParserException
121 {
122 return read( ReaderFactory.newXmlReader( in ), source );
123 } //-- Model read( InputStream )
124
125 /**
126 * Method read.
127 *
128 * @param parser a parser object.
129 * @param strict a strict object.
130 * @throws IOException IOException if any.
131 * @throws XmlPullParserException XmlPullParserException if
132 * any.
133 * @return Model
134 */
135 public Model read( XmlPullParser parser, boolean strict, InputSource source )
136 throws IOException, XmlPullParserException
137 {
138 org.apache.maven.model.v4.MavenXpp3ReaderEx reader = contentTransformer != null
139 ? new org.apache.maven.model.v4.MavenXpp3ReaderEx( contentTransformer::transform )
140 : new org.apache.maven.model.v4.MavenXpp3ReaderEx();
141 reader.setAddDefaultEntities( addDefaultEntities );
142 org.apache.maven.api.model.Model model = reader.read( parser, strict,
143 new org.apache.maven.api.model.InputSource( source.getModelId(), source.getLocation() ) );
144 return new Model( model );
145 }
146
147 /**
148 * Sets the state of the "add default entities" flag.
149 *
150 * @param addDefaultEntities a addDefaultEntities object.
151 */
152 public void setAddDefaultEntities( boolean addDefaultEntities )
153 {
154 this.addDefaultEntities = addDefaultEntities;
155 } //-- void setAddDefaultEntities( boolean )
156
157 public interface ContentTransformer
158 {
159 /**
160 * Interpolate the value read from the xpp3 document
161 * @param source The source value
162 * @param fieldName A description of the field being interpolated. The implementation may use this to
163 * log stuff.
164 * @return The interpolated value.
165 */
166 String transform( String source, String fieldName );
167 }
168
169 }