You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by cz...@apache.org on 2003/05/06 14:09:20 UTC
cvs commit: cocoon-2.1/src/java/org/apache/cocoon/xml AttributeTypes.java AttributesImpl.java XMLUtils.java
cziegeler 2003/05/06 05:09:20
Modified: src/java/org/apache/cocoon/xml XMLUtils.java
Added: src/java/org/apache/cocoon/xml AttributeTypes.java
AttributesImpl.java
Log:
Adding some more xml utility code
Revision Changes Path
1.3 +252 -1 cocoon-2.1/src/java/org/apache/cocoon/xml/XMLUtils.java
Index: XMLUtils.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/xml/XMLUtils.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- XMLUtils.java 12 Mar 2003 15:11:15 -0000 1.2
+++ XMLUtils.java 6 May 2003 12:09:20 -0000 1.3
@@ -68,6 +68,7 @@
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
+import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.ext.LexicalHandler;
@@ -82,6 +83,8 @@
*/
public class XMLUtils{
+ public static final AttributesImpl EMPTY_ATTRIBUTES = new AttributesImpl();
+
//using parent because some dom implementations like jtidy are bugged,
//cannot get parent or delete child
public static void stripDuplicateAttributes(Node node, Node parent) {
@@ -370,5 +373,253 @@
// Give up: hope it's a string or has a meaningful string representation
data(contentHandler, String.valueOf(v));
}
+
+ /**
+ * Create a start and endElement with a empty Namespace and without Attributes
+ *
+ * @param localName The local name (without prefix)
+ * @exception org.xml.sax.SAXException Any SAX exception, possibly
+ * wrapping another exception.
+ * @see #endElement
+ */
+ public static void createElement(ContentHandler contentHandler, String localName)
+ throws SAXException {
+ startElement(contentHandler, localName);
+ endElement(contentHandler, localName);
+ }
+
+ /**
+ * Create a start and endElement with a empty Namespace and without Attributes
+ * The content of the Element is set to the stringValue parameter
+ *
+ * @param localName The local name (without prefix)
+ * @param stringValue The content of the Element
+ * @exception org.xml.sax.SAXException Any SAX exception, possibly
+ * wrapping another exception.
+ * @see #endElement
+ */
+ public static void createElement(ContentHandler contentHandler, String localName, String stringValue)
+ throws SAXException {
+ startElement(contentHandler, localName);
+ data(contentHandler, stringValue);
+ endElement(contentHandler, localName);
+ }
+
+ /**
+ * Create a start and endElement with a empty Namespace
+ *
+ * @param localName The local name (without prefix)
+ * @param atts The attributes attached to the element. If
+ * there are no attributes, it shall be an empty
+ * Attributes object.
+ * @exception org.xml.sax.SAXException Any SAX exception, possibly
+ * wrapping another exception.
+ * @see #endElement
+ * @see org.xml.sax.Attributes
+ */
+ public static void createElement(ContentHandler contentHandler, String localName, Attributes atts)
+ throws SAXException {
+ startElement(contentHandler, localName, atts);
+ endElement(contentHandler, localName);
+ }
+
+ /**
+ * Create a start and endElement with a empty Namespace
+ * The content of the Element is set to the stringValue parameter
+ *
+ * @param localName The local name (without prefix)
+ * @param atts The attributes attached to the element. If
+ * there are no attributes, it shall be an empty
+ * Attributes object.
+ * @param stringValue The content of the Element
+ * @exception org.xml.sax.SAXException Any SAX exception, possibly
+ * wrapping another exception.
+ * @see #endElement
+ * @see org.xml.sax.Attributes
+ */
+ public static void createElement(ContentHandler contentHandler, String localName, Attributes atts, String stringValue)
+ throws SAXException {
+ startElement(contentHandler, localName, atts);
+ data(contentHandler, stringValue);
+ endElement(contentHandler, localName);
+ }
+
+ /**
+ * Create a start and endElement without Attributes
+ *
+ * @param localName The local name (without prefix)
+ * @exception org.xml.sax.SAXException Any SAX exception, possibly
+ * wrapping another exception.
+ * @see #endElement
+ */
+ public static void createElementNS(ContentHandler contentHandler, String namespaceURI, String localName)
+ throws SAXException {
+ startElement(contentHandler, namespaceURI, localName);
+ endElement(contentHandler, namespaceURI, localName);
+ }
+
+ /**
+ * Create a start and endElement without Attributes
+ * The content of the Element is set to the stringValue parameter
+ *
+ * @param localName The local name (without prefix)
+ * @param stringValue The content of the Element
+ * @exception org.xml.sax.SAXException Any SAX exception, possibly
+ * wrapping another exception.
+ * @see #endElement
+ */
+ public static void createElementNS(
+ ContentHandler contentHandler,
+ String namespaceURI,
+ String localName,
+ String stringValue)
+ throws SAXException {
+ startElement(contentHandler, namespaceURI, localName);
+ data(contentHandler, stringValue);
+ endElement(contentHandler, namespaceURI, localName);
+ }
+
+ /**
+ * Create a start and endElement
+ *
+ * @param localName The local name (without prefix)
+ * @param atts The attributes attached to the element. If
+ * there are no attributes, it shall be an empty
+ * Attributes object.
+ * @exception org.xml.sax.SAXException Any SAX exception, possibly
+ * wrapping another exception.
+ * @see #endElement
+ * @see org.xml.sax.Attributes
+ */
+ public static void createElementNS(
+ ContentHandler contentHandler,
+ String namespaceURI,
+ String localName,
+ Attributes atts)
+ throws SAXException {
+ 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
+ *
+ * @param localName The local name (without prefix)
+ * @param atts The attributes attached to the element. If
+ * there are no attributes, it shall be an empty
+ * Attributes object.
+ * @param stringValue The content of the Element
+ * @exception org.xml.sax.SAXException Any SAX exception, possibly
+ * wrapping another exception.
+ * @see #endElement
+ * @see org.xml.sax.Attributes
+ */
+ public static void createElementNS(
+ ContentHandler contentHandler,
+ String namespaceURI,
+ String localName,
+ Attributes atts,
+ String stringValue)
+ throws SAXException {
+ startElement(contentHandler, namespaceURI, localName, atts);
+ data(contentHandler, stringValue);
+ endElement(contentHandler, namespaceURI, localName);
+ }
+
+
+ /**
+ * Create endElement with empty Namespace
+ *
+ * <p>For information on the names, see startElement.</p>
+ *
+ * @param localName The local name (without prefix)
+ * @exception org.xml.sax.SAXException Any SAX exception, possibly
+ * wrapping another exception.
+ */
+ public static void endElement(ContentHandler contentHandler, String localName)
+ throws SAXException {
+ contentHandler.endElement("", localName, localName);
+ }
+
+ /**
+ * Create endElement
+ * Prefix must be mapped to empty String
+ *
+ * <p>For information on the names, see startElement.</p>
+ *
+ * @param localName The local name (without prefix)
+ * @exception org.xml.sax.SAXException Any SAX exception, possibly
+ * wrapping another exception.
+ */
+ public static void endElement(ContentHandler contentHandler, String namespaceURI, String localName)
+ throws SAXException {
+ contentHandler.endElement(namespaceURI, localName, localName);
+ }
+
+ /**
+ * Create a startElement with a empty Namespace and without Attributes
+ *
+ * @param localName The local name (without prefix)
+ * @exception org.xml.sax.SAXException Any SAX exception, possibly
+ * wrapping another exception.
+ * @see #endElement
+ */
+ public static void startElement(ContentHandler contentHandler, String localName)
+ throws SAXException {
+ contentHandler.startElement("", localName, localName, EMPTY_ATTRIBUTES);
+ }
+
+ /**
+ * Create a startElement without Attributes
+ * Prefix must be mapped to empty String
+ *
+ * @param namespaceURI The Namespace URI
+ * @param localName The local name (without prefix)
+ * @exception org.xml.sax.SAXException Any SAX exception, possibly
+ * wrapping another exception.
+ * @see #endElement
+ */
+ public static void startElement(ContentHandler contentHandler, String namespaceURI, String localName)
+ throws SAXException {
+ contentHandler.startElement(namespaceURI, localName, localName, EMPTY_ATTRIBUTES);
+ }
+
+ /**
+ * Create a startElement with a empty Namespace
+ *
+ * @param localName The local name (without prefix)
+ * @param atts The attributes attached to the element. If
+ * there are no attributes, it shall be an empty
+ * Attributes object.
+ * @exception org.xml.sax.SAXException Any SAX exception, possibly
+ * wrapping another exception.
+ * @see #endElement
+ * @see org.xml.sax.Attributes
+ */
+ public static void startElement(ContentHandler contentHandler, String localName, Attributes atts)
+ throws SAXException {
+ contentHandler.startElement("", localName, localName, atts);
+ }
+
+ /**
+ * Create a startElement with a empty Namespace
+ * Prefix must be mapped to empty String
+ *
+ * @param namespaceURI The Namespace URI
+ * @param localName The local name (without prefix)
+ * @param atts The attributes attached to the element. If
+ * there are no attributes, it shall be an empty
+ * Attributes object.
+ * @exception org.xml.sax.SAXException Any SAX exception, possibly
+ * wrapping another exception.
+ * @see #endElement
+ * @see org.xml.sax.Attributes
+ */
+ public static void startElement(ContentHandler contentHandler, String namespaceURI, String localName, Attributes atts)
+ throws SAXException {
+ contentHandler.startElement(namespaceURI, localName, localName, atts);
+ }
+
}
1.1 cocoon-2.1/src/java/org/apache/cocoon/xml/AttributeTypes.java
Index: AttributeTypes.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
apache@apache.org.
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
Stefano Mazzocchi <st...@apache.org>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.xml;
/**
* Insert the type's description here.
*
* @author <a href="mailto:volker.schmitt@basf-ag.de">Volker Schmitt</a>
* @version CVS $Id: AttributeTypes.java,v 1.1 2003/05/06 12:09:20 cziegeler Exp $
*/
public interface AttributeTypes
{
public static final String CDATA = "CDATA";
public static final String ENTITY = "ENTITY";
public static final String ENTITIES = "ENTITIES";
public static final String ID = "ID";
public static final String IDREF = "IDREF";
public static final String IDREFS = "IDREFS";
public static final String NAME = "NAME";
public static final String NAMES = "NAMES";
public static final String NMTOKEN = "NMTOKEN";
public static final String NMTOKENS = "NMTOKENS";
public static final String NOTATION = "NOTATION";
public static final String NUMBER = "NUMBER";
public static final String NUMBERS = "NUMBERS";
public static final String NUTOKEN = "NUTOKEN";
public static final String NUTOKENS = "NUTOKENS";
}
1.1 cocoon-2.1/src/java/org/apache/cocoon/xml/AttributesImpl.java
Index: AttributesImpl.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
apache@apache.org.
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
Stefano Mazzocchi <st...@apache.org>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.xml;
/**
* A helper Class creating SAX Attributes
*
* @author <a href="mailto:volker.schmitt@basf-ag.de">Volker Schmitt</a>
* @version CVS $Id: AttributesImpl.java,v 1.1 2003/05/06 12:09:20 cziegeler Exp $
*/
public class AttributesImpl extends org.xml.sax.helpers.AttributesImpl {
/**
* Add an attribute of type CDATA with empty Namespace to the end of the list.
*
* <p>For the sake of speed, this method does no checking
* to see if the attribute is already in the list: that is
* the responsibility of the application.</p>
*
* @param localName The local name.
* @param value The attribute value.
*/
public void addCDATAAttribute(String localName, String value) {
addAttribute("", localName, localName, AttributeTypes.CDATA, value);
}
/**
* Add an attribute of type CDATA to the end of the list.
*
* <p>For the sake of speed, this method does no checking
* to see if the attribute is already in the list: that is
* the responsibility of the application.</p>
*
* @param uri The Namespace URI, or the empty string if
* none is available or Namespace processing is not
* being performed.
* @param localName The local name, or the empty string if
* Namespace processing is not being performed.
* @param qName The qualified (prefixed) name, or the empty string
* if qualified names are not available.
* @param value The attribute value.
*/
public void addCDATAAttribute(String uri,
String localName,
String qName,
String value) {
addAttribute(uri, localName, qName, AttributeTypes.CDATA, value);
}
}