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