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.api.services.xml;
20  
21  import java.io.InputStream;
22  import java.io.OutputStream;
23  import java.io.Reader;
24  import java.io.StringReader;
25  import java.io.StringWriter;
26  import java.io.Writer;
27  import java.nio.file.Path;
28  
29  import org.apache.maven.api.Service;
30  import org.apache.maven.api.annotations.Experimental;
31  import org.apache.maven.api.annotations.Nonnull;
32  
33  /**
34   * Generic interface to read/write objects to/from XML.
35   *
36   * @param <T> the object type to read/write
37   * @since 4.0.0
38   */
39  @Experimental
40  public interface XmlFactory<T> extends Service {
41  
42      @Nonnull
43      default T read(@Nonnull Path path) throws XmlReaderException {
44          return read(path, true);
45      }
46  
47      @Nonnull
48      default T read(@Nonnull Path path, boolean strict) throws XmlReaderException {
49          return read(XmlReaderRequest.builder().path(path).strict(strict).build());
50      }
51  
52      @Nonnull
53      default T read(@Nonnull InputStream input) throws XmlReaderException {
54          return read(input, true);
55      }
56  
57      @Nonnull
58      default T read(@Nonnull InputStream input, boolean strict) throws XmlReaderException {
59          return read(XmlReaderRequest.builder().inputStream(input).strict(strict).build());
60      }
61  
62      @Nonnull
63      default T read(@Nonnull Reader reader) throws XmlReaderException {
64          return read(reader, true);
65      }
66  
67      @Nonnull
68      default T read(@Nonnull Reader reader, boolean strict) throws XmlReaderException {
69          return read(XmlReaderRequest.builder().reader(reader).strict(strict).build());
70      }
71  
72      @Nonnull
73      T read(@Nonnull XmlReaderRequest request) throws XmlReaderException;
74  
75      default void write(@Nonnull T content, @Nonnull Path path) throws XmlWriterException {
76          write(XmlWriterRequest.<T>builder().content(content).path(path).build());
77      }
78  
79      default void write(@Nonnull T content, @Nonnull OutputStream outputStream) throws XmlWriterException {
80          write(XmlWriterRequest.<T>builder()
81                  .content(content)
82                  .outputStream(outputStream)
83                  .build());
84      }
85  
86      default void write(@Nonnull T content, @Nonnull Writer writer) throws XmlWriterException {
87          write(XmlWriterRequest.<T>builder().content(content).writer(writer).build());
88      }
89  
90      void write(@Nonnull XmlWriterRequest<T> request) throws XmlWriterException;
91  
92      /**
93       * Simply parse the given xml string.
94       *
95       * @param xml the input xml string
96       * @return the parsed object
97       * @throws XmlReaderException if an error occurs during the parsing
98       * @see #toXmlString(Object)
99       */
100     @Nonnull
101     default T fromXmlString(@Nonnull String xml) throws XmlReaderException {
102         return read(new StringReader(xml));
103     }
104 
105     /**
106      * Simply converts the given content to an xml string.
107      *
108      * @param content the object to convert
109      * @return the xml string representation
110      * @throws XmlWriterException if an error occurs during the transformation
111      * @see #fromXmlString(String)
112      */
113     @Nonnull
114     default String toXmlString(@Nonnull T content) throws XmlWriterException {
115         StringWriter sw = new StringWriter();
116         write(content, sw);
117         return sw.toString();
118     }
119 }