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