View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2012-2026 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.jcabi.xml;
6   
7   import java.io.InputStream;
8   import java.io.Reader;
9   import java.nio.charset.Charset;
10  import java.nio.charset.StandardCharsets;
11  import org.cactoos.io.ResourceOf;
12  import org.cactoos.text.TextOf;
13  import org.w3c.dom.ls.LSInput;
14  
15  /**
16   * {@link LSInput} implementation used by {@link ClasspathResolver}.
17   *
18   * @since 0.1
19   */
20  final class ClasspathInput implements LSInput {
21  
22      /**
23       * Public Id.
24       */
25      private transient String publicid;
26  
27      /**
28       * System Id.
29       */
30      private transient String systemid;
31  
32      /**
33       * Base URI.
34       */
35      private transient String baseuri;
36  
37      /**
38       * Encoding to use when reading the resource.
39       */
40      private transient String encoding;
41  
42      /**
43       * Whether the input source is certified.
44       */
45      private transient boolean certified;
46  
47      /**
48       * Constructor.
49       * @param pubid Public id
50       * @param sysid System id
51       */
52      ClasspathInput(final String pubid, final String sysid) {
53          this(pubid, sysid, null, null);
54      }
55  
56      /**
57       * Constructor.
58       * @param pubid Public id
59       * @param sysid System id
60       * @param enc Encoding to use when reading the resource
61       * @param base Base URI
62       * @checkstyle ParameterNumberCheck (3 lines)
63       */
64      ClasspathInput(final String pubid, final String sysid,
65          final String enc, final String base) {
66          this.publicid = pubid;
67          this.systemid = sysid;
68          this.encoding = enc;
69          this.baseuri = base;
70      }
71  
72      @Override
73      public String getPublicId() {
74          return this.publicid;
75      }
76  
77      @Override
78      public void setPublicId(final String pubid) {
79          this.publicid = pubid;
80      }
81  
82      @Override
83      public String getSystemId() {
84          return this.systemid;
85      }
86  
87      @Override
88      public void setSystemId(final String sysid) {
89          this.systemid = sysid;
90      }
91  
92      @Override
93      public String getBaseURI() {
94          return this.baseuri;
95      }
96  
97      @Override
98      public InputStream getByteStream() {
99          return null;
100     }
101 
102     @Override
103     public boolean getCertifiedText() {
104         return this.certified;
105     }
106 
107     @Override
108     public Reader getCharacterStream() {
109         return null;
110     }
111 
112     @Override
113     public String getEncoding() {
114         return this.encoding;
115     }
116 
117     @SuppressWarnings({"PMD.AvoidCatchingGenericException", "PMD.ExceptionAsFlowControl"})
118     @Override
119     public String getStringData() {
120         try {
121             return new TextOf(
122                 new ResourceOf(
123                     this.systemid,
124                     path -> {
125                         throw new IllegalArgumentException(
126                             String.format(
127                                 "SystemID \"%s\" resource does not exist or can't be opened.",
128                                 path
129                             )
130                         );
131                     }
132                 ),
133                 this.charset()
134             ).asString();
135             // @checkstyle IllegalCatchCheck (1 line)
136         } catch (final Exception ex) {
137             throw new IllegalArgumentException(
138                 String.format(
139                     "Unable to read input stream of SystemID \"%s\"",
140                     this.systemid
141                 ),
142                 ex
143             );
144         }
145     }
146 
147     @Override
148     public void setBaseURI(final String base) {
149         this.baseuri = base;
150     }
151 
152     @Override
153     public void setByteStream(final InputStream bytestream) {
154         throw new UnsupportedOperationException(
155             "ClasspathInput resolves data from the classpath; setByteStream() is not supported"
156         );
157     }
158 
159     @Override
160     public void setCertifiedText(final boolean text) {
161         this.certified = text;
162     }
163 
164     @Override
165     public void setCharacterStream(final Reader stream) {
166         throw new UnsupportedOperationException(
167             "ClasspathInput resolves data from the classpath; setCharacterStream() is not supported"
168         );
169     }
170 
171     @Override
172     public void setEncoding(final String enc) {
173         this.encoding = enc;
174     }
175 
176     @Override
177     public void setStringData(final String data) {
178         throw new UnsupportedOperationException(
179             "ClasspathInput resolves data from the classpath; setStringData() is not supported"
180         );
181     }
182 
183     /**
184      * Charset to use when reading the resource.
185      * @return Charset, or UTF-8 if no encoding was set
186      */
187     private Charset charset() {
188         final Charset cset;
189         if (this.encoding == null) {
190             cset = StandardCharsets.UTF_8;
191         } else {
192             cset = Charset.forName(this.encoding);
193         }
194         return cset;
195     }
196 }