[Httpunit-commit] CVS: httpunit/site/com/meterware/xml DocumentSemantics.java,1.3,1.4 WriteableXMLEl
Brought to you by:
russgold
|
From: Russell G. <rus...@us...> - 2003-06-17 11:15:06
|
Update of /cvsroot/httpunit/httpunit/site/com/meterware/xml
In directory sc8-pr-cvs1:/tmp/cvs-serv15190/site/com/meterware/xml
Modified Files:
DocumentSemantics.java WriteableXMLElement.java
Log Message:
Support XML rewriting
Index: DocumentSemantics.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/site/com/meterware/xml/DocumentSemantics.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- DocumentSemantics.java 27 May 2003 05:15:21 -0000 1.3
+++ DocumentSemantics.java 17 Jun 2003 11:15:03 -0000 1.4
@@ -35,6 +35,7 @@
import java.util.Date;
import java.io.File;
import java.io.IOException;
+import java.text.SimpleDateFormat;
/**
@@ -43,8 +44,10 @@
**/
public class DocumentSemantics {
+ private final static String LINE_BREAK = System.getProperty( "line.separator" );
private static final Class[] NO_ARG_CLASSES = new Class[0];
private static final Object[] NO_ARGS = new Object[0];
+ private static SimpleDateFormat DATE_FORMAT = new SimpleDateFormat( "d-MMM-yyyy" );
public static void build( Document document, Object documentRoot, String documentName ) {
try {
@@ -169,6 +172,7 @@
final PropertyDescriptor propertyDescriptor = getProperty( beanInfo, propertyName );
if (propertyDescriptor == null) return "";
Object result = propertyDescriptor.getReadMethod().invoke( template, NO_ARGS );
+ if (result instanceof Date) result = DATE_FORMAT.format( (Date) result );
return result == null ? "" : result.toString();
} catch (Exception e) {
e.printStackTrace();
@@ -182,6 +186,52 @@
return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse( file );
} catch (ParserConfigurationException e) {
throw new RuntimeException( "Unable to create a parser for XML documents: " + e );
+ }
+ }
+
+
+ public static String generateXML( WriteableRootElement node ) {
+ StringBuffer sb = new StringBuffer( "<?xml version='1.0' ?>" ).append( LINE_BREAK );
+ appendElement( sb, node, "" );
+ return sb.toString();
+ }
+
+
+ private static void appendElement( StringBuffer sb, WriteableXMLElement element, String prefix ) {
+ sb.append( prefix ).append( '<' ).append( element.getElementName() );
+ String[] attributeNames = element.getAttributeNames();
+ for (int i = 0; i < attributeNames.length; i++) {
+ String attributeName = attributeNames[i];
+ if (element.isExplicitAttribute( attributeName )) {
+ sb.append( ' ' ).append( attributeName ).append( "='" ).append( getStringProperty( attributeName, element ) ).append( "'" );
+ }
+ }
+ boolean isEmpty = true;
+ final String contents = element.getContents();
+ if (contents != null) {
+ isEmpty = false;
+ sb.append( '>' );
+ sb.append( contents );
+ sb.append( "</" ).append( element.getElementName() ).append( '>' ).append( LINE_BREAK );
+ } else {
+ String[] nestedElementNames = element.getNestedElementNames();
+ for (int i = 0; i < nestedElementNames.length; i++) {
+ String nestedElementName = nestedElementNames[i];
+ WriteableXMLElement[] children = element.getNestedElements( nestedElementName );
+ for (int j = 0; j < children.length; j++) {
+ WriteableXMLElement child = children[j];
+ if (isEmpty) {
+ isEmpty = false;
+ sb.append( '>' ).append( LINE_BREAK );
+ }
+ appendElement( sb, child, prefix + " " );
+ }
+ }
+ if (isEmpty) {
+ sb.append( "/>" ).append( LINE_BREAK );
+ } else {
+ sb.append( prefix ).append( "</" ).append( element.getElementName() ).append( '>' ).append( LINE_BREAK );
+ }
}
}
Index: WriteableXMLElement.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/site/com/meterware/xml/WriteableXMLElement.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- WriteableXMLElement.java 18 May 2003 05:20:47 -0000 1.1
+++ WriteableXMLElement.java 17 Jun 2003 11:15:03 -0000 1.2
@@ -27,39 +27,38 @@
public interface WriteableXMLElement {
/**
- * Returns the names of the attributes of this element in the order in which they should be displayed.
+ * Returns the name of this element.
*/
- String[] getAttributeNames();
+ String getElementName();
/**
- * Returns the names of the nested elements of this element in the order in which they should be displayed.
+ * Returns the names of the attributes of this element in the order in which they should be displayed.
*/
- String[] getNestedElementNames();
+ String[] getAttributeNames();
/**
- * Returns all nested elements with the specified name.
+ * Returns true if the specified attribute should be written. Some attributes may be left implicit if they have
+ * their default values.
*/
- WriteableXMLElement[] getNestedElements( String elementName );
+ boolean isExplicitAttribute( String attributeName );
/**
- * Returns any text contents of this element.
+ * Returns the names of the nested elements of this element in the order in which they should be displayed.
*/
- String getContents();
+ String[] getNestedElementNames();
/**
- * Returns true if the specified attribute should be written. Some attributes may be left implicit if they have
- * their default values.
+ * Returns all nested elements with the specified name.
*/
- boolean isExplicitAttribute( String attributeName );
+ WriteableXMLElement[] getNestedElements( String elementName );
/**
- * Resets the specified attribute to its default value. Implementations may use this rather than an explicit
- * setter to decide whether to make an attribute explicit.
+ * Returns any text contents of this element.
*/
- void resetAttribute( String attributeName );
+ String getContents();
}
|