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