001: /*
002: * Copyright (c) 1998-2007 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Adam Megacz
028: */
029:
030: package com.caucho.jaxb.property;
031:
032: import javax.xml.XMLConstants;
033: import javax.xml.bind.JAXBException;
034: import javax.xml.namespace.QName;
035:
036: import javax.xml.transform.Source;
037: import javax.xml.transform.Transformer;
038: import javax.xml.transform.TransformerException;
039: import javax.xml.transform.TransformerFactory;
040: import javax.xml.transform.dom.DOMResult;
041: import javax.xml.transform.dom.DOMSource;
042: import javax.xml.transform.sax.SAXSource;
043: import javax.xml.transform.stream.StreamResult;
044: import javax.xml.transform.stream.StreamSource;
045:
046: import java.io.ByteArrayInputStream;
047: import java.io.ByteArrayOutputStream;
048: import java.io.CharArrayWriter;
049: import java.io.FileNotFoundException;
050: import java.io.InputStream;
051: import java.io.IOException;
052: import java.io.OutputStream;
053: import java.io.Reader;
054: import java.io.StringReader;
055:
056: import org.xml.sax.*;
057: import org.xml.sax.helpers.*;
058:
059: import com.caucho.util.Attachment;
060: import com.caucho.util.Base64;
061:
062: /**
063: * a Source Property
064: */
065: public class SourceProperty extends CDataProperty implements
066: AttachmentProperty {
067: public static final QName SCHEMA_TYPE = new QName(
068: XMLConstants.W3C_XML_SCHEMA_NS_URI, "base64Binary", "xsd");
069:
070: public static final SourceProperty PROPERTY = new SourceProperty();
071:
072: private final Transformer _transformer;
073:
074: private SourceProperty() {
075: try {
076: TransformerFactory factory = TransformerFactory
077: .newInstance();
078: _transformer = factory.newTransformer();
079: } catch (TransformerException e) {
080: throw new RuntimeException(e);
081: }
082: }
083:
084: public String write(Object in) throws IOException, JAXBException {
085: Source src = (Source) in;
086:
087: try {
088: ByteArrayOutputStream out = new ByteArrayOutputStream();
089:
090: _transformer.transform(src, new StreamResult(out));
091:
092: return Base64.encodeFromByteArray(out.toByteArray());
093: } catch (TransformerException e) {
094: if (src instanceof StreamSource) {
095: StreamSource ss = (StreamSource) src;
096:
097: InputStream is = ss.getInputStream();
098:
099: if (is != null) {
100: ByteArrayOutputStream out = new ByteArrayOutputStream();
101:
102: for (int ch = is.read(); ch >= 0; ch = is.read())
103: out.write(ch);
104:
105: return Base64
106: .encodeFromByteArray(out.toByteArray());
107: }
108:
109: Reader reader = ss.getReader();
110:
111: if (reader != null) {
112: CharArrayWriter out = new CharArrayWriter();
113:
114: for (int ch = reader.read(); ch >= 0; ch = reader
115: .read())
116: out.write(ch);
117:
118: return Base64.encode(new String(out.toCharArray()));
119: }
120: }
121:
122: throw new JAXBException(e);
123: }
124: }
125:
126: protected Object read(String in) {
127: byte[] bytes = Base64.decodeToByteArray(in);
128: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
129:
130: return new StreamSource(bais);
131: }
132:
133: public QName getSchemaType() {
134: return SCHEMA_TYPE;
135: }
136:
137: public String getMimeType(Object obj) {
138: return "text/xml";
139: }
140:
141: public void writeAsAttachment(Object obj, OutputStream out)
142: throws IOException {
143: try {
144: if (obj instanceof StreamSource) {
145: StreamSource source = (StreamSource) obj;
146: InputSource inputSource = null;
147:
148: InputStream is = source.getInputStream();
149: Reader reader = source.getReader();
150: String systemId = source.getSystemId();
151:
152: if (is != null)
153: inputSource = new InputSource(is);
154:
155: else if (reader != null)
156: inputSource = new InputSource(reader);
157:
158: else if (systemId != null)
159: inputSource = new InputSource(systemId);
160:
161: XMLReader xmlReader = XMLReaderFactory
162: .createXMLReader();
163: xmlReader.setEntityResolver(_entityResolver);
164:
165: SAXSource saxSource = new SAXSource(xmlReader,
166: inputSource);
167: _transformer
168: .transform(saxSource, new StreamResult(out));
169: } else
170: _transformer.transform((Source) obj, new StreamResult(
171: out));
172: } catch (TransformerException e) {
173: IOException ioe = new IOException();
174: ioe.initCause(e);
175:
176: throw ioe;
177: } catch (SAXException saxe) {
178: IOException ioe = new IOException();
179: ioe.initCause(saxe);
180:
181: throw ioe;
182: }
183: }
184:
185: public Object readFromAttachment(Attachment attachment)
186: throws IOException {
187: byte[] contents = attachment.getRawContents();
188:
189: // try to give a DOMSource, but if that doesn't work,
190: // just give a simple StreamSource
191: try {
192: DOMResult result = new DOMResult();
193: InputStream is = new ByteArrayInputStream(contents);
194:
195: _transformer.transform(new StreamSource(is), result);
196:
197: return new DOMSource(result.getNode());
198: } catch (TransformerException e) {
199: InputStream is = new ByteArrayInputStream(contents);
200:
201: return new StreamSource(is);
202: }
203: }
204:
205: private static final EntityResolver _entityResolver = new EntityResolver() {
206: public InputSource resolveEntity(String publicId,
207: String systemId) {
208: return new InputSource(new StringReader(""));
209: }
210: };
211: }
|