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.plugin.descriptor.PluginDescriptor;
34  import org.apache.maven.api.services.xml.PluginXmlFactory;
35  import org.apache.maven.api.services.xml.XmlReaderException;
36  import org.apache.maven.api.services.xml.XmlReaderRequest;
37  import org.apache.maven.api.services.xml.XmlWriterException;
38  import org.apache.maven.api.services.xml.XmlWriterRequest;
39  import org.apache.maven.plugin.descriptor.io.PluginDescriptorStaxReader;
40  import org.apache.maven.plugin.descriptor.io.PluginDescriptorStaxWriter;
41  
42  import static org.apache.maven.internal.impl.StaxLocation.getLocation;
43  import static org.apache.maven.internal.impl.StaxLocation.getMessage;
44  import static org.apache.maven.internal.impl.Utils.nonNull;
45  
46  @Named
47  @Singleton
48  public class DefaultPluginXmlFactory implements PluginXmlFactory {
49      @Override
50      public PluginDescriptor read(@Nonnull XmlReaderRequest request) throws XmlReaderException {
51          nonNull(request, "request");
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              PluginDescriptorStaxReader xml = new PluginDescriptorStaxReader();
61              xml.setAddDefaultEntities(request.isAddDefaultEntities());
62              if (inputStream != null) {
63                  return xml.read(inputStream, request.isStrict());
64              } else if (reader != null) {
65                  return xml.read(reader, request.isStrict());
66              } else if (path != null) {
67                  try (InputStream is = Files.newInputStream(path)) {
68                      return xml.read(is, request.isStrict());
69                  }
70              } else {
71                  try (InputStream is = url.openStream()) {
72                      return xml.read(is, request.isStrict());
73                  }
74              }
75          } catch (Exception e) {
76              throw new XmlReaderException("Unable to read plugin: " + getMessage(e), getLocation(e), e);
77          }
78      }
79  
80      @Override
81      public void write(XmlWriterRequest<PluginDescriptor> request) throws XmlWriterException {
82          nonNull(request, "request");
83          PluginDescriptor content = nonNull(request.getContent(), "content");
84          Path path = request.getPath();
85          OutputStream outputStream = request.getOutputStream();
86          Writer writer = request.getWriter();
87          if (writer == null && outputStream == null && path == null) {
88              throw new IllegalArgumentException("writer, outputStream or path must be non null");
89          }
90          try {
91              if (writer != null) {
92                  new PluginDescriptorStaxWriter().write(writer, content);
93              } else if (outputStream != null) {
94                  new PluginDescriptorStaxWriter().write(outputStream, content);
95              } else {
96                  try (OutputStream os = Files.newOutputStream(path)) {
97                      new PluginDescriptorStaxWriter().write(outputStream, content);
98                  }
99              }
100         } catch (Exception e) {
101             throw new XmlWriterException("Unable to write plugin: " + getMessage(e), getLocation(e), e);
102         }
103     }
104 
105     /**
106      * Simply parse the given xml string.
107      *
108      * @param xml the input xml string
109      * @return the parsed object
110      * @throws XmlReaderException if an error occurs during the parsing
111      * @see #toXmlString(Object)
112      */
113     public static PluginDescriptor fromXml(@Nonnull String xml) throws XmlReaderException {
114         return new DefaultPluginXmlFactory().fromXmlString(xml);
115     }
116 
117     /**
118      * Simply converts the given content to an xml string.
119      *
120      * @param content the object to convert
121      * @return the xml string representation
122      * @throws XmlWriterException if an error occurs during the transformation
123      * @see #fromXmlString(String)
124      */
125     public static String toXml(@Nonnull PluginDescriptor content) throws XmlWriterException {
126         return new DefaultPluginXmlFactory().toXmlString(content);
127     }
128 }