remm 01/02/15 09:37:31
Added: src/util/org/apache/util Base64.java DOMUtils.java
DOMWriter.java MD5Encoder.java MIME2Java.java
URLUtil.java XMLPrinter.java
Log:
- Create a set of utils which are shared between the three components of Slide
to avoid having a version of the file in multiple spots in the repository.
Revision Changes Path
1.1 jakarta-slide/src/util/org/apache/util/Base64.java
Index: Base64.java
===================================================================
/*
* $Header: /home/cvs/jakarta-slide/src/util/org/apache/util/Base64.java,v 1.1
2001/02/15 17:37:28 remm Exp $
* $Revision: 1.1 $
* $Date: 2001/02/15 17:37:28 $
*
* ====================================================================
*
* 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 [EMAIL PROTECTED]
*
* 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.util;
/**
* This class provides encode/decode for RFC 2045 Base64 as
* defined by RFC 2045, N. Freed and N. Borenstein.
* RFC 2045: Multipurpose Internet Mail Extensions (MIME)
* Part One: Format of Internet Message Bodies. Reference
* 1996 Available at: http://www.ietf.org/rfc/rfc2045.txt
* This class is used by XML Schema binary format validation
*
* @author Jeffrey Rodriguez
* @version $Revision: 1.1 $ $Date: 2001/02/15 17:37:28 $
*/
public final class Base64 {
static private final int BASELENGTH = 255;
static private final int LOOKUPLENGTH = 63;
static private final int TWENTYFOURBITGROUP = 24;
static private final int EIGHTBIT = 8;
static private final int SIXTEENBIT = 16;
static private final int SIXBIT = 6;
static private final int FOURBYTE = 4;
static private final byte PAD = ( byte ) '=';
static private byte [] base64Alphabet = new byte[BASELENGTH];
static private byte [] lookUpBase64Alphabet = new byte[LOOKUPLENGTH];
static {
for (int i = 0; i<BASELENGTH; i++ ) {
base64Alphabet[i] = -1;
}
for ( int i = 'Z'; i >= 'A'; i-- ) {
base64Alphabet[i] = (byte) (i-'A');
}
for ( int i = 'z'; i>= 'a'; i--) {
base64Alphabet[i] = (byte) ( i-'a' + 26);
}
for ( int i = '9'; i >= '0'; i--) {
base64Alphabet[i] = (byte) (i-'0' + 52);
}
base64Alphabet['+'] = 62;
base64Alphabet['/'] = 63;
for (int i = 0; i<=25; i++ )
lookUpBase64Alphabet[i] = (byte) ('A'+i );
for (int i = 26, j = 0; i<=51; i++, j++ )
lookUpBase64Alphabet[i] = (byte) ('a'+ j );
for (int i = 52, j = 0; i<=61; i++, j++ )
lookUpBase64Alphabet[i] = (byte) ('0' + j );
}
static boolean isBase64( byte octect ) {
//shall we ignore white space? JEFF??
return(octect == PAD || base64Alphabet[octect] != -1 );
}
static boolean isArrayByteBase64( byte[] arrayOctect ) {
int length = arrayOctect.length;
if ( length == 0 )
return false;
for ( int i=0; i < length; i++ ) {
if ( Base64.isBase64( arrayOctect[i] ) == false)
return false;
}
return true;
}
/**
* Encodes hex octects into Base64
*
* @param binaryData Array containing binaryData
* @return Encoded Base64 array
*/
public byte[] encode( byte[] binaryData ) {
int lengthDataBits = binaryData.length*EIGHTBIT;
int fewerThan24bits = lengthDataBits%TWENTYFOURBITGROUP;
int numberTriplets = lengthDataBits/TWENTYFOURBITGROUP;
byte encodedData[] = null;
if ( fewerThan24bits != 0 ) //data not divisible by 24 bit
encodedData = new byte[ (numberTriplets + 1 )*4 ];
else // 16 or 8 bit
encodedData = new byte[ numberTriplets*4 ];
byte k=0, l=0, b1=0,b2=0,b3=0;
int encodedIndex = 0;
int dataIndex = 0;
int i = 0;
for ( i = 0; i<numberTriplets; i++ ) {
dataIndex = i*3;
b1 = binaryData[dataIndex];
b2 = binaryData[dataIndex + 1];
b3 = binaryData[dataIndex + 2];
l = (byte)(b2 & 0x0f);
k = (byte)(b1 & 0x03);
encodedIndex = i*4;
encodedData[encodedIndex] = lookUpBase64Alphabet[ b1 >>2 ];
encodedData[encodedIndex+1] = lookUpBase64Alphabet[(b2 >>4 ) |
( k<<4 )];
encodedData[encodedIndex+2] = lookUpBase64Alphabet[ (l <<2 ) |
( b3>>6)];
encodedData[encodedIndex+3] = lookUpBase64Alphabet[ b3 & 0x3f ];
}
// form integral number of 6-bit groups
dataIndex = i*3;
encodedIndex = i*4;
if (fewerThan24bits == EIGHTBIT ) {
b1 = binaryData[dataIndex];
k = (byte) ( b1 &0x03 );
encodedData[encodedIndex] = lookUpBase64Alphabet[ b1 >>2 ];
encodedData[encodedIndex + 1] = lookUpBase64Alphabet[ k<<4 ];
encodedData[encodedIndex + 2] = PAD;
encodedData[encodedIndex + 3] = PAD;
} else if ( fewerThan24bits == SIXTEENBIT ) {
b1 = binaryData[dataIndex];
b2 = binaryData[dataIndex +1 ];
l = ( byte ) ( b2 &0x0f );
k = ( byte ) ( b1 &0x03 );
encodedData[encodedIndex] = lookUpBase64Alphabet[ b1 >>2 ];
encodedData[encodedIndex + 1] = lookUpBase64Alphabet[ (b2 >>4 )
| ( k<<4 )];
encodedData[encodedIndex + 2] = lookUpBase64Alphabet[ l<<2 ];
encodedData[encodedIndex + 3] = PAD;
}
return encodedData;
}
/**
* Decodes Base64 data into octects
*
* @param binaryData Byte array containing Base64 data
* @return Array containind decoded data.
*/
public byte[] decode( byte[] base64Data ) {
int numberQuadruple = base64Data.length/FOURBYTE;
byte decodedData[] = null;
byte b1=0,b2=0,b3=0, b4=0, marker0=0, marker1=0;
// Throw away anything not in base64Data
// Adjust size
int encodedIndex = 0;
int dataIndex = 0;
decodedData = new byte[ numberQuadruple*3 + 1 ];
for (int i = 0; i<numberQuadruple; i++ ) {
dataIndex = i*4;
marker0 = base64Data[dataIndex +2];
marker1 = base64Data[dataIndex +3];
b1 = base64Alphabet[base64Data[dataIndex]];
b2 = base64Alphabet[base64Data[dataIndex +1]];
if ( marker0 != PAD && marker1 != PAD ) { //No PAD e.g 3cQl
b3 = base64Alphabet[ marker0 ];
b4 = base64Alphabet[ marker1 ];
decodedData[encodedIndex] = (byte)( b1 <<2 | b2>>4 ) ;
decodedData[encodedIndex+1] = (byte)(((b2 & 0xf)<<4 ) |(
(b3>>2) & 0xf) );
decodedData[encodedIndex+2] = (byte)( b3<<6 | b4 );
} else if ( marker0 == PAD ) { //Two PAD e.g. 3c[Pad][Pad]
decodedData[encodedIndex] = (byte)( b1 <<2 | b2>>4 ) ;
decodedData[encodedIndex+1] = (byte)((b2 & 0xf)<<4 );
decodedData[encodedIndex+2] = (byte) 0;
} else if ( marker1 == PAD ) { //One PAD e.g. 3cQ[Pad]
b3 = base64Alphabet[ marker0 ];
decodedData[encodedIndex] = (byte)( b1 <<2 | b2>>4 );
decodedData[encodedIndex+1] = (byte)(((b2 & 0xf)<<4 ) |(
(b3>>2) & 0xf) );
decodedData[encodedIndex+2] = (byte)( b3<<6);
}
encodedIndex += 3;
}
return decodedData;
}
}
1.1 jakarta-slide/src/util/org/apache/util/DOMUtils.java
Index: DOMUtils.java
===================================================================
/*
* $Header: /home/cvs/jakarta-slide/src/util/org/apache/util/DOMUtils.java,v 1.1
2001/02/15 17:37:29 remm Exp $
* $Revision: 1.1 $
* $Date: 2001/02/15 17:37:29 $
*
* ====================================================================
*
* 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 [EMAIL PROTECTED]
*
* 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.util;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.io.InputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.StringTokenizer;
import java.util.Vector;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
/**
* This class provides some basic utility methods for working with
* XML Document objects. Many of these utilities provide JAXP 1.0 "brute
* force" implementations of functions that are available in JAXP 1.1.
*
* @author <a href="mailto:[EMAIL PROTECTED]>B.C. Holmes</a>
* @version $Revision: 1.1 $
*/
public class DOMUtils {
protected static Class[] getElementsByNSParameterTypes =
{ String.class, String.class };
/**
* This method checks a DOM node to see if the implementation
* is DOM version 2 compliant (and hence, supports namespaces).
*/
public static boolean isDOM2Compliant(Node node) {
if (node == null) {
throw new IllegalArgumentException
("The input node cannot be null");
}
Document document = node.getOwnerDocument();
boolean isDOM2 = false;
try {
Class documentClass = document.getClass();
Method method = documentClass.getMethod(
"getElementsByTagNameNS",
getElementsByNSParameterTypes );
// can method ever be null?
if (method != null) {
isDOM2 = true;
}
} catch (NoSuchMethodException e) {
}
return isDOM2;
}
/**
* Determine the namespace prefix being used for DAV.
* Generally, DAV responses say something like:
*
* <PRE>
* <D:multistatus xmlns:D="DAV:">
* </PRE>
*
* <P> In this case, the "D:" is the prefix for DAV.
*/
public static String findDavPrefix(Document document) {
Element multistatus = document.getDocumentElement();
NamedNodeMap list = multistatus.getAttributes();
String prefix = "DAV:";
for (int i = 0; i < list.getLength(); i++) {
try {
Attr attr = (Attr) list.item(i);
if (attr.getName() != null &&
attr.getName().startsWith("xmlns") &&
attr.getValue().equals("DAV:")) {
int indx = attr.getName().indexOf(":");
if ((indx >= 0) && (indx < attr.getName().length()-1)) {
prefix = attr.getName().substring(indx + 1) + ":";
} else {
prefix = "";
}
}
} catch (ClassCastException e) {
}
}
return prefix;
}
/**
* Scan all immediate children of a node, and append all
* text nodes into a string. Consider the following example
*
* <PRE>
* <customer>Joe Schmoe</customer>
* </PRE>
*
* <P> In this case, calling this method on the
* <CODE>customer</CODE> element returns "Joe Schmoe".
*/
public static String getTextValue(Node node) {
// I *thought* that I should be able to use element.getNodeValue()...
String text = "";
NodeList textList = node.getChildNodes();
for (int i = 0; i < textList.getLength(); i++) {
try {
text += ((Text) textList.item(i)).getData();
} catch (ClassCastException e) {
// we don't care about non-Text nodes
}
}
return text;
}
/**
* Get the status code out of the normal status response.
*
* <P> Each <code>DAV:propstat</code> node contains a
* status line, such as:
*
* <PRE>
* <DAV:status>HTTP/1.1 200 OK</DAV:status>
* </PRE>
*
* <P> In this case, calling this method on the
* text string returns 200.
*/
public static int parseStatus(String statusString) {
int status = -1;
if (statusString != null) {
StringTokenizer tokenizer = new StringTokenizer(statusString);
if (tokenizer.countTokens() >= 2) {
Object dummy = tokenizer.nextElement();
String statusCode = tokenizer.nextElement().toString();
try {
status = Integer.parseInt(statusCode);
} catch (NumberFormatException e) {
throw new IllegalArgumentException(
"Status code is not numeric");
}
} else {
throw new IllegalArgumentException(
"There aren't enough words in the input argument");
}
}
return status;
}
/**
*
*/
public static String getElementNamespaceURI(Element element) {
String namespace = null;
if (element == null) {
throw new IllegalArgumentException(
"The element cannot be null");
} else if (isDOM2Compliant(element)) {
/*
try {
Method method = element.getClass().getMethod(
"getNamespaceURI", null );
if (method != null) {
namespace = (String) method.invoke(element, null);
} else {
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
} catch (NoSuchMethodException e) {
}
*/
namespace = element.getNamespaceURI();
} else {
String tagName = element.getTagName();
String attribute = "xmlns";
int index = tagName.indexOf(":");
if (index > 0 && index < (tagName.length()-1)) {
attribute += (":" + tagName.substring(0,index));
}
boolean found = false;
for (Node node = element; !found && node != null;
node = node.getParentNode()) {
try {
String tmp = ((Element) node).getAttribute(attribute);
if (tmp != null && !tmp.equals("")) {
namespace = tmp;
found = true;
}
} catch (ClassCastException e) {
// this will happen for Documents
}
}
}
return namespace;
}
/**
*
*/
public static String getElementLocalName(Element element) {
String localName = null;
if (element == null) {
throw new IllegalArgumentException(
"The element cannot be null");
} else if (isDOM2Compliant(element)) {
/*
try {
Method method = element.getClass().getMethod(
"getLocalName", null );
if (method != null) {
localName = (String) method.invoke(element, null);
} else {
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
*/
localName = element.getLocalName();
} else {
localName = element.getTagName();
int index = localName.indexOf(":");
if (index > 0 && index < (localName.length()-1)) {
localName = localName.substring(index + 1);
}
}
return localName;
}
/**
*
*/
public static NodeList getElementsByTagNameNS(
Node node, String tagName, String namespace) {
NodeList list = null;
if (node == null) {
} else if (!(node instanceof Document) &&
!(node instanceof Element)) {
throw new IllegalArgumentException(
"The node parameter must be an Element or a Document node");
} else if (isDOM2Compliant(node)) {
/*
try {
Method method = node.getClass().getMethod(
"getElementsByTagNameNS", getElementsByNSParameterTypes );
if (method != null) {
Object[] params = { namespace, tagName };
list = (NodeList) method.invoke(node, params);
} else {
}
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
} catch (NoSuchMethodException e) {
}
*/
list = ((Element) node).getElementsByTagNameNS(namespace, tagName);
} else {
Vector vector = new Vector();
getChildElementsByTagNameNS(vector, node, tagName, namespace);
list = new NodeListImpl(vector);
}
return list;
}
protected static void getChildElementsByTagNameNS(
Vector vector, Node node, String tagName, String namespace) {
NodeList list = node.getChildNodes();
for (int i = 0; list != null && i < list.getLength(); i++) {
try {
Element element = (Element) list.item(i);
if (tagName.equals(DOMUtils.getElementLocalName(element)) &&
namespace.equals(
DOMUtils.getElementNamespaceURI(element))) {
vector.addElement(element);
} else {
// RECURSIVE! DANGER, WILL ROBINSON!
getChildElementsByTagNameNS(vector, element,
tagName, namespace);
}
} catch (ClassCastException e) {
}
}
}
// ---------------------------------------------------------- Inner Classes
/**
* This class provides an implementation of NodeList, which is used by
* the getElementsByTagNameNS() method.
*/
static class NodeListImpl implements NodeList {
private Vector vector = null;
NodeListImpl(Vector vector) {
this.vector = vector;
}
public int getLength() {
return vector.size();
}
public Node item(int i) {
return (Node) vector.elementAt(i);
}
}
}
1.1 jakarta-slide/src/util/org/apache/util/DOMWriter.java
Index: DOMWriter.java
===================================================================
/*
* $Header: /home/cvs/jakarta-slide/src/util/org/apache/util/DOMWriter.java,v 1.1
2001/02/15 17:37:29 remm Exp $
* $Revision: 1.1 $
* $Date: 2001/02/15 17:37:29 $
*
* ====================================================================
*
* 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 [EMAIL PROTECTED]
*
* 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.util;
import java.io.*;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
/**
* A sample DOM writer. This sample program illustrates how to
* traverse a DOM tree in order to print a document that is parsed.
*/
public class DOMWriter {
//
// Data
//
/** Default Encoding */
private static String
PRINTWRITER_ENCODING = "UTF8";
private static String MIME2JAVA_ENCODINGS[] =
{ "Default", "UTF-8", "US-ASCII", "ISO-8859-1", "ISO-8859-2", "ISO-8859-3",
"ISO-8859-4",
"ISO-8859-5", "ISO-8859-6", "ISO-8859-7", "ISO-8859-8", "ISO-8859-9",
"ISO-2022-JP",
"SHIFT_JIS", "EUC-JP","GB2312", "BIG5", "EUC-KR", "ISO-2022-KR", "KOI8-R",
"EBCDIC-CP-US",
"EBCDIC-CP-CA", "EBCDIC-CP-NL", "EBCDIC-CP-DK", "EBCDIC-CP-NO",
"EBCDIC-CP-FI", "EBCDIC-CP-SE",
"EBCDIC-CP-IT", "EBCDIC-CP-ES", "EBCDIC-CP-GB", "EBCDIC-CP-FR",
"EBCDIC-CP-AR1",
"EBCDIC-CP-HE", "EBCDIC-CP-CH", "EBCDIC-CP-ROECE","EBCDIC-CP-YU",
"EBCDIC-CP-IS", "EBCDIC-CP-AR2", "UTF-16"
};
/** Print writer. */
protected PrintWriter out;
/** Canonical output. */
protected boolean canonical;
public DOMWriter(String encoding, boolean canonical)
throws UnsupportedEncodingException {
out = new PrintWriter(new OutputStreamWriter(System.out, encoding));
this.canonical = canonical;
} // <init>(String,boolean)
//
// Constructors
//
/** Default constructor. */
public DOMWriter(boolean canonical) throws UnsupportedEncodingException {
this( getWriterEncoding(), canonical);
}
public DOMWriter(Writer writer, boolean canonical) {
out = new PrintWriter(writer);
this.canonical = canonical;
}
public static String getWriterEncoding( ) {
return (PRINTWRITER_ENCODING);
}// getWriterEncoding
public static void setWriterEncoding( String encoding ) {
if( encoding.equalsIgnoreCase( "DEFAULT" ) )
PRINTWRITER_ENCODING = "UTF8";
else if( encoding.equalsIgnoreCase( "UTF-16" ) )
PRINTWRITER_ENCODING = "Unicode";
else
PRINTWRITER_ENCODING = MIME2Java.convert( encoding );
}// setWriterEncoding
public static boolean isValidJavaEncoding( String encoding ) {
for ( int i = 0; i < MIME2JAVA_ENCODINGS.length; i++ )
if ( encoding.equals( MIME2JAVA_ENCODINGS[i] ) )
return (true);
return (false);
}// isValidJavaEncoding
/** Prints the specified node, recursively. */
public void print(Node node) {
// is there anything to do?
if ( node == null ) {
return;
}
int type = node.getNodeType();
switch ( type ) {
// print document
case Node.DOCUMENT_NODE: {
if ( !canonical ) {
String Encoding = this.getWriterEncoding();
if( Encoding.equalsIgnoreCase( "DEFAULT" ) )
Encoding = "UTF-8";
else if( Encoding.equalsIgnoreCase( "Unicode" ) )
Encoding = "UTF-16";
else
Encoding = MIME2Java.reverse( Encoding );
out.println("<?xml version=\"1.0\" encoding=\""+
Encoding + "\"?>");
}
print(((Document)node).getDocumentElement());
out.flush();
break;
}
// print element with attributes
case Node.ELEMENT_NODE: {
out.print('<');
out.print(node.getNodeName());
Attr attrs[] = sortAttributes(node.getAttributes());
for ( int i = 0; i < attrs.length; i++ ) {
Attr attr = attrs[i];
out.print(' ');
out.print(attr.getNodeName());
out.print("=\"");
out.print(normalize(attr.getNodeValue()));
out.print('"');
}
out.print('>');
NodeList children = node.getChildNodes();
if ( children != null ) {
int len = children.getLength();
for ( int i = 0; i < len; i++ ) {
print(children.item(i));
}
}
break;
}
// handle entity reference nodes
case Node.ENTITY_REFERENCE_NODE: {
if ( canonical ) {
NodeList children = node.getChildNodes();
if ( children != null ) {
int len = children.getLength();
for ( int i = 0; i < len; i++ ) {
print(children.item(i));
}
}
} else {
out.print('&');
out.print(node.getNodeName());
out.print(';');
}
break;
}
// print cdata sections
case Node.CDATA_SECTION_NODE: {
if ( canonical ) {
out.print(normalize(node.getNodeValue()));
} else {
out.print("<![CDATA[");
out.print(node.getNodeValue());
out.print("]]>");
}
break;
}
// print text
case Node.TEXT_NODE: {
out.print(normalize(node.getNodeValue()));
break;
}
// print processing instruction
case Node.PROCESSING_INSTRUCTION_NODE: {
out.print("<?");
out.print(node.getNodeName());
String data = node.getNodeValue();
if ( data != null && data.length() > 0 ) {
out.print(' ');
out.print(data);
}
out.print("?>");
break;
}
}
if ( type == Node.ELEMENT_NODE ) {
out.print("</");
out.print(node.getNodeName());
out.print('>');
}
out.flush();
} // print(Node)
/** Returns a sorted list of attributes. */
protected Attr[] sortAttributes(NamedNodeMap attrs) {
int len = (attrs != null) ? attrs.getLength() : 0;
Attr array[] = new Attr[len];
for ( int i = 0; i < len; i++ ) {
array[i] = (Attr)attrs.item(i);
}
for ( int i = 0; i < len - 1; i++ ) {
String name = array[i].getNodeName();
int index = i;
for ( int j = i + 1; j < len; j++ ) {
String curName = array[j].getNodeName();
if ( curName.compareTo(name) < 0 ) {
name = curName;
index = j;
}
}
if ( index != i ) {
Attr temp = array[i];
array[i] = array[index];
array[index] = temp;
}
}
return (array);
} // sortAttributes(NamedNodeMap):Attr[]
/** Normalizes the given string. */
protected String normalize(String s) {
StringBuffer str = new StringBuffer();
int len = (s != null) ? s.length() : 0;
for ( int i = 0; i < len; i++ ) {
char ch = s.charAt(i);
switch ( ch ) {
case '<': {
str.append("<");
break;
}
case '>': {
str.append(">");
break;
}
case '&': {
str.append("&");
break;
}
case '"': {
str.append(""");
break;
}
case '\r':
case '\n': {
if ( canonical ) {
str.append("&#");
str.append(Integer.toString(ch));
str.append(';');
break;
}
// else, default append char
}
default: {
str.append(ch);
}
}
}
return (str.toString());
} // normalize(String):String
private static void printValidJavaEncoding() {
System.err.println( " ENCODINGS:" );
System.err.print( " " );
for( int i = 0;
i < MIME2JAVA_ENCODINGS.length; i++) {
System.err.print( MIME2JAVA_ENCODINGS[i] + " " );
if( (i % 7 ) == 0 ){
System.err.println();
System.err.print( " " );
}
}
} // printJavaEncoding()
}
1.1 jakarta-slide/src/util/org/apache/util/MD5Encoder.java
Index: MD5Encoder.java
===================================================================
/*
* $Header: /home/cvs/jakarta-slide/src/util/org/apache/util/MD5Encoder.java,v 1.1
2001/02/15 17:37:29 remm Exp $
* $Revision: 1.1 $
* $Date: 2001/02/15 17:37:29 $
*
* ====================================================================
*
* 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 [EMAIL PROTECTED]
*
* 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.util;
/**
* Encode an MD5 digest into a String.
* <p>
* The 128 bit MD5 hash is converted into a 32 character long String.
* Each character of the String is the hexadecimal representation of 4 bits
* of the digest.
*
* @author Remy Maucherat
* @version $Revision: 1.1 $ $Date: 2001/02/15 17:37:29 $
*/
public final class MD5Encoder {
// ----------------------------------------------------- Instance Variables
private static final char[] hexadecimal =
{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f'};
// --------------------------------------------------------- Public Methods
/**
* Encodes the 128 bit (16 bytes) MD5 into a 32 character String.
*
* @param binaryData Array containing the digest
* @return Encoded MD5, or null if encoding failed
*/
public String encode( byte[] binaryData ) {
if (binaryData.length != 16)
return null;
char[] buffer = new char[32];
for (int i=0; i<16; i++) {
int low = (int) (binaryData[i] & 0x0f);
int high = (int) ((binaryData[i] & 0xf0) >> 4);
buffer[i*2] = hexadecimal[high];
buffer[i*2 + 1] = hexadecimal[low];
}
return new String(buffer);
}
}
1.1 jakarta-slide/src/util/org/apache/util/MIME2Java.java
Index: MIME2Java.java
===================================================================
/*
* 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 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 "Xerces" 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 [EMAIL PROTECTED]
*
* 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 (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 and was
* originally based on software copyright (c) 1999, International
* Business Machines, Inc., http://www.apache.org. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.util;
import java.util.*;
/**
* MIME2Java is a convenience class which handles conversions between MIME charset
names
* and Java encoding names.
* <p>The supported XML encodings are the intersection of XML-supported code sets
and those
* supported in JDK 1.1.
* <p>MIME charset names are used on <var>xmlEncoding</var> parameters to methods
such
* as <code>TXDocument#setEncoding</code> and <code>DTD#setEncoding</code>.
* <p>Java encoding names are used on <var>encoding</var> parameters to
* methods such as <code>TXDocument#printWithFormat</code> and
<code>DTD#printExternal</code>.
* <P>
* <TABLE BORDER="0" WIDTH="100%">
* <TR>
* <TD WIDTH="33%">
* <P ALIGN="CENTER"><B>Common Name</B>
* </TD>
* <TD WIDTH="15%">
* <P ALIGN="CENTER"><B>Use this name in XML files</B>
* </TD>
* <TD WIDTH="12%">
* <P ALIGN="CENTER"><B>Name Type</B>
* </TD>
* <TD WIDTH="31%">
* <P ALIGN="CENTER"><B>Xerces converts to this Java Encoder Name</B>
* </TD>
* </TR>
* <TR>
* <TD WIDTH="33%">8 bit Unicode</TD>
* <TD WIDTH="15%">
* <P ALIGN="CENTER">UTF-8
* </TD>
* <TD WIDTH="12%">
* <P ALIGN="CENTER">IANA
* </TD>
* <TD WIDTH="31%">
* <P ALIGN="CENTER">UTF8
* </TD>
* </TR>
* <TR>
* <TD WIDTH="33%">ISO Latin 1</TD>
* <TD WIDTH="15%">
* <P ALIGN="CENTER">ISO-8859-1
* </TD>
* <TD WIDTH="12%">
* <P ALIGN="CENTER">MIME
* </TD>
* <TD WIDTH="31%">
* <P ALIGN="CENTER">ISO-8859-1
* </TD>
* </TR>
* <TR>
* <TD WIDTH="33%">ISO Latin 2</TD>
* <TD WIDTH="15%">
* <P ALIGN="CENTER">ISO-8859-2
* </TD>
* <TD WIDTH="12%">
* <P ALIGN="CENTER">MIME
* </TD>
* <TD WIDTH="31%">
* <P ALIGN="CENTER">ISO-8859-2
* </TD>
* </TR>
* <TR>
* <TD WIDTH="33%">ISO Latin 3</TD>
* <TD WIDTH="15%">
* <P ALIGN="CENTER">ISO-8859-3
* </TD>
* <TD WIDTH="12%">
* <P ALIGN="CENTER">MIME
* </TD>
* <TD WIDTH="31%">
* <P ALIGN="CENTER">ISO-8859-3
* </TD>
* </TR>
* <TR>
* <TD WIDTH="33%">ISO Latin 4</TD>
* <TD WIDTH="15%">
* <P ALIGN="CENTER">ISO-8859-4
* </TD>
* <TD WIDTH="12%">
* <P ALIGN="CENTER">MIME
* </TD>
* <TD WIDTH="31%">
* <P ALIGN="CENTER">ISO-8859-4
* </TD>
* </TR>
* <TR>
* <TD WIDTH="33%">ISO Latin Cyrillic</TD>
* <TD WIDTH="15%">
* <P ALIGN="CENTER">ISO-8859-5
* </TD>
* <TD WIDTH="12%">
* <P ALIGN="CENTER">MIME
* </TD>
* <TD WIDTH="31%">
* <P ALIGN="CENTER">ISO-8859-5
* </TD>
* </TR>
* <TR>
* <TD WIDTH="33%">ISO Latin Arabic</TD>
* <TD WIDTH="15%">
* <P ALIGN="CENTER">ISO-8859-6
* </TD>
* <TD WIDTH="12%">
* <P ALIGN="CENTER">MIME
* </TD>
* <TD WIDTH="31%">
* <P ALIGN="CENTER">ISO-8859-6
* </TD>
* </TR>
* <TR>
* <TD WIDTH="33%">ISO Latin Greek</TD>
* <TD WIDTH="15%">
* <P ALIGN="CENTER">ISO-8859-7
* </TD>
* <TD WIDTH="12%">
* <P ALIGN="CENTER">MIME
* </TD>
* <TD WIDTH="31%">
* <P ALIGN="CENTER">ISO-8859-7
* </TD>
* </TR>
* <TR>
* <TD WIDTH="33%">ISO Latin Hebrew</TD>
* <TD WIDTH="15%">
* <P ALIGN="CENTER">ISO-8859-8
* </TD>
* <TD WIDTH="12%">
* <P ALIGN="CENTER">MIME
* </TD>
* <TD WIDTH="31%">
* <P ALIGN="CENTER">ISO-8859-8
* </TD>
* </TR>
* <TR>
* <TD WIDTH="33%">ISO Latin 5</TD>
* <TD WIDTH="15%">
* <P ALIGN="CENTER">ISO-8859-9
* </TD>
* <TD WIDTH="12%">
* <P ALIGN="CENTER">MIME
* </TD>
* <TD WIDTH="31%">
* <P ALIGN="CENTER">ISO-8859-9
* </TD>
* </TR>
* <TR>
* <TD WIDTH="33%">EBCDIC: US</TD>
* <TD WIDTH="15%">
* <P ALIGN="CENTER">ebcdic-cp-us
* </TD>
* <TD WIDTH="12%">
* <P ALIGN="CENTER">IANA
* </TD>
* <TD WIDTH="31%">
* <P ALIGN="CENTER">cp037
* </TD>
* </TR>
* <TR>
* <TD WIDTH="33%">EBCDIC: Canada</TD>
* <TD WIDTH="15%">
* <P ALIGN="CENTER">ebcdic-cp-ca
* </TD>
* <TD WIDTH="12%">
* <P ALIGN="CENTER">IANA
* </TD>
* <TD WIDTH="31%">
* <P ALIGN="CENTER">cp037
* </TD>
* </TR>
* <TR>
* <TD WIDTH="33%">EBCDIC: Netherlands</TD>
* <TD WIDTH="15%">
* <P ALIGN="CENTER">ebcdic-cp-nl
* </TD>
* <TD WIDTH="12%">
* <P ALIGN="CENTER">IANA
* </TD>
* <TD WIDTH="31%">
* <P ALIGN="CENTER">cp037
* </TD>
* </TR>
* <TR>
* <TD WIDTH="33%">EBCDIC: Denmark</TD>
* <TD WIDTH="15%">
* <P ALIGN="CENTER">ebcdic-cp-dk
* </TD>
* <TD WIDTH="12%">
* <P ALIGN="CENTER">IANA
* </TD>
* <TD WIDTH="31%">
* <P ALIGN="CENTER">cp277
* </TD>
* </TR>
* <TR>
* <TD WIDTH="33%">EBCDIC: Norway</TD>
* <TD WIDTH="15%">
* <P ALIGN="CENTER">ebcdic-cp-no
* </TD>
* <TD WIDTH="12%">
* <P ALIGN="CENTER">IANA
* </TD>
* <TD WIDTH="31%">
* <P ALIGN="CENTER">cp277
* </TD>
* </TR>
* <TR>
* <TD WIDTH="33%">EBCDIC: Finland</TD>
* <TD WIDTH="15%">
* <P ALIGN="CENTER">ebcdic-cp-fi
* </TD>
* <TD WIDTH="12%">
* <P ALIGN="CENTER">IANA
* </TD>
* <TD WIDTH="31%">
* <P ALIGN="CENTER">cp278
* </TD>
* </TR>
* <TR>
* <TD WIDTH="33%">EBCDIC: Sweden</TD>
* <TD WIDTH="15%">
* <P ALIGN="CENTER">ebcdic-cp-se
* </TD>
* <TD WIDTH="12%">
* <P ALIGN="CENTER">IANA
* </TD>
* <TD WIDTH="31%">
* <P ALIGN="CENTER">cp278
* </TD>
* </TR>
* <TR>
* <TD WIDTH="33%">EBCDIC: Italy</TD>
* <TD WIDTH="15%">
* <P ALIGN="CENTER">ebcdic-cp-it
* </TD>
* <TD WIDTH="12%">
* <P ALIGN="CENTER">IANA
* </TD>
* <TD WIDTH="31%">
* <P ALIGN="CENTER">cp280
* </TD>
* </TR>
* <TR>
* <TD WIDTH="33%">EBCDIC: Spain, Latin America</TD>
* <TD WIDTH="15%">
* <P ALIGN="CENTER">ebcdic-cp-es
* </TD>
* <TD WIDTH="12%">
* <P ALIGN="CENTER">IANA
* </TD>
* <TD WIDTH="31%">
* <P ALIGN="CENTER">cp284
* </TD>
* </TR>
* <TR>
* <TD WIDTH="33%">EBCDIC: Great Britain</TD>
* <TD WIDTH="15%">
* <P ALIGN="CENTER">ebcdic-cp-gb
* </TD>
* <TD WIDTH="12%">
* <P ALIGN="CENTER">IANA
* </TD>
* <TD WIDTH="31%">
* <P ALIGN="CENTER">cp285
* </TD>
* </TR>
* <TR>
* <TD WIDTH="33%">EBCDIC: France</TD>
* <TD WIDTH="15%">
* <P ALIGN="CENTER">ebcdic-cp-fr
* </TD>
* <TD WIDTH="12%">
* <P ALIGN="CENTER">IANA
* </TD>
* <TD WIDTH="31%">
* <P ALIGN="CENTER">cp297
* </TD>
* </TR>
* <TR>
* <TD WIDTH="33%">EBCDIC: Arabic</TD>
* <TD WIDTH="15%">
* <P ALIGN="CENTER">ebcdic-cp-ar1
* </TD>
* <TD WIDTH="12%">
* <P ALIGN="CENTER">IANA
* </TD>
* <TD WIDTH="31%">
* <P ALIGN="CENTER">cp420
* </TD>
* </TR>
* <TR>
* <TD WIDTH="33%">EBCDIC: Hebrew</TD>
* <TD WIDTH="15%">
* <P ALIGN="CENTER">ebcdic-cp-he
* </TD>
* <TD WIDTH="12%">
* <P ALIGN="CENTER">IANA
* </TD>
* <TD WIDTH="31%">
* <P ALIGN="CENTER">cp424
* </TD>
* </TR>
* <TR>
* <TD WIDTH="33%">EBCDIC: Switzerland</TD>
* <TD WIDTH="15%">
* <P ALIGN="CENTER">ebcdic-cp-ch
* </TD>
* <TD WIDTH="12%">
* <P ALIGN="CENTER">IANA
* </TD>
* <TD WIDTH="31%">
* <P ALIGN="CENTER">cp500
* </TD>
* </TR>
* <TR>
* <TD WIDTH="33%">EBCDIC: Roece</TD>
* <TD WIDTH="15%">
* <P ALIGN="CENTER">ebcdic-cp-roece
* </TD>
* <TD WIDTH="12%">
* <P ALIGN="CENTER">IANA
* </TD>
* <TD WIDTH="31%">
* <P ALIGN="CENTER">cp870
* </TD>
* </TR>
* <TR>
* <TD WIDTH="33%">EBCDIC: Yogoslavia</TD>
* <TD WIDTH="15%">
* <P ALIGN="CENTER">ebcdic-cp-yu
* </TD>
* <TD WIDTH="12%">
* <P ALIGN="CENTER">IANA
* </TD>
* <TD WIDTH="31%">
* <P ALIGN="CENTER">cp870
* </TD>
* </TR>
* <TR>
* <TD WIDTH="33%">EBCDIC: Iceland</TD>
* <TD WIDTH="15%">
* <P ALIGN="CENTER">ebcdic-cp-is
* </TD>
* <TD WIDTH="12%">
* <P ALIGN="CENTER">IANA
* </TD>
* <TD WIDTH="31%">
* <P ALIGN="CENTER">cp871
* </TD>
* </TR>
* <TR>
* <TD WIDTH="33%">EBCDIC: Urdu</TD>
* <TD WIDTH="15%">
* <P ALIGN="CENTER">ebcdic-cp-ar2
* </TD>
* <TD WIDTH="12%">
* <P ALIGN="CENTER">IANA
* </TD>
* <TD WIDTH="31%">
* <P ALIGN="CENTER">cp918
* </TD>
* </TR>
* <TR>
* <TD WIDTH="33%">Chinese for PRC, mixed 1/2 byte</TD>
* <TD WIDTH="15%">
* <P ALIGN="CENTER">gb2312
* </TD>
* <TD WIDTH="12%">
* <P ALIGN="CENTER">MIME
* </TD>
* <TD WIDTH="31%">
* <P ALIGN="CENTER">GB2312
* </TD>
* </TR>
* <TR>
* <TD WIDTH="33%">Extended Unix Code, packed for Japanese</TD>
* <TD WIDTH="15%">
* <P ALIGN="CENTER">euc-jp
* </TD>
* <TD WIDTH="12%">
* <P ALIGN="CENTER">MIME
* </TD>
* <TD WIDTH="31%">
* <P ALIGN="CENTER">eucjis
* </TD>
* </TR>
* <TR>
* <TD WIDTH="33%">Japanese: iso-2022-jp</TD>
* <TD WIDTH="15%">
* <P ALIGN="CENTER">iso-2020-jp
* </TD>
* <TD WIDTH="12%">
* <P ALIGN="CENTER">MIME
* </TD>
* <TD WIDTH="31%">
* <P ALIGN="CENTER">JIS
* </TD>
* </TR>
* <TR>
* <TD WIDTH="33%">Japanese: Shift JIS</TD>
* <TD WIDTH="15%">
* <P ALIGN="CENTER">Shift_JIS
* </TD>
* <TD WIDTH="12%">
* <P ALIGN="CENTER">MIME
* </TD>
* <TD WIDTH="31%">
* <P ALIGN="CENTER">SJIS
* </TD>
* </TR>
* <TR>
* <TD WIDTH="33%">Chinese: Big5</TD>
* <TD WIDTH="15%">
* <P ALIGN="CENTER">Big5
* </TD>
* <TD WIDTH="12%">
* <P ALIGN="CENTER">MIME
* </TD>
* <TD WIDTH="31%">
* <P ALIGN="CENTER">Big5
* </TD>
* </TR>
* <TR>
* <TD WIDTH="33%">Extended Unix Code, packed for Korean</TD>
* <TD WIDTH="15%">
* <P ALIGN="CENTER">euc-kr
* </TD>
* <TD WIDTH="12%">
* <P ALIGN="CENTER">MIME
* </TD>
* <TD WIDTH="31%">
* <P ALIGN="CENTER">iso2022kr
* </TD>
* </TR>
* <TR>
* <TD WIDTH="33%">Cyrillic</TD>
* <TD WIDTH="15%">
* <P ALIGN="CENTER">koi8-r
* </TD>
* <TD WIDTH="12%">
* <P ALIGN="CENTER">MIME
* </TD>
* <TD WIDTH="31%">
* <P ALIGN="CENTER">koi8-r
* </TD>
* </TR>
* </TABLE>
*
* @author TAMURA Kent <[EMAIL PROTECTED]>
*/
public class MIME2Java {
static private Hashtable s_enchash;
static private Hashtable s_revhash;
static {
s_enchash = new Hashtable();
// <preferred MIME name>, <Java encoding name>
s_enchash.put("UTF-8", "UTF8");
s_enchash.put("US-ASCII", "8859_1"); // ?
s_enchash.put("ISO-8859-1", "8859_1");
s_enchash.put("ISO-8859-2", "8859_2");
s_enchash.put("ISO-8859-3", "8859_3");
s_enchash.put("ISO-8859-4", "8859_4");
s_enchash.put("ISO-8859-5", "8859_5");
s_enchash.put("ISO-8859-6", "8859_6");
s_enchash.put("ISO-8859-7", "8859_7");
s_enchash.put("ISO-8859-8", "8859_8");
s_enchash.put("ISO-8859-9", "8859_9");
s_enchash.put("ISO-2022-JP", "JIS");
s_enchash.put("SHIFT_JIS", "SJIS");
s_enchash.put("EUC-JP", "EUCJIS");
s_enchash.put("GB2312", "GB2312");
s_enchash.put("BIG5", "Big5");
s_enchash.put("EUC-KR", "KSC5601");
s_enchash.put("ISO-2022-KR", "ISO2022KR");
s_enchash.put("KOI8-R", "KOI8_R");
s_enchash.put("EBCDIC-CP-US", "CP037");
s_enchash.put("EBCDIC-CP-CA", "CP037");
s_enchash.put("EBCDIC-CP-NL", "CP037");
s_enchash.put("EBCDIC-CP-DK", "CP277");
s_enchash.put("EBCDIC-CP-NO", "CP277");
s_enchash.put("EBCDIC-CP-FI", "CP278");
s_enchash.put("EBCDIC-CP-SE", "CP278");
s_enchash.put("EBCDIC-CP-IT", "CP280");
s_enchash.put("EBCDIC-CP-ES", "CP284");
s_enchash.put("EBCDIC-CP-GB", "CP285");
s_enchash.put("EBCDIC-CP-FR", "CP297");
s_enchash.put("EBCDIC-CP-AR1", "CP420");
s_enchash.put("EBCDIC-CP-HE", "CP424");
s_enchash.put("EBCDIC-CP-CH", "CP500");
s_enchash.put("EBCDIC-CP-ROECE", "CP870");
s_enchash.put("EBCDIC-CP-YU", "CP870");
s_enchash.put("EBCDIC-CP-IS", "CP871");
s_enchash.put("EBCDIC-CP-AR2", "CP918");
// j:CNS11643 -> EUC-TW?
// ISO-2022-CN? ISO-2022-CN-EXT?
s_revhash = new Hashtable();
// <Java encoding name>, <preferred MIME name>
s_revhash.put("UTF8", "UTF-8");
//s_revhash.put("8859_1", "US-ASCII"); // ?
s_revhash.put("8859_1", "ISO-8859-1");
s_revhash.put("8859_2", "ISO-8859-2");
s_revhash.put("8859_3", "ISO-8859-3");
s_revhash.put("8859_4", "ISO-8859-4");
s_revhash.put("8859_5", "ISO-8859-5");
s_revhash.put("8859_6", "ISO-8859-6");
s_revhash.put("8859_7", "ISO-8859-7");
s_revhash.put("8859_8", "ISO-8859-8");
s_revhash.put("8859_9", "ISO-8859-9");
s_revhash.put("JIS", "ISO-2022-JP");
s_revhash.put("SJIS", "Shift_JIS");
s_revhash.put("EUCJIS", "EUC-JP");
s_revhash.put("GB2312", "GB2312");
s_revhash.put("BIG5", "Big5");
s_revhash.put("KSC5601", "EUC-KR");
s_revhash.put("ISO2022KR", "ISO-2022-KR");
s_revhash.put("KOI8_R", "KOI8-R");
s_revhash.put("CP037", "EBCDIC-CP-US");
s_revhash.put("CP037", "EBCDIC-CP-CA");
s_revhash.put("CP037", "EBCDIC-CP-NL");
s_revhash.put("CP277", "EBCDIC-CP-DK");
s_revhash.put("CP277", "EBCDIC-CP-NO");
s_revhash.put("CP278", "EBCDIC-CP-FI");
s_revhash.put("CP278", "EBCDIC-CP-SE");
s_revhash.put("CP280", "EBCDIC-CP-IT");
s_revhash.put("CP284", "EBCDIC-CP-ES");
s_revhash.put("CP285", "EBCDIC-CP-GB");
s_revhash.put("CP297", "EBCDIC-CP-FR");
s_revhash.put("CP420", "EBCDIC-CP-AR1");
s_revhash.put("CP424", "EBCDIC-CP-HE");
s_revhash.put("CP500", "EBCDIC-CP-CH");
s_revhash.put("CP870", "EBCDIC-CP-ROECE");
s_revhash.put("CP870", "EBCDIC-CP-YU");
s_revhash.put("CP871", "EBCDIC-CP-IS");
s_revhash.put("CP918", "EBCDIC-CP-AR2");
}
private MIME2Java() {
}
/**
* Convert a MIME charset name, also known as an XML encoding name, to a Java
encoding name.
* @param mimeCharsetName Case insensitive MIME charset name: <code>UTF-8,
US-ASCII, ISO-8859-1,
* ISO-8859-2, ISO-8859-3, ISO-8859-4, ISO-8859-5,
ISO-8859-6,
* ISO-8859-7, ISO-8859-8, ISO-8859-9, ISO-2022-JP,
Shift_JIS,
* EUC-JP, GB2312, Big5, EUC-KR, ISO-2022-KR, KOI8-R,
* EBCDIC-CP-US, EBCDIC-CP-CA, EBCDIC-CP-NL,
EBCDIC-CP-DK,
* EBCDIC-CP-NO, EBCDIC-CP-FI, EBCDIC-CP-SE,
EBCDIC-CP-IT,
* EBCDIC-CP-ES, EBCDIC-CP-GB, EBCDIC-CP-FR,
EBCDIC-CP-AR1,
* EBCDIC-CP-HE, EBCDIC-CP-CH, EBCDIC-CP-ROECE,
EBCDIC-CP-YU,
* EBCDIC-CP-IS and EBCDIC-CP-AR2</code>.
* @return Java encoding name, or <var>null</var> if
<var>mimeCharsetName</var>
* is unknown.
* @see #reverse
*/
public static String convert(String mimeCharsetName) {
return (String)s_enchash.get(mimeCharsetName.toUpperCase());
}
/**
* Convert a Java encoding name to MIME charset name.
* Available values of <i>encoding</i> are "UTF8", "8859_1", "8859_2", "8859_3",
"8859_4",
* "8859_5", "8859_6", "8859_7", "8859_8", "8859_9", "JIS", "SJIS", "EUCJIS",
* "GB2312", "BIG5", "KSC5601", "ISO2022KR", "KOI8_R", "CP037", "CP277",
"CP278",
* "CP280", "CP284", "CP285", "CP297", "CP420", "CP424", "CP500", "CP870",
"CP871" and "CP918".
* @param encoding Case insensitive Java encoding name: <code>UTF8, 8859_1,
8859_2, 8859_3,
* 8859_4, 8859_5, 8859_6, 8859_7, 8859_8, 8859_9, JIS,
SJIS, EUCJIS,
* GB2312, BIG5, KSC5601, ISO2022KR, KOI8_R, CP037, CP277,
CP278,
* CP280, CP284, CP285, CP297, CP420, CP424, CP500, CP870,
CP871
* and CP918</code>.
* @return MIME charset name, or <var>null</var> if
<var>encoding</var> is unknown.
* @see #convert
*/
public static String reverse(String encoding) {
return (String)s_revhash.get(encoding.toUpperCase());
}
}
1.1 jakarta-slide/src/util/org/apache/util/URLUtil.java
Index: URLUtil.java
===================================================================
/*
* $Header: /home/cvs/jakarta-slide/src/util/org/apache/util/URLUtil.java,v 1.1
2001/02/15 17:37:30 remm Exp $
* $Revision: 1.1 $
* $Date: 2001/02/15 17:37:30 $
*
* ====================================================================
*
* 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 [EMAIL PROTECTED]
*
* 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.util;
import java.io.UnsupportedEncodingException;
import java.io.ByteArrayOutputStream;
import java.io.OutputStreamWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;
import java.util.TimeZone;
import java.util.BitSet;
/**
* General purpose request parsing and encoding utility methods.
*
* @author Craig R. McClanahan
* @author Tim Tye
* @author Remy Maucherat
* @version $Revision: 1.1 $ $Date: 2001/02/15 17:37:30 $
*/
public final class URLUtil {
// -------------------------------------------------------------- Variables
/**
* The DateFormat to use for generating readable dates in cookies.
*/
private static SimpleDateFormat format =
new SimpleDateFormat(" EEEE, dd-MMM-yy kk:mm:ss zz");
/**
* Array containing the safe characters set.
*/
protected static BitSet safeCharacters;
protected static final char[] hexadecimal =
{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F'};
// ----------------------------------------------------- Static Initializer
static {
format.setTimeZone(TimeZone.getTimeZone("GMT"));
safeCharacters = new BitSet(256);
int i;
for (i = 'a'; i <= 'z'; i++) {
safeCharacters.set(i);
}
for (i = 'A'; i <= 'Z'; i++) {
safeCharacters.set(i);
}
for (i = '0'; i <= '9'; i++) {
safeCharacters.set(i);
}
safeCharacters.set('-');
safeCharacters.set('_');
safeCharacters.set('.');
safeCharacters.set('*');
safeCharacters.set('/');
}
// --------------------------------------------------------- Public Methods
/**
* Decode and return the specified URL-encoded String.
* When the byte array is converted to a string, the system default
* character encoding is used... This may be different than some other
* servers.
*
* @param str The url-encoded string
*
* @exception IllegalArgumentException if a '%' character is not followed
* by a valid 2-digit hexadecimal number
*/
public static String URLDecode(String str) {
return URLDecode(str, null);
}
/**
* Decode and return the specified URL-encoded String.
*
* @param str The url-encoded string
* @param enc The encoding to use; if null, the default encoding is used
* @exception IllegalArgumentException if a '%' character is not followed
* by a valid 2-digit hexadecimal number
*/
public static String URLDecode(String str, String enc) {
if (str == null)
return (null);
int len = str.length();
byte[] bytes = new byte[len];
str.getBytes(0, len, bytes, 0);
return URLDecode(bytes, enc);
}
/**
* Decode and return the specified URL-encoded byte array.
*
* @param bytes The url-encoded byte array
* @exception IllegalArgumentException if a '%' character is not followed
* by a valid 2-digit hexadecimal number
*/
public static String URLDecode(byte[] bytes) {
return URLDecode(bytes, null);
}
/**
* Decode and return the specified URL-encoded byte array.
*
* @param bytes The url-encoded byte array
* @param enc The encoding to use; if null, the default encoding is used
* @exception IllegalArgumentException if a '%' character is not followed
* by a valid 2-digit hexadecimal number
*/
public static String URLDecode(byte[] bytes, String enc) {
if (bytes == null)
return (null);
int len = bytes.length;
int ix = 0;
int ox = 0;
while (ix < len) {
byte b = bytes[ix++]; // Get byte to test
if (b == '+') {
b = (byte)' ';
} else if (b == '%') {
b = (byte) ((convertHexDigit(bytes[ix++]) << 4)
+ convertHexDigit(bytes[ix++]));
}
bytes[ox++] = b;
}
if (enc != null) {
try {
return new String(bytes, 0, ox, enc);
} catch (Exception e) {
e.printStackTrace();
}
}
return new String(bytes, 0, ox);
}
/**
* Convert a byte character value to hexidecimal digit value.
*
* @param b the character value byte
*/
private static byte convertHexDigit( byte b ) {
if ((b >= '0') && (b <= '9')) return (byte)(b - '0');
if ((b >= 'a') && (b <= 'f')) return (byte)(b - 'a' + 10);
if ((b >= 'A') && (b <= 'F')) return (byte)(b - 'A' + 10);
return 0;
}
/**
* URL rewriter.
*
* @param path Path which has to be rewiten
*/
public static String URLEncode(String path, String enc) {
/**
* Note: This code portion is very similar to URLEncoder.encode.
* Unfortunately, there is no way to specify to the URLEncoder which
* characters should be encoded. Here, ' ' should be encoded as "%20"
* and '/' shouldn't be encoded.
*/
int maxBytesPerChar = 10;
int caseDiff = ('a' - 'A');
StringBuffer rewrittenPath = new StringBuffer(path.length());
ByteArrayOutputStream buf = new ByteArrayOutputStream(maxBytesPerChar);
OutputStreamWriter writer = null;
try {
// FIXME: Use the same encoding as the one specified above
writer = new OutputStreamWriter(buf, enc);
} catch (Exception e) {
e.printStackTrace();
writer = new OutputStreamWriter(buf);
}
for (int i = 0; i < path.length(); i++) {
int c = (int) path.charAt(i);
if (safeCharacters.get(c)) {
rewrittenPath.append((char)c);
} else {
// convert to external encoding before hex conversion
try {
writer.write(c);
writer.flush();
} catch(IOException e) {
buf.reset();
continue;
}
byte[] ba = buf.toByteArray();
for (int j = 0; j < ba.length; j++) {
// Converting each byte in the buffer
byte toEncode = ba[j];
rewrittenPath.append('%');
int low = (int) (toEncode & 0x0f);
int high = (int) ((toEncode & 0xf0) >> 4);
rewrittenPath.append(hexadecimal[high]);
rewrittenPath.append(hexadecimal[low]);
}
buf.reset();
}
}
return rewrittenPath.toString();
}
}
1.1 jakarta-slide/src/util/org/apache/util/XMLPrinter.java
Index: XMLPrinter.java
===================================================================
/*
* $Header: /home/cvs/jakarta-slide/src/util/org/apache/util/XMLPrinter.java,v 1.1
2001/02/15 17:37:30 remm Exp $
* $Revision: 1.1 $
* $Date: 2001/02/15 17:37:30 $
*
* ====================================================================
*
* 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 [EMAIL PROTECTED]
*
* 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.util;
import java.io.IOException;
import java.io.Writer;
/**
* WebdavXMLPrinter helper class.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Remy Maucherat</a>
*/
public class XMLPrinter {
// -------------------------------------------------------------- Constants
/**
* Opening tag.
*/
public static final int OPENING = 0;
/**
* Closing tag.
*/
public static final int CLOSING = 1;
/**
* Element with no content.
*/
public static final int NO_CONTENT = 2;
// ----------------------------------------------------- Instance Variables
/**
* Buffer.
*/
protected StringBuffer buffer = new StringBuffer();
/**
* Writer.
*/
protected Writer writer = null;
// ----------------------------------------------------------- Constructors
/**
* Constructor.
*/
public XMLPrinter() {
}
/**
* Constructor.
*/
public XMLPrinter(Writer writer) {
this.writer = writer;
}
// --------------------------------------------------------- Public Methods
/**
* Retrieve generated XML.
*
* @return String containing the generated XML
*/
public String toString() {
return buffer.toString();
}
/**
* Write property to the XML.
*
* @param namespace Namespace
* @param namespaceInfo Namespace info
* @param name Property name
* @param value Property value
*/
public void writeProperty(String namespace, String namespaceInfo,
String name, String value) {
writeElement(namespace, namespaceInfo, name, OPENING);
buffer.append(value);
writeElement(namespace, namespaceInfo, name, CLOSING);
}
/**
* Write property to the XML.
*
* @param namespace Namespace
* @param name Property name
* @param value Property value
*/
public void writeProperty(String namespace, String name, String value) {
writeElement(namespace, name, OPENING);
buffer.append(value);
writeElement(namespace, name, CLOSING);
}
/**
* Write property to the XML.
*
* @param namespace Namespace
* @param name Property name
*/
public void writeProperty(String namespace, String name) {
writeElement(namespace, name, NO_CONTENT);
}
/**
* Write an element.
*
* @param name Element name
* @param namespace Namespace abbreviation
* @param type Element type
*/
public void writeElement(String namespace, String name, int type) {
writeElement(namespace, null, name, type);
}
/**
* Write an element.
*
* @param namespace Namespace abbreviation
* @param namespaceInfo Namespace info
* @param name Element name
* @param type Element type
*/
public void writeElement(String namespace, String namespaceInfo,
String name, int type) {
if ((namespace != null) && (namespace.length() > 0)) {
switch (type) {
case OPENING:
if (namespaceInfo != null) {
buffer.append("<" + namespace + ":" + name + " xmlns:"
+ namespace + "=\""
+ namespaceInfo + "\">");
} else {
buffer.append("<" + namespace + ":" + name + ">");
}
break;
case CLOSING:
buffer.append("</" + namespace + ":" + name + ">\n");
break;
case NO_CONTENT:
default:
if (namespaceInfo != null) {
buffer.append("<" + namespace + ":" + name + " xmlns:"
+ namespace + "=\""
+ namespaceInfo + "\"/>");
} else {
buffer.append("<" + namespace + ":" + name + "/>");
}
break;
}
} else {
switch (type) {
case OPENING:
buffer.append("<" + name + ">");
break;
case CLOSING:
buffer.append("</" + name + ">\n");
break;
case NO_CONTENT:
default:
buffer.append("<" + name + "/>");
break;
}
}
}
/**
* Write text.
*
* @param text Text to append
*/
public void writeText(String text) {
buffer.append(text);
}
/**
* Write XML Header.
*/
public void writeXMLHeader() {
buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n");
}
/**
* Send data and reinitializes buffer.
*/
public void sendData()
throws IOException {
if (writer != null) {
writer.write(buffer.toString());
buffer = new StringBuffer();
}
}
}