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.internal.impl;
20  
21  import java.io.InputStream;
22  import java.io.OutputStream;
23  import java.io.Reader;
24  import java.io.Writer;
25  import java.net.URL;
26  import java.nio.file.Files;
27  import java.nio.file.Path;
28  import java.util.function.Function;
29  
30  import org.apache.maven.api.annotations.Nonnull;
31  import org.apache.maven.api.di.Named;
32  import org.apache.maven.api.di.Singleton;
33  import org.apache.maven.api.model.InputSource;
34  import org.apache.maven.api.model.Model;
35  import org.apache.maven.api.services.xml.ModelXmlFactory;
36  import org.apache.maven.api.services.xml.XmlReaderException;
37  import org.apache.maven.api.services.xml.XmlReaderRequest;
38  import org.apache.maven.api.services.xml.XmlWriterException;
39  import org.apache.maven.api.services.xml.XmlWriterRequest;
40  import org.apache.maven.model.v4.MavenStaxReader;
41  import org.apache.maven.model.v4.MavenStaxWriter;
42  
43  import static org.apache.maven.internal.impl.StaxLocation.getLocation;
44  import static org.apache.maven.internal.impl.StaxLocation.getMessage;
45  import static org.apache.maven.internal.impl.Utils.nonNull;
46  
47  @Named
48  @Singleton
49  public class DefaultModelXmlFactory implements ModelXmlFactory {
50      @Override
51      public Model read(@Nonnull XmlReaderRequest request) throws XmlReaderException {
52          nonNull(request, "request");
53          Path path = request.getPath();
54          URL url = request.getURL();
55          Reader reader = request.getReader();
56          InputStream inputStream = request.getInputStream();
57          if (path == null && url == null && reader == null && inputStream == null) {
58              throw new IllegalArgumentException("path, url, reader or inputStream must be non null");
59          }
60          try {
61              InputSource source = null;
62              if (request.getModelId() != null || request.getLocation() != null) {
63                  source = new InputSource(
64                          request.getModelId(), path != null ? path.toUri().toString() : null);
65              }
66              MavenStaxReader xml = new MavenStaxReader();
67              xml.setAddDefaultEntities(request.isAddDefaultEntities());
68              if (inputStream != null) {
69                  return xml.read(inputStream, request.isStrict(), source);
70              } else if (reader != null) {
71                  return xml.read(reader, request.isStrict(), source);
72              } else if (path != null) {
73                  try (InputStream is = Files.newInputStream(path)) {
74                      return xml.read(is, request.isStrict(), source);
75                  }
76              } else {
77                  try (InputStream is = url.openStream()) {
78                      return xml.read(is, request.isStrict(), source);
79                  }
80              }
81          } catch (Exception e) {
82              throw new XmlReaderException("Unable to read model: " + getMessage(e), getLocation(e), e);
83          }
84      }
85  
86      @Override
87      public void write(XmlWriterRequest<Model> request) throws XmlWriterException {
88          nonNull(request, "request");
89          Model content = nonNull(request.getContent(), "content");
90          Path path = request.getPath();
91          OutputStream outputStream = request.getOutputStream();
92          Writer writer = request.getWriter();
93          Function<Object, String> inputLocationFormatter = request.getInputLocationFormatter();
94          if (writer == null && outputStream == null && path == null) {
95              throw new IllegalArgumentException("writer, outputStream or path must be non null");
96          }
97          try {
98              MavenStaxWriter w = new MavenStaxWriter();
99              if (inputLocationFormatter != null) {
100                 w.setStringFormatter((Function) inputLocationFormatter);
101             }
102             if (writer != null) {
103                 w.write(writer, content);
104             } else if (outputStream != null) {
105                 w.write(outputStream, content);
106             } else {
107                 try (OutputStream os = Files.newOutputStream(path)) {
108                     w.write(outputStream, content);
109                 }
110             }
111         } catch (Exception e) {
112             throw new XmlWriterException("Unable to write model: " + getMessage(e), getLocation(e), e);
113         }
114     }
115 
116     /**
117      * Simply parse the given xml string.
118      *
119      * @param xml the input xml string
120      * @return the parsed object
121      * @throws XmlReaderException if an error occurs during the parsing
122      * @see #toXmlString(Object)
123      */
124     public static Model fromXml(@Nonnull String xml) throws XmlReaderException {
125         return new DefaultModelXmlFactory().fromXmlString(xml);
126     }
127 
128     /**
129      * Simply converts the given content to an xml string.
130      *
131      * @param content the object to convert
132      * @return the xml string representation
133      * @throws XmlWriterException if an error occurs during the transformation
134      * @see #fromXmlString(String)
135      */
136     public static String toXml(@Nonnull Model content) throws XmlWriterException {
137         return new DefaultModelXmlFactory().toXmlString(content);
138     }
139 }