View Javadoc
1   /*
2    * Copyright (c) 2012-2022, jcabi.com
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met: 1) Redistributions of source code must retain the above
8    * copyright notice, this list of conditions and the following
9    * disclaimer. 2) Redistributions in binary form must reproduce the above
10   * copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided
12   * with the distribution. 3) Neither the name of the jcabi.com nor
13   * the names of its contributors may be used to endorse or promote
14   * products derived from this software without specific prior written
15   * permission.
16   *
17   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28   * OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package com.jcabi.xml;
31  
32  import java.io.InputStream;
33  import java.io.Reader;
34  import java.nio.charset.StandardCharsets;
35  import org.cactoos.io.ResourceOf;
36  import org.cactoos.text.TextOf;
37  import org.w3c.dom.ls.LSInput;
38  
39  /**
40   * {@link LSInput} implementation used by {@link ClasspathResolver}.
41   *
42   * @since 0.1
43   */
44  final class ClasspathInput implements LSInput {
45  
46      /**
47       * Public Id.
48       */
49      private transient String publicid;
50  
51      /**
52       * System Id.
53       */
54      private transient String systemid;
55  
56      /**
57       * Constructor.
58       * @param pubid Public id
59       * @param sysid System id
60       */
61      ClasspathInput(final String pubid, final String sysid) {
62          this.publicid = pubid;
63          this.systemid = sysid;
64      }
65  
66      @Override
67      public String getPublicId() {
68          return this.publicid;
69      }
70  
71      @Override
72      public void setPublicId(final String pubid) {
73          this.publicid = pubid;
74      }
75  
76      @Override
77      public String getSystemId() {
78          return this.systemid;
79      }
80  
81      @Override
82      public void setSystemId(final String sysid) {
83          this.systemid = sysid;
84      }
85  
86      @Override
87      public String getBaseURI() {
88          return null;
89      }
90  
91      @Override
92      public InputStream getByteStream() {
93          return null;
94      }
95  
96      @Override
97      @SuppressWarnings("PMD.BooleanGetMethodName")
98      public boolean getCertifiedText() {
99          return false;
100     }
101 
102     @Override
103     public Reader getCharacterStream() {
104         return null;
105     }
106 
107     @Override
108     public String getEncoding() {
109         return null;
110     }
111 
112     @Override
113     public String getStringData() {
114         try {
115             return new TextOf(
116                 new ResourceOf(
117                     this.systemid,
118                     path -> {
119                         throw new IllegalArgumentException(
120                             String.format(
121                                 "SystemID \"%s\" resource does not exist or can't be opened.",
122                                 path
123                             )
124                         );
125                     }
126                 ),
127                 StandardCharsets.UTF_8
128             ).asString();
129             // @checkstyle IllegalCatchCheck (1 line)
130         } catch (final Exception ex) {
131             throw new IllegalArgumentException(
132                 String.format(
133                     "Unable to read input stream of SystemID \"%s\"",
134                     this.systemid
135                 ),
136                 ex
137             );
138         }
139     }
140 
141     @Override
142     public void setBaseURI(final String baseuri) {
143         // intentionally empty
144     }
145 
146     @Override
147     public void setByteStream(final InputStream bytestream) {
148         // intentionally empty
149     }
150 
151     @Override
152     public void setCertifiedText(final boolean certifiedtext) {
153         // intentionally empty
154     }
155 
156     @Override
157     public void setCharacterStream(final Reader characterstream) {
158         // intentionally empty
159     }
160 
161     @Override
162     public void setEncoding(final String encoding) {
163         // intentionally empty
164     }
165 
166     @Override
167     public void setStringData(final String stringdata) {
168         // intentionally empty
169     }
170 }