You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by vg...@apache.org on 2003/05/07 01:42:21 UTC
cvs commit: cocoon-2.1/src/blocks/batik/java/org/apache/cocoon/xml/dom SVGBuilder.java
vgritsenko 2003/05/06 16:42:21
Modified: src/blocks/batik/java/org/apache/cocoon/serialization
SVGSerializer.java
src/blocks/batik/java/org/apache/cocoon/xml/dom
SVGBuilder.java
Log:
Remove buffering from SVGSerializer. Use pipelines buffering by returning true
in shouldSetContentLength. Remove dead code.
Revision Changes Path
1.4 +41 -73 cocoon-2.1/src/blocks/batik/java/org/apache/cocoon/serialization/SVGSerializer.java
Index: SVGSerializer.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/blocks/batik/java/org/apache/cocoon/serialization/SVGSerializer.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- SVGSerializer.java 24 Mar 2003 14:33:57 -0000 1.3
+++ SVGSerializer.java 6 May 2003 23:42:21 -0000 1.4
@@ -75,17 +75,14 @@
import org.apache.cocoon.components.url.ParsedContextURLProtocolHandler;
import org.apache.cocoon.components.url.ParsedResourceURLProtocolHandler;
import org.apache.cocoon.util.ClassUtils;
-import org.apache.cocoon.xml.XMLConsumer;
import org.apache.cocoon.xml.dom.SVGBuilder;
import org.apache.excalibur.source.SourceValidity;
import org.apache.excalibur.source.impl.validity.NOPValidity;
import org.w3c.dom.Document;
-import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
-import org.xml.sax.ext.LexicalHandler;
/**
- * A Batik based Serializer for generating PNG/JPEG images
+ * A <a href="http://xml.apache.org/batik/">Batik</a> based Serializer for generating PNG/JPEG images
*
* @author <a href="mailto:dims@yahoo.com">Davanum Srinivas</a>
* @author <a href="mailto:rossb@apache.org">Ross Burton</a>
@@ -104,18 +101,15 @@
ParsedURL.registerHandler(new ParsedResourceURLProtocolHandler());
}
- /** The <code>ContentHandler</code> receiving SAX events. */
- private ContentHandler contentHandler=null;
- /** The <code>LexicalHandler</code> receiving SAX events. */
- private LexicalHandler lexicalHandler=null;
- /** The component manager instance */
- private ComponentManager manager=null;
/** The current <code>OutputStream</code>. */
- private OutputStream output=null;
+ private OutputStream output;
+
/** The current <code>mime-type</code>. */
- private String mimetype = null;
+ private String mimetype;
+
/** The current <code>Transcoder</code>. */
- Transcoder transcoder = null;
+ Transcoder transcoder;
+
/** The Transcoder Factory to use */
TranscoderFactory factory = ExtendableTranscoderFactory.getTranscoderFactoryImplementation();
@@ -131,11 +125,14 @@
*/
public void configure(Configuration conf) throws ConfigurationException {
this.mimetype = conf.getAttribute("mime-type");
- log.debug("SVGSerializer mime-type:" + mimetype);
+ if (getLogger().isDebugEnabled()) {
+ getLogger().debug("mime-type: " + mimetype);
+ }
// Using the Transcoder Factory, get the default transcoder
// for this MIME type.
this.transcoder = factory.createTranscoder(mimetype);
+
// Iterate through the parameters, looking for a transcoder reference
Configuration[] parameters = conf.getChildren("parameter");
for (int i = 0; i < parameters.length; i++) {
@@ -145,7 +142,9 @@
try {
this.transcoder = (Transcoder)ClassUtils.newInstance(transcoderName);
} catch (Exception ex) {
- log.error("Cannot load class " + transcoderName, ex);
+ if (getLogger().isDebugEnabled()) {
+ getLogger().debug("Cannot load class " + transcoderName, ex);
+ }
throw new ConfigurationException("Cannot load class " + transcoderName, ex);
}
}
@@ -164,7 +163,10 @@
String name = parameters[i].getAttribute("name");
// Skip over the parameters we've dealt with. Ensure this
// is kept in sync with the above list!
- if ("transcoder".equals(name)) continue;
+ if ("transcoder".equals(name)) {
+ continue;
+ }
+
// Now try and get the hints out
try {
// Turn it into a key name (assume the current Batik style continues!
@@ -184,18 +186,18 @@
// Can throw an exception.
value = new Boolean(parameters[i].getAttributeAsBoolean("value"));
} else if ("COLOR".equals(keyType)) {
- // Can throw an exception
- String stringValue = parameters[i].getAttribute("value");
- if (stringValue.startsWith("#")) {
- stringValue = stringValue.substring(1);
- }
- value = new Color(Integer.parseInt(stringValue, 16));
+ // Can throw an exception
+ String stringValue = parameters[i].getAttribute("value");
+ if (stringValue.startsWith("#")) {
+ stringValue = stringValue.substring(1);
+ }
+ value = new Color(Integer.parseInt(stringValue, 16));
} else {
// Assume String, and get the value. Allow an empty string.
value = parameters[i].getValue("");
}
- if(log.isDebugEnabled()) {
- log.debug("SVG Serializer: adding hint \"" + name + "\" with value \"" + value.toString() + "\"");
+ if(getLogger().isDebugEnabled()) {
+ getLogger().debug("Adding hint \"" + name + "\" with value \"" + value.toString() + "\"");
}
transcoder.addTranscodingHint(key, value);
} catch (ClassCastException ex) {
@@ -216,42 +218,6 @@
* <code>Composable</code>.
*/
public void compose(ComponentManager manager) {
- this.manager = manager;
- }
-
- /**
- * Set the <code>XMLConsumer</code> that will receive XML data.
- * <br>
- * This method will simply call <code>setContentHandler(consumer)</code>
- * and <code>setLexicalHandler(consumer)</code>.
- */
- public void setConsumer(XMLConsumer consumer) {
- this.contentHandler=consumer;
- this.lexicalHandler=consumer;
- }
-
- /**
- * Set the <code>ContentHandler</code> that will receive XML data.
- * <br>
- * Subclasses may retrieve this <code>ContentHandler</code> instance
- * accessing the protected <code>super.contentHandler</code> field.
- */
- public void setContentHandler(ContentHandler content) {
- this.contentHandler=content;
- }
-
- /**
- * Set the <code>LexicalHandler</code> that will receive XML data.
- * <br>
- * Subclasses may retrieve this <code>LexicalHandler</code> instance
- * accessing the protected <code>super.lexicalHandler</code> field.
- *
- * @exception IllegalStateException If the <code>LexicalHandler</code> or
- * the <code>XMLConsumer</code> were
- * already set.
- */
- public void setLexicalHandler(LexicalHandler lexical) {
- this.lexicalHandler=lexical;
}
/**
@@ -261,17 +227,14 @@
try {
TranscoderInput transInput = new TranscoderInput(doc);
- // Batik's PNGTranscoder closes the output stream.
- // Therefore we cannot pass it this.output directly.
- // Otherwise we get an exception when Cocoon.process
- // tries to flush/close the stream again.
- ByteArrayOutputStream baos = new ByteArrayOutputStream(8192);
- TranscoderOutput transOutput = new TranscoderOutput(baos);
+ // Buffering is done by the pipeline (See shouldSetContentLength)
+ TranscoderOutput transOutput = new TranscoderOutput(this.output);
transcoder.transcode(transInput, transOutput);
- baos.writeTo(this.output);
} catch (Exception ex) {
- log.error("SVGSerializer: Exception writing image", ex);
- throw new SAXException("Exception writing image ", ex);
+ if (getLogger().isDebugEnabled()) {
+ getLogger().debug("Got exception writing image, rethrowing", ex);
+ }
+ throw new SAXException("Exception writing image", ex);
}
}
@@ -307,10 +270,15 @@
}
/**
- * Test if the component wants to set the content length
+ * Returns true so the pipeline implementation will buffer generated
+ * output and write content length to the response.
+ * <p>Batik's PNGTranscoder closes the output stream, therefore we
+ * cannot pass {@link #output} directly to Batik and have to
+ * instruct pipeline to buffer it. If we do not buffer, we would get
+ * an exception when {@link org.apache.cocoon.Cocoon#process}
+ * tries to close the stream.
*/
public boolean shouldSetContentLength() {
- return false;
+ return true;
}
-
}
1.3 +17 -14 cocoon-2.1/src/blocks/batik/java/org/apache/cocoon/xml/dom/SVGBuilder.java
Index: SVGBuilder.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/blocks/batik/java/org/apache/cocoon/xml/dom/SVGBuilder.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- SVGBuilder.java 27 Apr 2003 14:48:32 -0000 1.2
+++ SVGBuilder.java 6 May 2003 23:42:21 -0000 1.3
@@ -92,11 +92,15 @@
}
}
+ protected Logger getLogger() {
+ return this.log;
+ }
+
/**
* Return the newly built Document.
*/
public Document getDocument() {
- return(this.document);
+ return this.document;
}
/**
@@ -104,8 +108,7 @@
*
* @exception SAXException If this method was not called appropriately.
*/
- public void startDocument()
- throws SAXException {
+ public void startDocument() throws SAXException {
try {
// Create SVG Document
String namespaceURI = SVGDOMImplementation.SVG_NAMESPACE_URI;
@@ -116,11 +119,12 @@
// it is not as general as tieing any prefix to svg namespace uri
namespaces.put("svg", SVGDOMImplementation.SVG_NAMESPACE_URI);
} catch (SAXException se) {
- log.error("SVGBuilder: startDocument", se);
throw se;
} catch (Exception ex){
- log.error("SVGBuilder: startDocument", ex);
- throw new SAXException("SVGBuilder: startDocument", ex);
+ if (getLogger().isDebugEnabled()) {
+ getLogger().debug("Got exception in startDocument, rethrowing", ex);
+ }
+ throw new SAXException("Exception in startDocument", ex);
}
}
@@ -129,8 +133,7 @@
*
* @exception SAXException If this method was not called appropriately.
*/
- public void endDocument ()
- throws SAXException {
+ public void endDocument() throws SAXException {
try {
super.endDocument();
@@ -139,18 +142,18 @@
this.notify(this.document);
} catch (SAXException se) {
- log.error("SVGBuilder: endDocument", se);
throw se;
} catch (Exception ex){
- log.error("SVGBuilder: endDocument", ex);
- throw new SAXException("SVGBuilder: endDocument", ex);
+ if (getLogger().isDebugEnabled()) {
+ getLogger().debug("Got exception in endDocument, rethrowing", ex);
+ }
+ throw new SAXException("Exception in endDocument", ex);
}
}
/**
* Receive notification of a successfully completed DOM tree generation.
*/
- protected void notify(Document doc)
- throws SAXException {
+ protected void notify(Document doc) throws SAXException {
}
}