Author: vgritsenko Date: Thu Apr 21 05:09:38 2005 New Revision: 164042 URL: http://svn.apache.org/viewcvs?rev=164042&view=rev Log: public static empty attributes should be an immutable object. use single FACTORY. add serialize(XMLizable) method.
Added: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/xml/ImmutableAttributesImpl.java (with props) Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/xml/XMLUtils.java Added: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/xml/ImmutableAttributesImpl.java URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/xml/ImmutableAttributesImpl.java?rev=164042&view=auto ============================================================================== --- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/xml/ImmutableAttributesImpl.java (added) +++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/xml/ImmutableAttributesImpl.java Thu Apr 21 05:09:38 2005 @@ -0,0 +1,74 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cocoon.xml; + +import org.xml.sax.Attributes; + +/** + * Immutable attributes + * + * @version $Id$ + */ +public class ImmutableAttributesImpl extends AttributesImpl { + + public ImmutableAttributesImpl() { + super(); + } + + public ImmutableAttributesImpl(Attributes attrs) { + super(attrs); + } + + public void clear() { + throw new UnsupportedOperationException("immutable attributes"); + } + + public void removeAttribute(int index) { + throw new UnsupportedOperationException("immutable attributes"); + } + + public void setLocalName(int index, String localName) { + throw new UnsupportedOperationException("immutable attributes"); + } + + public void setQName(int index, String qName) { + throw new UnsupportedOperationException("immutable attributes"); + } + + public void setType(int index, String type) { + throw new UnsupportedOperationException("immutable attributes"); + } + + public void setURI(int index, String uri) { + throw new UnsupportedOperationException("immutable attributes"); + } + + public void setValue(int index, String value) { + throw new UnsupportedOperationException("immutable attributes"); + } + + public void setAttributes(Attributes atts) { + throw new UnsupportedOperationException("immutable attributes"); + } + + public void setAttribute(int index, String uri, String localName, String qName, String type, String value) { + throw new UnsupportedOperationException("immutable attributes"); + } + + public void addAttribute(String uri, String localName, String qName, String type, String value) { + throw new UnsupportedOperationException("immutable attributes"); + } +} Propchange: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/xml/ImmutableAttributesImpl.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/xml/ImmutableAttributesImpl.java ------------------------------------------------------------------------------ svn:keywords = Id Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/xml/XMLUtils.java URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/xml/XMLUtils.java?rev=164042&r1=164041&r2=164042&view=diff ============================================================================== --- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/xml/XMLUtils.java (original) +++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/xml/XMLUtils.java Thu Apr 21 05:09:38 2005 @@ -1,12 +1,12 @@ /* - * Copyright 1999-2004 The Apache Software Foundation. - * + * Copyright 1999-2005 The Apache Software Foundation. + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -44,19 +44,23 @@ * @author <a href="mailto:[EMAIL PROTECTED]">Nicola Ken Barozzi</a> * @author <a href="mailto:[EMAIL PROTECTED]">Sylvain Wallez</a> * @author <a href="mailto:[EMAIL PROTECTED]">Carsten Ziegeler</a> - * @version CVS $Id$ + * @version $Id$ */ public class XMLUtils { - public static final AttributesImpl EMPTY_ATTRIBUTES = new AttributesImpl(); + /** + * Empty attributes immutable object. + */ + public static final Attributes EMPTY_ATTRIBUTES = new ImmutableAttributesImpl(); + private static final SAXTransformerFactory FACTORY = (SAXTransformerFactory) TransformerFactory.newInstance(); private static final Properties XML_FORMAT = createDefaultPropertiesForXML(false); private static final Properties XML_FORMAT_NODECL = createDefaultPropertiesForXML(true); // FIXME: parent parameter not used anymore // Using parent because some dom implementations like jtidy are bugged, - //cannot get parent or delete child + // cannot get parent or delete child public static void stripDuplicateAttributes(Node node, Node parent) { // The output depends on the type of the node @@ -212,7 +216,7 @@ public static Properties createPropertiesForXML(boolean omitXMLDeclaration) { return new Properties(omitXMLDeclaration? XML_FORMAT_NODECL: XML_FORMAT); } - + /** * Serialize a DOM node into a string using format created by * <code>createPropertiesForXML(false)</code>. @@ -239,27 +243,59 @@ if (node == null) { return ""; } + StringWriter writer = new StringWriter(); TransformerHandler transformerHandler; - transformerHandler = ((SAXTransformerFactory)TransformerFactory.newInstance()).newTransformerHandler(); + transformerHandler = FACTORY.newTransformerHandler(); transformerHandler.getTransformer().setOutputProperties(format); transformerHandler.setResult(new StreamResult(writer)); if (node.getNodeType() != Node.DOCUMENT_NODE) { transformerHandler.startDocument(); - } + } DOMStreamer domStreamer = new DOMStreamer(transformerHandler, transformerHandler); domStreamer.stream(node); if (node.getNodeType() != Node.DOCUMENT_NODE) { transformerHandler.endDocument(); - } + } + + return writer.toString(); + } catch (javax.xml.transform.TransformerException e) { + throw new ProcessingException("TransformerException: " + e, e); + } catch (SAXException e) { + throw new ProcessingException("SAXException while streaming DOM node to SAX: " + e, e); + } + } + + /** + * Serialize a XMLizable into a string. + * If the object is null the empty string is returned. + * + * @param format The format of the output to be used by SAX transformer. + * @see OutputKeys + */ + public static String serialize(org.apache.excalibur.xml.sax.XMLizable xml, Properties format) + throws ProcessingException { + + try { + if (xml == null) { + return ""; + } + + StringWriter writer = new StringWriter(); + TransformerHandler transformerHandler; + transformerHandler = FACTORY.newTransformerHandler(); + transformerHandler.getTransformer().setOutputProperties(format); + transformerHandler.setResult(new StreamResult(writer)); + transformerHandler.startDocument(); + xml.toSAX(new EmbeddedXMLPipe(transformerHandler)); + transformerHandler.endDocument(); + return writer.toString(); - } catch (javax.xml.transform.TransformerException local) { - throw new ProcessingException("TransformerException: " + local, - local); - } catch (SAXException local) { - throw new ProcessingException("SAXException while streaming DOM node to SAX: " + local, - local); - } + } catch (javax.xml.transform.TransformerException e) { + throw new ProcessingException("TransformerException: " + e, e); + } catch (SAXException e) { + throw new ProcessingException("SAXException while streaming DOM node to SAX: " + e, e); + } } /** @@ -516,7 +552,7 @@ data(contentHandler, stringValue); endElement(contentHandler, namespaceURI, localName); } - + /** * Create a start and endElement * @@ -538,7 +574,7 @@ startElement(contentHandler, namespaceURI, localName, atts); endElement(contentHandler, namespaceURI, localName); } - + /** * Create a start and endElement with a empty Namespace * The content of the Element is set to the stringValue parameter @@ -564,8 +600,8 @@ data(contentHandler, stringValue); endElement(contentHandler, namespaceURI, localName); } - - + + /** * Create endElement with empty Namespace * @@ -581,7 +617,7 @@ contentHandler.endElement("", localName, localName); } - + /** * Create endElement * Prefix must be mapped to empty String @@ -599,7 +635,7 @@ contentHandler.endElement(namespaceURI, localName, localName); } - + /** * Create a startElement with a empty Namespace and without Attributes *