1
2
3
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
17
18
19
20 final class ClasspathInput implements LSInput {
21
22
23
24
25 private transient String publicid;
26
27
28
29
30 private transient String systemid;
31
32
33
34
35 private transient String baseuri;
36
37
38
39
40 private transient String encoding;
41
42
43
44
45 private transient boolean certified;
46
47
48
49
50
51
52 ClasspathInput(final String pubid, final String sysid) {
53 this(pubid, sysid, null, null);
54 }
55
56
57
58
59
60
61
62
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
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
185
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 }