Project

General

Profile

1
package eu.dnetlib.enabling.tools; // NOPMD
2

    
3
import java.io.IOException;
4
import java.io.InputStream;
5
import java.io.StringWriter;
6

    
7
import javax.xml.parsers.ParserConfigurationException;
8
import javax.xml.transform.TransformerFactory;
9
import javax.xml.xpath.XPathExpressionException;
10

    
11
import org.apache.commons.io.IOUtils;
12
import org.xml.sax.SAXException;
13

    
14
/**
15
 * implements a possibly optimized OpaqueResource constructed from a stream containing the xml source.
16
 * 
17
 * <p>the optimization consists of postponing the parsing as possibly and parse the profile passing the stream
18
 * directly to the xml parser.<p>
19
 * 
20
 * <p>
21
 * TODO: implement the optimization, currenly converts to string and reuses StringOpaqueResource
22
 * </p>
23
 * 
24
 * @author marko
25
 *
26
 */
27
public class StreamOpaqueResource extends StringOpaqueResource {
28

    
29
	/**
30
	 * construct with an input stream.
31
	 * 
32
	 * @param source source stream of xml text
33
	 * @throws XPathExpressionException shouldn't happen
34
	 * @throws SAXException could happen
35
	 * @throws IOException could happen
36
	 * @throws ParserConfigurationException shouldn't happen
37
	 */
38
	public StreamOpaqueResource(final InputStream source, final TransformerFactory tf) throws XPathExpressionException, SAXException, IOException, ParserConfigurationException {
39
		super(stringify(source), tf);
40
	}
41

    
42
	/**
43
	 * helper method: reads a stream to a string.
44
	 * 
45
	 * @param source input stream
46
	 * @return string containing the whole stream content.
47
	 * @throws IOException could happen.
48
	 */
49
	private static String stringify(final InputStream source) throws IOException {
50
		final StringWriter writer = new StringWriter();
51
		IOUtils.copy(source, writer);
52
		return writer.getBuffer().toString();
53
	}
54
	
55
}
(15-15/17)