001package com.hammurapi.convert; 002 003import java.io.ByteArrayInputStream; 004import java.io.ByteArrayOutputStream; 005import java.io.File; 006import java.io.FileInputStream; 007import java.io.IOException; 008import java.io.InputStream; 009import java.io.Reader; 010import java.io.StringReader; 011import java.io.StringWriter; 012 013import javax.xml.parsers.DocumentBuilder; 014import javax.xml.parsers.DocumentBuilderFactory; 015import javax.xml.stream.XMLEventReader; 016import javax.xml.stream.XMLInputFactory; 017import javax.xml.transform.OutputKeys; 018import javax.xml.transform.Transformer; 019import javax.xml.transform.TransformerFactory; 020import javax.xml.transform.dom.DOMSource; 021import javax.xml.transform.stream.StreamResult; 022 023import org.w3c.dom.Document; 024import org.w3c.dom.Node; 025 026import com.hammurapi.common.Pumper; 027 028/** 029 * Intuitive conversions. 030 * @author Pavel 031 * 032 */ 033public class ReflectiveAtomicConvertersBundle extends AbstractReflectiveAtomicConvertersBundle { 034 035 @ConverterMethod 036 public InputStream byteArrayToInputStream(byte[] ba) { 037 return new ByteArrayInputStream(ba); 038 } 039 040 @ConverterMethod 041 public byte[] inputStreamToByteArray(InputStream in) throws IOException { 042 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 043 Pumper.pump(in, baos, true); 044 return baos.toByteArray(); 045 } 046 047 @ConverterMethod 048 public byte[] fileToByteArray(File in) throws IOException { 049 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 050 Pumper.pump(new FileInputStream(in), baos, true); 051 return baos.toByteArray(); 052 } 053 054 @ConverterMethod 055 public Document parseXml(InputStream in) throws Exception { 056 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 057 DocumentBuilder builder = dbf.newDocumentBuilder(); 058 return builder.parse(in); 059 } 060 061 @ConverterMethod 062 public Document parseXml(File in) throws Exception { 063 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 064 DocumentBuilder builder = dbf.newDocumentBuilder(); 065 return builder.parse(in); 066 } 067 068 @ConverterMethod 069 public Document parseXml(byte[] in) throws Exception { 070 return parseXml(new ByteArrayInputStream(in)); 071 } 072 073 074 @ConverterMethod 075 public Document parseXml(String str) throws Exception { 076 return parseXml(str.getBytes()); 077 } 078 079 @ConverterMethod 080 public String serialize(Node node) throws Exception { 081 DOMSource src = new DOMSource(node); 082 StringWriter sw = new StringWriter(); 083 StreamResult result = new StreamResult(sw); 084 TransformerFactory tf = TransformerFactory.newInstance(); 085 Transformer serializer = tf.newTransformer(); 086 serializer.setOutputProperty(OutputKeys.INDENT,"yes"); 087 serializer.transform(src, result); 088 sw.close(); 089 return sw.toString(); 090 } 091 092 @ConverterMethod 093 public XMLEventReader inputStreamToXmlEventReader(InputStream in) throws Exception { 094 XMLInputFactory xif = XMLInputFactory.newInstance(); 095 return xif.createXMLEventReader(in); 096 } 097 098 @ConverterMethod 099 public XMLEventReader readerToXmlEventReader(Reader in) throws Exception { 100 XMLInputFactory xif = XMLInputFactory.newInstance(); 101 return xif.createXMLEventReader(in); 102 } 103 104 @ConverterMethod 105 public XMLEventReader byteArrayToXmlEventReader(byte[] in) throws Exception { 106 XMLInputFactory xif = XMLInputFactory.newInstance(); 107 return xif.createXMLEventReader(new ByteArrayInputStream(in)); 108 } 109 110 @ConverterMethod 111 public XMLEventReader fileToXmlEventReader(File in) throws Exception { 112 XMLInputFactory xif = XMLInputFactory.newInstance(); 113 return xif.createXMLEventReader(new FileInputStream(in)); 114 } 115 116 @ConverterMethod 117 public XMLEventReader stringToXmlEventReader(String in) throws Exception { 118 XMLInputFactory xif = XMLInputFactory.newInstance(); 119 return xif.createXMLEventReader(new StringReader(in)); 120 } 121 122}