-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathSourceHandle.java
More file actions
298 lines (276 loc) · 8.42 KB
/
SourceHandle.java
File metadata and controls
298 lines (276 loc) · 8.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
* Copyright © 2024 MarkLogic Corporation. All Rights Reserved.
*/
package com.marklogic.client.io;
import com.marklogic.client.MarkLogicIOException;
import com.marklogic.client.impl.XmlFactories;
import com.marklogic.client.io.marker.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.*;
import java.nio.charset.StandardCharsets;
/**
* <p>A Source Handle represents XML content as a transform source for reading
* or transforms a source into a result for writing.</p>
*
* <p>Always call {@link #close} when finished with this handle to release the resources.</p>
*/
public class SourceHandle
extends BaseHandle<InputStream, OutputStreamSender>
implements OutputStreamSender, StreamingContentHandle<Source, InputStream>,
XMLReadHandle, XMLWriteHandle,
StructureReadHandle, StructureWriteHandle, CtsQueryWriteHandle,
Closeable
{
static final private Logger logger = LoggerFactory.getLogger(SourceHandle.class);
private Transformer transformer;
private Source content;
private InputStream underlyingStream;
/**
* Creates a factory to create a SourceHandle instance for a Transformer Source.
* @return the factory
*/
static public ContentHandleFactory newFactory() {
return new ContentHandleFactory() {
@Override
public Class<?>[] getHandledClasses() {
return new Class<?>[]{ Source.class };
}
@Override
public boolean isHandled(Class<?> type) {
return Source.class.isAssignableFrom(type);
}
@Override
public <C> ContentHandle<C> newHandle(Class<C> type) {
@SuppressWarnings("unchecked")
ContentHandle<C> handle = isHandled(type) ?
(ContentHandle<C>) new SourceHandle() : null;
return handle;
}
};
}
/**
* Zero-argument constructor.
*/
public SourceHandle() {
super();
super.setFormat(Format.XML);
setResendable(false);
}
/**
* Initializes the handle with a transform source for the content.
* @param content a transform source
*/
public SourceHandle(Source content) {
this();
set(content);
}
/**
* Returns a transformer for modifying the content.
* @return the transformer
*/
public Transformer getTransformer() {
return transformer;
}
/**
* Specifies a transformer for modifying the content.
* @param transformer the transformer
*/
public void setTransformer(Transformer transformer) {
this.transformer = transformer;
}
/**
* Specifies a transformer for modifying the content and returns the handle
* as a fluent convenience.
* @param transformer the transformer
* @return this handle
*/
public SourceHandle withTransformer(Transformer transformer) {
setTransformer(transformer);
return this;
}
/**
* Returns the transform source that produces the content.
* @return the transform source
*/
@Override
public Source get() {
return content;
}
/**
* Assigns a transform source that produces the content.
* @param content the transform source
*/
@Override
public void set(Source content) {
this.content = content;
}
/**
* Assigns a transform source that produces the content and returns
* the handle as a fluent convenience.
* @param content the transform source
* @return this handle
*/
public SourceHandle with(Source content) {
set(content);
return this;
}
@Override
public Class<Source> getContentClass() {
return Source.class;
}
@Override
public SourceHandle newHandle() {
return new SourceHandle().withMimetype(getMimetype());
}
@Override
public SourceHandle[] newHandleArray(int length) {
if (length < 0) throw new IllegalArgumentException("array length less than zero: "+length);
return new SourceHandle[length];
}
@Override
public Source[] newArray(int length) {
if (length < 0) throw new IllegalArgumentException("array length less than zero: "+length);
return new Source[length];
}
/**
* Transforms the source for the content output to the result. If
* the transformer is not specified, an identity transform sends
* the source to the result. When writing, the result is stored
* in the database
* @param result the receiver of the transform output
*/
public void transform(Result result) {
if (logger.isInfoEnabled())
logger.info("Transforming source into result");
try {
if (content == null) {
throw new IllegalStateException("No source to transform");
}
Transformer transformer = null;
if (this.transformer != null) {
transformer = getTransformer();
} else {
if (logger.isWarnEnabled())
logger.warn("No transformer, so using identity transform");
transformer = makeTransformerFactory().newTransformer();
}
transformer.transform(content, result);
} catch (TransformerException e) {
logger.error("Failed to transform source into result",e);
throw new MarkLogicIOException(e);
}
}
private TransformerFactory makeTransformerFactory() {
return XmlFactories.makeNewTransformerFactory();
}
/**
* Restricts the format to XML.
*/
@Override
public void setFormat(Format format) {
if (format != Format.XML)
throw new IllegalArgumentException("SourceHandle supports the XML format only");
}
/**
* Specifies the mime type of the content and returns the handle
* as a fluent convenience.
* @param mimetype the mime type of the content
* @return this handle
*/
public SourceHandle withMimetype(String mimetype) {
setMimetype(mimetype);
return this;
}
@Override
public void fromBuffer(byte[] buffer) {
if (buffer == null || buffer.length == 0)
content = null;
else
receiveContent(new ByteArrayInputStream(buffer));
}
@Override
public byte[] toBuffer() {
try {
if (content == null)
return null;
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
write(buffer);
byte[] b = buffer.toByteArray();
fromBuffer(b);
return b;
} catch (IOException e) {
throw new MarkLogicIOException(e);
}
}
@Override
public Source toContent(InputStream serialization) {
return (serialization == null) ? null :
new StreamSource(new InputStreamReader(serialization, StandardCharsets.UTF_8));
}
@Override
public Source bytesToContent(byte[] buffer) {
return (buffer == null || buffer.length == 0) ? null :
toContent(new ByteArrayInputStream(buffer));
}
@Override
public byte[] contentToBytes(Source content) {
if (content == null) return null;
try {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
makeTransformerFactory().newTransformer().transform(content,
new StreamResult(new OutputStreamWriter(buffer, StandardCharsets.UTF_8)));
return buffer.toByteArray();
} catch (TransformerException e) {
throw new MarkLogicIOException("Could not convert Source to byte[] array", e);
}
}
/**
* Buffers the transform source and returns the buffer as a string.
*/
@Override
public String toString() {
byte[] buffer = toBuffer();
return (buffer == null) ? null : new String(buffer, StandardCharsets.UTF_8);
}
@Override
protected Class<InputStream> receiveAs() {
return InputStream.class;
}
@Override
protected void receiveContent(InputStream content) {
if (content == null) {
this.content = null;
return;
}
this.underlyingStream = content;
this.content = new StreamSource(new InputStreamReader(content, StandardCharsets.UTF_8));
}
@Override
protected OutputStreamSender sendContent() {
if (content == null) {
throw new IllegalStateException("No source to transform to result for writing");
}
return this;
}
@Override
public void write(OutputStream out) throws IOException {
transform(new StreamResult(new OutputStreamWriter(out, StandardCharsets.UTF_8)));
}
/** Always call close() when finished with this handle -- it closes the underlying InputStream.
*/
@Override
public void close() {
if ( underlyingStream != null ) {
try {
underlyingStream.close();
} catch (IOException e) {
logger.error("Failed to close underlying InputStream",e);
throw new MarkLogicIOException(e);
}
}
}
}