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