org.apache.cocoon.components.source
abstract public class: AbstractStreamSource [javadoc |
source]
java.lang.Object
org.apache.avalon.framework.logger.AbstractLogEnabled
org.apache.cocoon.components.source.AbstractStreamSource
All Implemented Interfaces:
ModifiableSource
Direct Known Subclasses:
AbstractStreamWriteableSource, URLSource, BlobSource, FileSource
Deprecated! Use - the new Avalon Excalibur Source Resolving
This abstract class provides convenience methods to implement
a stream based Source. Implement getInputStream(), getSystemId() and
optionally override refresh(), recycle(), getLastModified() and
getContentLength() to obtain a valid Source implementation.
This base implementation provides services to parse HTML sources
(HTML is not valid XML) using JTidy, if present. The source is
considered to contain HTML if isHTMLContent() returns
true.
- author:
< - a href="mailto:sylvain@apache.org">Sylvain Wallez
- author:
< - a href="mailto:cziegeler@apache.org">Carsten Ziegeler
- version:
CVS - $Id: AbstractStreamSource.java 433543 2006-08-22 06:22:54Z crossley $
| Field Summary |
|---|
| public static TransformerFactory | transformerFactory | The TrAX factory for serializing xml |
| protected ComponentManager | manager | The ComponentManager needed for streaming |
| Method from org.apache.cocoon.components.source.AbstractStreamSource Detail: |
public boolean exists() {
try {
InputStream stream = getInputStream();
stream.close();
return true;
} catch(Exception e) {
return false;
}
} Deprecated!Returns true if getInputStream() succeeds.
Subclasses can provide a more efficient implementation. |
public long getContentLength() {
return -1;
} Deprecated!Override this method to set the Content Length |
public InputSource getInputSource() throws IOException, ProcessingException {
InputStream stream = this.getInputStream();
if (jtidyClass != null && isHTMLContent()) {
try {
final Object xhtmlconvert = jtidyClass.newInstance();
Method m = jtidyClass.getMethod("setXmlOut", new Class[] { Class.forName("java.lang.Boolean")});
m.invoke(xhtmlconvert, new Object[] { Boolean.TRUE });
m = jtidyClass.getMethod("setXHTML", new Class[] {Class.forName("java.lang.Boolean")});
m.invoke(xhtmlconvert, new Object[] { Boolean.TRUE });
m = jtidyClass.getMethod("setShowWarnings", new Class[] { Class.forName("java.lang.Boolean")});
m.invoke(xhtmlconvert, new Object[] { Boolean.FALSE });
m = jtidyClass.getMethod("parseDOM", new Class[] { Class.forName("java.io.InputStream"), Class.forName("java.io.OutputStream")});
final Document doc = (Document)m.invoke(xhtmlconvert, new Object[] { stream, null });
final StringWriter writer = new StringWriter();
final Transformer transformer;
transformer = transformerFactory.newTransformer();
transformer.setOutputProperties(xmlProperties);
transformer.transform(new DOMSource(doc), new StreamResult(writer));
final String xmlstring = writer.toString();
InputSource newObject = new InputSource(new java.io.StringReader(xmlstring));
newObject.setSystemId(this.getSystemId());
return newObject;
} catch (Exception ignore) {
// Let someone else worry about what we got . This is as before.
this.refresh();
stream = this.getInputStream();
}
}
InputSource newObject = new InputSource(stream);
newObject.setSystemId(this.getSystemId());
return newObject;
} Deprecated!Return a new InputSource object |
public long getLastModified() {
return 0;
} Deprecated!Override this method to set the Last Modification date |
protected boolean isHTMLContent() {
return false;
} Deprecated! |
public void recycle() {
} Deprecated!To be overriden in concrete subclasses if needed. |
public void refresh() {
} Deprecated!To be overriden in concrete subclasses if needed. |
public void toSAX(ContentHandler handler) throws SAXException {
SAXParser parser = null;
try {
parser = (SAXParser)this.manager.lookup(SAXParser.ROLE);
parser.parse( this.getInputSource(), handler);
} catch (SAXException e) {
// Preserve original exception
throw e;
} catch (Exception e){
throw new SAXException("Exception during processing of "
+ this.getSystemId(), e);
} finally {
if (parser != null) this.manager.release( (Component)parser);
}
} Deprecated!Stream content to a content handler or to an XMLConsumer. |