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