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