Return-Path: Delivered-To: apmail-xml-cocoon-cvs-archive@xml.apache.org Received: (qmail 75613 invoked by uid 500); 11 Jun 2003 00:11:16 -0000 Mailing-List: contact cocoon-cvs-help@xml.apache.org; run by ezmlm Precedence: bulk Reply-To: cocoon-dev@xml.apache.org list-help: list-unsubscribe: list-post: Delivered-To: mailing list cocoon-cvs@xml.apache.org Received: (qmail 75602 invoked by uid 500); 11 Jun 2003 00:11:16 -0000 Delivered-To: apmail-cocoon-2.1-cvs@apache.org Received: (qmail 75598 invoked from network); 11 Jun 2003 00:11:16 -0000 Received: from icarus.apache.org (208.185.179.13) by daedalus.apache.org with SMTP; 11 Jun 2003 00:11:16 -0000 Received: (qmail 61959 invoked by uid 1672); 11 Jun 2003 00:11:15 -0000 Date: 11 Jun 2003 00:11:15 -0000 Message-ID: <20030611001115.61958.qmail@icarus.apache.org> From: joerg@apache.org To: cocoon-2.1-cvs@apache.org Subject: cvs commit: cocoon-2.1/src/java/org/apache/cocoon/serialization AbstractTextSerializer.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N joerg 2003/06/10 17:11:15 Modified: src/java/org/apache/cocoon/serialization AbstractTextSerializer.java Log: not more than simple code formatting Revision Changes Path 1.5 +76 -85 cocoon-2.1/src/java/org/apache/cocoon/serialization/AbstractTextSerializer.java Index: AbstractTextSerializer.java =================================================================== RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/serialization/AbstractTextSerializer.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- AbstractTextSerializer.java 11 Apr 2003 08:38:26 -0000 1.4 +++ AbstractTextSerializer.java 11 Jun 2003 00:11:15 -0000 1.5 @@ -66,8 +66,10 @@ import org.xml.sax.SAXException; import org.xml.sax.ext.LexicalHandler; import org.xml.sax.helpers.AttributesImpl; + import javax.xml.transform.OutputKeys; import javax.xml.transform.TransformerFactory; +import javax.xml.transform.TransformerException; import javax.xml.transform.sax.SAXTransformerFactory; import javax.xml.transform.sax.TransformerHandler; import javax.xml.transform.stream.StreamResult; @@ -87,9 +89,8 @@ * @author Sylvain Wallez * @version CVS $Id$ */ -public abstract class AbstractTextSerializer -extends AbstractSerializer -implements Configurable, CacheableProcessingComponent { +public abstract class AbstractTextSerializer extends AbstractSerializer + implements Configurable, CacheableProcessingComponent { /** * The trax TransformerFactory used by this serializer. @@ -161,16 +162,14 @@ /** * Helper for TransformerFactory. */ - protected SAXTransformerFactory getTransformerFactory() - { + protected SAXTransformerFactory getTransformerFactory() { return tfactory; } /** * Helper for TransformerHandler. */ - protected TransformerHandler getTransformerHandler() - throws javax.xml.transform.TransformerException { + protected TransformerHandler getTransformerHandler() throws TransformerException { return this.getTransformerFactory().newTransformerHandler(); } @@ -178,32 +177,30 @@ * Set the {@link OutputStream} where the requested resource should * be serialized. */ - public void setOutputStream(OutputStream out) - throws IOException { + public void setOutputStream(OutputStream out) throws IOException { /* * Add a level of buffering to the output stream. Xalan serializes * every character individually. In conjunction with chunked * transfer encoding this would otherwise lead to a whopping 6-fold * increase of data on the wire. */ - // if (outputBufferSize > 0) { - // super.setOutputStream( - // new BufferedOutputStream(out, outputBufferSize)); - // } else { - super.setOutputStream(out); - // } + // if (outputBufferSize > 0) { + // super.setOutputStream( + // new BufferedOutputStream(out, outputBufferSize)); + // } else { + super.setOutputStream(out); + // } } /** * Set the configurations for this serializer. */ - public void configure(Configuration conf) - throws ConfigurationException { + public void configure(Configuration conf) throws ConfigurationException { // configure buffer size - // Configuration bsc = conf.getChild("buffer-size", false); - // if(null != bsc) - // outputBufferSize = bsc.getValueAsInteger(DEFAULT_BUFFER_SIZE); + // Configuration bsc = conf.getChild("buffer-size", false); + // if(null != bsc) + // outputBufferSize = bsc.getValueAsInteger(DEFAULT_BUFFER_SIZE); // configure xalan Configuration cdataSectionElements = conf.getChild("cdata-section-elements"); @@ -217,53 +214,53 @@ Configuration standAlone = conf.getChild("standalone"); Configuration version = conf.getChild("version"); - if (! cdataSectionElements.getLocation().equals("-")) { - format.put(OutputKeys.CDATA_SECTION_ELEMENTS,cdataSectionElements.getValue()); + if (!cdataSectionElements.getLocation().equals("-")) { + format.put(OutputKeys.CDATA_SECTION_ELEMENTS, cdataSectionElements.getValue()); } - if (! dtPublic.getLocation().equals("-")) { - format.put(OutputKeys.DOCTYPE_PUBLIC,dtPublic.getValue()); + if (!dtPublic.getLocation().equals("-")) { + format.put(OutputKeys.DOCTYPE_PUBLIC, dtPublic.getValue()); } - if (! dtSystem.getLocation().equals("-")) { - format.put(OutputKeys.DOCTYPE_SYSTEM,dtSystem.getValue()); + if (!dtSystem.getLocation().equals("-")) { + format.put(OutputKeys.DOCTYPE_SYSTEM, dtSystem.getValue()); } - if (! encoding.getLocation().equals("-")) { - format.put(OutputKeys.ENCODING,encoding.getValue()); + if (!encoding.getLocation().equals("-")) { + format.put(OutputKeys.ENCODING, encoding.getValue()); } - if (! indent.getLocation().equals("-")) { - format.put(OutputKeys.INDENT,indent.getValue()); + if (!indent.getLocation().equals("-")) { + format.put(OutputKeys.INDENT, indent.getValue()); } - if (! mediaType.getLocation().equals("-")) { - format.put(OutputKeys.MEDIA_TYPE,mediaType.getValue()); + if (!mediaType.getLocation().equals("-")) { + format.put(OutputKeys.MEDIA_TYPE, mediaType.getValue()); } - if (! method.getLocation().equals("-")) { - format.put(OutputKeys.METHOD,method.getValue()); + if (!method.getLocation().equals("-")) { + format.put(OutputKeys.METHOD, method.getValue()); } - if (! omitXMLDeclaration.getLocation().equals("-")) { - format.put(OutputKeys.OMIT_XML_DECLARATION,omitXMLDeclaration.getValue()); + if (!omitXMLDeclaration.getLocation().equals("-")) { + format.put(OutputKeys.OMIT_XML_DECLARATION, omitXMLDeclaration.getValue()); } - if (! standAlone.getLocation().equals("-")) { - format.put(OutputKeys.STANDALONE,standAlone.getValue()); + if (!standAlone.getLocation().equals("-")) { + format.put(OutputKeys.STANDALONE, standAlone.getValue()); } - if (! version.getLocation().equals("-")) { - format.put(OutputKeys.VERSION,version.getValue()); + if (!version.getLocation().equals("-")) { + format.put(OutputKeys.VERSION, version.getValue()); } Configuration tFactoryConf = conf.getChild("transformer-factory", false); if (tFactoryConf != null) { String tFactoryClass = tFactoryConf.getValue(); try { - this.tfactory = (SAXTransformerFactory)ClassUtils.newInstance(tFactoryClass); - if (getLogger().isDebugEnabled()) { - getLogger().debug("Using transformer factory " + tFactoryClass); - } - } catch(Exception e) { - throw new ConfigurationException("Cannot load transformer factory " + tFactoryClass, e); + this.tfactory = (SAXTransformerFactory) ClassUtils.newInstance(tFactoryClass); + if (getLogger().isDebugEnabled()) { + getLogger().debug("Using transformer factory " + tFactoryClass); + } + } catch (Exception e) { + throw new ConfigurationException("Cannot load transformer factory " + tFactoryClass, e); } } else { - // Standard TrAX behaviour - this.tfactory = (SAXTransformerFactory)TransformerFactory.newInstance(); + // Standard TrAX behaviour + this.tfactory = (SAXTransformerFactory) TransformerFactory.newInstance(); } - + tfactory.setErrorListener(new TraxErrorHandler(getLogger())); // Check if we need namespace as attributes. @@ -273,7 +270,7 @@ this.namespacePipe = new NamespaceAsAttributes(); this.namespacePipe.enableLogging(getLogger()); } - } catch(Exception e) { + } catch (Exception e) { getLogger().warn("Cannot know if transformer needs namespaces attributes - assuming NO.", e); } } @@ -286,7 +283,6 @@ } } - /** * Generate the unique key. * This key must be unique inside the space of this component. @@ -323,10 +319,9 @@ SAXTransformerFactory factory = getTransformerFactory(); - Boolean cacheValue = (Boolean)needsNamespaceCache.get(factory.getClass().getName()); + Boolean cacheValue = (Boolean) needsNamespaceCache.get(factory.getClass().getName()); if (cacheValue != null) { return cacheValue.booleanValue(); - } else { // Serialize a minimal document to check how namespaces are handled. StringWriter writer = new StringWriter(); @@ -352,8 +347,7 @@ // Check if the namespace is there (replace " by ' to be sure of what we search in) boolean needsIt = (text.replace('"', '\'').indexOf(check) == -1); - String msg = needsIt ? " needs namespace attributes (will be slower)." : - " handles correctly namespaces."; + String msg = needsIt ? " needs namespace attributes (will be slower)." : " handles correctly namespaces."; getLogger().debug("Trax handler " + handler.getClass().getName() + msg); @@ -406,8 +400,7 @@ * Track mappings to be able to add xmlns: attributes * in startElement(). */ - public void startPrefixMapping(String prefix, String uri) - throws SAXException { + public void startPrefixMapping(String prefix, String uri) throws SAXException { // Store the mappings to reconstitute xmlns:attributes // except prefixes starting with "xml": these are reserved // VG: (uri != null) fixes NPE in startElement @@ -418,7 +411,7 @@ // append the prefix colon now, in order to save concatenations later, but // only for non-empty prefixes. - if(prefix.length() > 0) { + if (prefix.length() > 0) { this.uriToPrefixMap.put(uri, prefix + ":"); } else { this.uriToPrefixMap.put(uri, prefix); @@ -436,12 +429,12 @@ * ignores start/endPrefixMapping(). */ public void startElement(String eltUri, String eltLocalName, String eltQName, Attributes attrs) - throws SAXException { + throws SAXException { // try to restore the qName. The map already contains the colon - if (null != eltUri && eltUri.length() != 0 && this.uriToPrefixMap.containsKey(eltUri)) - eltQName = (String) this.uriToPrefixMap.get(eltUri) + eltLocalName; - + if (null != eltUri && eltUri.length() != 0 && this.uriToPrefixMap.containsKey(eltUri)) { + eltQName = (String) this.uriToPrefixMap.get(eltUri) + eltLocalName; + } if (this.hasMappings) { // Add xmlns* attributes where needed @@ -451,11 +444,11 @@ int mappingCount = this.prefixList.size(); int attrCount = attrs.getLength(); - for(int mapping = 0; mapping < mappingCount; mapping++) { + for (int mapping = 0; mapping < mappingCount; mapping++) { // Build infos for this namespace - String uri = (String)this.uriList.get(mapping); - String prefix = (String)this.prefixList.get(mapping); + String uri = (String) this.uriList.get(mapping); + String prefix = (String) this.prefixList.get(mapping); String qName = prefix.equals("") ? "xmlns" : ("xmlns:" + prefix); // Search for the corresponding xmlns* attribute @@ -463,8 +456,9 @@ for (int attr = 0; attr < attrCount; attr++) { if (qName.equals(attrs.getQName(attr))) { // Check if mapping and attribute URI match - if (! uri.equals(attrs.getValue(attr))) { - getLogger().error("URI in prefix mapping and attribute do not match : '" + uri + "' - '" + attrs.getURI(attr) + "'"); + if (!uri.equals(attrs.getValue(attr))) { + getLogger().error("URI in prefix mapping and attribute do not match : '" + + uri + "' - '" + attrs.getURI(attr) + "'"); throw new SAXException("URI in prefix mapping and attribute do not match"); } found = true; @@ -477,10 +471,11 @@ if (newAttrs == null) { // Need to test if attrs is empty or we go into an infinite loop... // Well know SAX bug which I spent 3 hours to remind of :-( - if (attrCount == 0) + if (attrCount == 0) { newAttrs = new AttributesImpl(); - else + } else { newAttrs = new AttributesImpl(attrs); + } } if (prefix.equals("")) { @@ -496,8 +491,7 @@ // Start element with new attributes, if any super.startElement(eltUri, eltLocalName, eltQName, newAttrs == null ? attrs : newAttrs); - } - else { + } else { // Normal job super.startElement(eltUri, eltLocalName, eltQName, attrs); } @@ -508,12 +502,11 @@ * Receive notification of the end of an element. * Try to restore the element qName. */ - public void endElement(String eltUri, String eltLocalName, String eltQName) - throws SAXException { + public void endElement(String eltUri, String eltLocalName, String eltQName) throws SAXException { // try to restore the qName. The map already contains the colon - if(null != eltUri && eltUri.length() != 0 && this.uriToPrefixMap.containsKey(eltUri) ) - eltQName = (String) this.uriToPrefixMap.get(eltUri) + eltLocalName; - + if (null != eltUri && eltUri.length() != 0 && this.uriToPrefixMap.containsKey(eltUri)) { + eltQName = (String) this.uriToPrefixMap.get(eltUri) + eltLocalName; + } super.endElement(eltUri, eltLocalName, eltQName); } @@ -521,14 +514,13 @@ * End the scope of a prefix-URI mapping: * remove entry from mapping tables. */ - public void endPrefixMapping(String prefix) - throws SAXException { + public void endPrefixMapping(String prefix) throws SAXException { // remove mappings for xalan-bug-workaround. // Unfortunately, we're not passed the uri, but the prefix here, // so we need to maintain maps in both directions. - if(this.prefixToUriMap.containsKey(prefix)) { - this.uriToPrefixMap.remove((String) this.prefixToUriMap.get(prefix)); - this.prefixToUriMap.remove(prefix); + if (this.prefixToUriMap.containsKey(prefix)) { + this.uriToPrefixMap.remove(this.prefixToUriMap.get(prefix)); + this.prefixToUriMap.remove(prefix); } if (hasMappings) { @@ -559,8 +551,7 @@ super.endDocument(); } - private void clearMappings() - { + private void clearMappings() { this.hasMappings = false; this.prefixList.clear(); this.uriList.clear(); @@ -578,7 +569,7 @@ // this.output.flush(); // } catch (IOException ignored) { // } - // } + // } } }