You are viewing a plain text version of this content. The canonical link for it is here.
Posted to slide-dev@jakarta.apache.org by ju...@apache.org on 2002/03/08 13:00:47 UTC
cvs commit: jakarta-slide/src/webdav/server/org/apache/slide/webdav/util XMLValue.java
juergen 02/03/08 04:00:47
Added: src/webdav/server/org/apache/slide/webdav/util XMLValue.java
Log:
Initial revision.
(ralf)
Revision Changes Path
1.1 jakarta-slide/src/webdav/server/org/apache/slide/webdav/util/XMLValue.java
Index: XMLValue.java
===================================================================
/*
* $Header: /home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/util/XMLValue.java,v 1.1 2002/03/08 12:00:47 juergen Exp $
* $Revision: 1.1 $
* $Date: 2002/03/08 12:00:47 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, 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 acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Tomcat", 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 names without prior written
* permission of the Apache Group.
*
* 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 (INCLUDING, 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. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.slide.webdav.util;
// import list
import java.io.StringReader;
import java.io.StringWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
/**
* This class is a container for a list of JDOM Elements.
* The {@link #toString toString()} method provides a XML document fragment
* describing the Elements of this XMLValue.
*
* @version $Revision: 1.1 $
*
* @author <a href="mailto:ralf.stuckert@softwareag.com">Ralf Stuckert</a>
**/
public class XMLValue implements Cloneable {
/**
* Constant for the message of the IllegalArgumentException that may be
* thrown in method {@link #add add()}.
*/
public static final String ELEMENT_MUST_NOT_BE_NULL = "'null' Element is not allowed";
/**
* Constant for the message of the IllegalArgumentException that may be
* thrown in constructor {@link #XMLValue(List) XMLValue(List)}.
*/
public static final String TYPE_MISMATCH = "Only objects of type Element are allowed";
/**
* The start tag used to create a valid document out of the XML Element list
* string in constructor {@link #XMLValue(String) XMLValue(String)}.
*/
protected static final String START_TAG = "<root>";
/**
* The end tag used to create a valid document out of the XML Element list
* string in constructor {@link #XMLValue(String) XMLValue(String)}.
*/
protected static final String END_TAG = "</root>";
/**
* The list of JDOM Elements.
*/
protected ArrayList elementList = null;
/**
* Creates a XMLValue.
*/
public XMLValue() {
this((List)null);
}
/**
* Creates a XMLValue from the given <code>element</code>.
*
* @param element the JDOM Element to add to the list.
*
* @throws IllegalArgumentException if the given <code>element</code>
* is <code>null</code>.
*/
public XMLValue(Element element) throws IllegalArgumentException {
this((List)null);
add(element);
}
/**
* Creates a XMLValue from the given <code>list</code> of JDOM Elements.
* the given List may be <code>null</code>.
*
* @param elementList the list of JDOM Elements to add.
*
* @throws IllegalArgumentException if one of the list items
* is <code>null</code> or not a
* <code>Element</code>.
*/
public XMLValue(List elementList) throws IllegalArgumentException{
this.elementList = new ArrayList();
add(elementList);
}
/**
* Creates a XMLValue from the given String representation of a
* list of XML Elements.
*
* @param xmlString a String representation of a list of XML Elements.
*
*/
public XMLValue(String xmlString) throws JDOMException, IllegalArgumentException{
this((List)null);
if (xmlString != null) {
StringBuffer buffer = new StringBuffer(START_TAG.length() +
xmlString.length() +
END_TAG.length());
buffer.append(START_TAG);
buffer.append(xmlString);
buffer.append(END_TAG);
SAXBuilder builder = new SAXBuilder();
Document document = builder.build(new StringReader(buffer.toString()));
add(document.getRootElement().getChildren());
}
}
/**
* Adds a JDOM Element.
*
* @param element the JDOM Element to add.
*
* @throws IllegalArgumentException if the given <code>element</code>
* is <code>null</code>.
*/
public void add(Element element) throws IllegalArgumentException {
if (element == null) {
throw new IllegalArgumentException(ELEMENT_MUST_NOT_BE_NULL);
}
elementList.add(element);
}
/**
* Adds a List of JDOM Element.
*
* @param elementList the list of JDOM Elements to add.
*
* @throws IllegalArgumentException if one of the list items
* is <code>null</code> or not a
* <code>Element</code>.
*/
public void add(List elementList) throws IllegalArgumentException {
if (elementList != null) {
Iterator iterator = elementList.iterator();
while (iterator.hasNext()) {
try {
add((Element)iterator.next());
}
catch (ClassCastException e) {
throw new IllegalArgumentException(TYPE_MISMATCH);
}
}
}
}
/**
* Returns an iterator of JDOM Elements.
*
* @return an iterator of JDOM Elements.
*/
public Iterator iterator() {
return elementList.iterator();
}
/**
* Returns the amount of JDOM elements.
*
* @return the amount of JDOM elements.
*/
public int size() {
return elementList.size();
}
/**
* Returns a clone of the List of JDOM Elements.
*
* @return a clone of the List of JDOM Elements.
*/
public List getList() {
return (List)elementList.clone();
}
/**
* Returns <code>true</code> if the other object is an XMLValue and
* both their Element Lists are equal.
*
* @param other the Object to test for equality.
*
* @return <code>true</code> if the other object is an XMLValue and
* both their Element Lists are equal.
*/
public boolean equals(Object other) {
if ( ! (other instanceof XMLValue) ) {
return false;
}
return (this.elementList.equals(((XMLValue)other).elementList));
}
/**
* Returns the hash code of this instance.
*
* @return the hash code of this instance.
*/
public int hashCode() {
return elementList.hashCode();
}
/**
* Returns a clone of this instance.
*
* @return a clone of this instance.
*/
public Object clone() {
return new XMLValue(elementList);
}
/**
* Returns a String representation of the Elements as a XML document fragment.
*
* @return a String representation of the Elements.
*/
public String toString() {
XMLOutputter outputter = new XMLOutputter();
StringWriter stringWriter = new StringWriter();
Iterator iterator = elementList.iterator();
while (iterator.hasNext()) {
try {
outputter.output((Element)iterator.next(), stringWriter);
}
catch (IOException e) {
// this should not happen since we're writing to a StringWriter
// which is no 'real' I/O
throw new RuntimeException("IOException occurred: " + e.getMessage());
}
}
return stringWriter.toString();
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>