You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by je...@locus.apache.org on 2000/06/22 17:48:15 UTC
cvs commit: xml-xerces/java/samples/dom Features.java Arguments.java DOMCount.java DOMFilter.java DOMParserWrapper.java DOMWriter.java
jeffreyr 00/06/22 08:48:12
Modified: java/samples/dom Arguments.java DOMCount.java DOMFilter.java
DOMParserWrapper.java DOMWriter.java
Added: java/samples/dom Features.java
Log:
added argopt like class, added switch to turn on/off validation, namespace, schema support, etc.
Revision Changes Path
1.2 +31 -4 xml-xerces/java/samples/dom/Arguments.java
Index: Arguments.java
===================================================================
RCS file: /home/cvs/xml-xerces/java/samples/dom/Arguments.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Arguments.java 2000/06/22 05:30:58 1.1
+++ Arguments.java 2000/06/22 15:47:36 1.2
@@ -101,6 +101,7 @@
int theDash = 0;
int lengthOfToken = 0;
char []bufferOfToken = null;
+ Object[] temp;
for ( int i = 0; i<arguments.length; i++ ){
bufferOfToken = arguments[i].toCharArray();
lengthOfToken = bufferOfToken.length;
@@ -109,9 +110,34 @@
stackOfOptions.push( (Object ) new Integer(bufferOfToken[j] ));
}
} else{
- argumentList.addElement( arguments[i] );
+ argumentList.push( arguments[i] );
+ //argumentList.addElement( arguments[i] );
+ // temp.addElement( arguments[i] );
}
}
+
+ //temp = argumentList.toArray();
+
+ //while( ! argumentList.isEmpty() ){
+ // System.out.println( "remove" );
+ // argumentList.pop();
+ // }
+
+ //argumentList.removeAllElements();
+ // for( int j = temp.length -1 ; j>=0; j--){
+ //for( int j = 0; j < temp.length ; j++ )
+ // System.out.println( "elem = " + temp[j] );
+ // argumentList.push( temp[j] );
+ //}
+
+ // for( int j = 0; j < argumentList.size() ; j++ ) {
+
+ // System.out.println( "elem = " + (String )argumentList.elementAt( j) );
+ //}
+
+
+
+
}
/**
@@ -191,6 +217,10 @@
tst.parseArgumentTokens(argv);
while ( (c = tst.getArguments()) != -1 ){
switch (c) {
+ case 'e':
+ System.out.println( "e = " + tst.getStringParameter() );
+ break;
+
case 'v':
System.out.println( "v" );
break;
@@ -222,9 +252,6 @@
case 'h':
case '-':
tst.printUsage();
- break;
- case 'e':
- System.out.println( "e = " + tst.getStringParameter() );
break;
default:
break;
1.2 +71 -33 xml-xerces/java/samples/dom/DOMCount.java
Index: DOMCount.java
===================================================================
RCS file: /home/cvs/xml-xerces/java/samples/dom/DOMCount.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DOMCount.java 1999/11/09 01:13:55 1.1
+++ DOMCount.java 2000/06/22 15:47:37 1.2
@@ -85,6 +85,13 @@
private static final String
DEFAULT_PARSER_NAME = "dom.wrappers.DOMParser";
+ private static boolean setValidation = false; //defaults
+ private static boolean setNameSpaces = true;
+ private static boolean setSchemaSupport = true;
+ private static boolean setDeferredDOM = true;
+
+
+
//
// Data
//
@@ -101,6 +108,7 @@
/** Ignorable whitespace. */
private long ignorableWhitespace;
+
//
// Public static methods
//
@@ -113,6 +121,9 @@
(DOMParserWrapper)Class.forName(parserWrapperName).newInstance();
DOMCount counter = new DOMCount();
long before = System.currentTimeMillis();
+ parser.setFeatures( new Features( setValidation, setNameSpaces,
+ setSchemaSupport, setDeferredDOM ) );
+
Document document = parser.parse(uri);
counter.traverse(document);
long after = System.currentTimeMillis();
@@ -230,53 +241,80 @@
/** Main program entry point. */
public static void main(String argv[]) {
+ Arguments argopt = new Arguments();
+ argopt.setUsage( new String[] {
+ "usage: java dom.DOMCount (options) uri ...",
+ "",
+ "options:",
+ " -p name Specify DOM parser wrapper by name.",
+ " Default parser: ",
+ " -n turn on Namespace - default",
+ " -v turn on Validation - default",
+ " -s turn on Schema support - default",
+ " -N turn off Namespace",
+ " -V turn off Validation",
+ " -S turn off Schema validation",
+ " -h This help screen." } );
+
// is there anything to do?
if (argv.length == 0) {
- printUsage();
+ argopt.printUsage();
System.exit(1);
}
// vars
String parserName = DEFAULT_PARSER_NAME;
- // check parameters
- for (int i = 0; i < argv.length; i++) {
- String arg = argv[i];
-
- // options
- if (arg.startsWith("-")) {
- if (arg.equals("-p")) {
- if (i == argv.length - 1) {
- System.err.println("error: missing parser name");
- System.exit(1);
- }
- parserName = argv[++i];
- continue;
- }
+ argopt.parseArgumentTokens(argv);
- if (arg.equals("-h")) {
- printUsage();
- System.exit(1);
- }
+ int c;
+ while ( (c = argopt.getArguments()) != -1 ){
+ switch (c) {
+ case 'v':
+ setValidation = true;
+ break;
+ case 'V':
+ setValidation = false;
+ break;
+ case 'N':
+ setNameSpaces = false;
+ break;
+ case 'n':
+ setNameSpaces = true;
+ break;
+ case 'p':
+ parserName = argopt.getStringParameter();
+ break;
+ case 'd':
+ setDeferredDOM = true;
+ break;
+ case 'D':
+ setDeferredDOM = false;
+ break;
+ case 's':
+ System.out.println( "s" );
+ break;
+ case 'S':
+ System.out.println( "S" );
+ break;
+ case '?':
+ case 'h':
+ case '-':
+ argopt.printUsage();
+ System.exit(1);
+ break;
+ default:
+ break;
}
+ }
- // count uri
- count(parserName, arg);
+ // count uri
+
+ for( int j = 0; j<argopt.stringParameterLeft(); j++){
+ count(parserName, argopt.getStringParameter());
}
} // main(String[])
-
- /** Prints the usage. */
- private static void printUsage() {
-
- System.err.println("usage: java dom.DOMCount (options) uri ...");
- System.err.println();
- System.err.println("options:");
- System.err.println(" -p name Specify DOM parser wrapper by name.");
- System.err.println(" Default parser: "+DEFAULT_PARSER_NAME);
- System.err.println(" -h This help screen.");
-
- } // printUsage()
} // class DOMCount
1.2 +84 -53 xml-xerces/java/samples/dom/DOMFilter.java
Index: DOMFilter.java
===================================================================
RCS file: /home/cvs/xml-xerces/java/samples/dom/DOMFilter.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DOMFilter.java 1999/11/09 01:13:55 1.1
+++ DOMFilter.java 2000/06/22 15:47:37 1.2
@@ -85,6 +85,13 @@
private static final String
DEFAULT_PARSER_NAME = "dom.wrappers.DOMParser";
+ private static boolean setValidation = false; //defaults
+ private static boolean setNameSpaces = true;
+ private static boolean setSchemaSupport = true;
+ private static boolean setDeferredDOM = true;
+
+
+
//
// Public static methods
//
@@ -97,6 +104,9 @@
// parse document
DOMParserWrapper parser =
(DOMParserWrapper)Class.forName(parserWrapperName).newInstance();
+ parser.setFeatures( new Features( setValidation, setNameSpaces,
+ setSchemaSupport, setDeferredDOM ) );
+
Document document = parser.parse(uri);
// get elements that match
@@ -218,9 +228,27 @@
/** Main program entry point. */
public static void main(String argv[]) {
+ Arguments argopt = new Arguments();
+ argopt.setUsage( new String[]
+ { "usage: java dom.DOMFilter (options) uri ...","",
+ "options:",
+ " -p name Specify DOM parser wrapper by name.",
+ " Default parser: "+DEFAULT_PARSER_NAME,
+ " -e name Specify element name to search for. Default is \"*\".",
+ " -a name Specify attribute name of specified elements.",
+ " -n turn on Namespace - default",
+ " -v turn on Validation - default",
+ " -s turn on Schema support - default",
+ " -d turn on Deferred DOM - default",
+ " -N turn off Namespace",
+ " -V turn off Validation",
+ " -S turn off Schema validation",
+ " -D turn off Deferred DOM",
+ " -h This help screen." } );
+
// is there anything to do?
if (argv.length == 0) {
- printUsage();
+ argopt.printUsage();
System.exit(1);
}
@@ -229,64 +257,67 @@
String elementName = "*"; // all elements
String attributeName = null;
- // check parameters
- for (int i = 0; i < argv.length; i++) {
- String arg = argv[i];
+ /////
- // options
- if (arg.startsWith("-")) {
- if (arg.equals("-p")) {
- if (i == argv.length - 1) {
- System.err.println("error: missing parser name");
- System.exit(1);
- }
- parserName = argv[++i];
- continue;
- }
-
- if (arg.equals("-e")) {
- if (i == argv.length - 1) {
- System.err.println("error: missing element name");
- System.exit(1);
- }
- elementName = argv[++i];
- continue;
- }
-
- if (arg.equals("-a")) {
- if (i == argv.length - 1) {
- System.err.println("error: missing attribute name");
- System.exit(1);
- }
- attributeName = argv[++i];
- continue;
- }
-
- if (arg.equals("-h")) {
- printUsage();
- System.exit(1);
- }
+ int c;
+ while ( (c = argopt.getArguments()) != -1 ){
+ switch (c) {
+ case 'v':
+ setValidation = true;
+ break;
+ case 'V':
+ setValidation = false;
+ break;
+ case 'N':
+ setNameSpaces = false;
+ break;
+ case 'n':
+ setNameSpaces = true;
+ break;
+ case 'p':
+ parserName = argopt.getStringParameter();
+ break;
+ case 'd':
+ setDeferredDOM = true;
+ break;
+ case 'D':
+ setDeferredDOM = false;
+ break;
+ case 's':
+ System.out.println( "s" );
+ break;
+ case 'S':
+ System.out.println( "S" );
+ break;
+ case 'e':
+ elementName = argopt.getStringParameter();
+ break;
+ case 'a':
+ attributeName = argopt.getStringParameter();
+ break;
+ case '?':
+ case 'h':
+ case '-':
+ argopt.printUsage();
+ System.exit(1);
+ break;
+ default:
+ break;
}
-
- // print uri
- System.err.println(arg+':');
- print(parserName, arg, elementName, attributeName);
}
- } // main(String[])
+ // count uri
+
+ String argument = argopt.getStringParameter();
+ ////
- /** Prints the usage. */
- private static void printUsage() {
- System.err.println("usage: java dom.DOMFilter (options) uri ...");
- System.err.println();
- System.err.println("options:");
- System.err.println(" -p name Specify DOM parser wrapper by name.");
- System.err.println(" Default parser: "+DEFAULT_PARSER_NAME);
- System.err.println(" -e name Specify element name to search for. Default is \"*\".");
- System.err.println(" -a name Specify attribute name of specified elements.");
- System.err.println(" -h This help screen.");
+ // check parameters
+
+ // print uri
+ System.err.println(argument+':');
+ print(parserName, argument, elementName, attributeName);
- } // printUsage()
+ } // main(String[])
} // class DOMFilter
1.2 +2 -0 xml-xerces/java/samples/dom/DOMParserWrapper.java
Index: DOMParserWrapper.java
===================================================================
RCS file: /home/cvs/xml-xerces/java/samples/dom/DOMParserWrapper.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DOMParserWrapper.java 1999/11/09 01:13:55 1.1
+++ DOMParserWrapper.java 2000/06/22 15:47:38 1.2
@@ -73,4 +73,6 @@
/** Parses the specified URI and returns the document. */
public Document parse(String uri) throws Exception;
+ public void setFeatures(Features feature );
+
} // interface DOMParserWrapper
1.2 +371 -341 xml-xerces/java/samples/dom/DOMWriter.java
Index: DOMWriter.java
===================================================================
RCS file: /home/cvs/xml-xerces/java/samples/dom/DOMWriter.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DOMWriter.java 1999/11/09 01:13:56 1.1
+++ DOMWriter.java 2000/06/22 15:47:38 1.2
@@ -78,30 +78,39 @@
*/
public class DOMWriter {
- //
- // Constants
- //
-
- /** Default parser name. */
- private static final String
- DEFAULT_PARSER_NAME = "dom.wrappers.DOMParser";
-
- //
- // Data
- //
-
- /** Default Encoding */
- private static String
- PRINTWRITER_ENCODING = "UTF8";
+ //
+ // Constants
+ //
- private static String MIME2JAVA_ENCODINGS[] =
+
+ /** Default parser name. */
+ private static final String
+ DEFAULT_PARSER_NAME = "dom.wrappers.DOMParser";
+
+
+ private static boolean setValidation = false; //defaults
+ private static boolean setNameSpaces = true;
+ private static boolean setSchemaSupport = true;
+ private static boolean setDeferredDOM = true;
+
+
+
+ //
+ // 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"
+ "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"
};
@@ -133,353 +142,374 @@
"MacUkraine", "SJIS", "Unicode", "UnicodeBig", "UnicodeLittle", "UTF8"};
*/
- /** Print writer. */
- protected PrintWriter out;
+ /** Print writer. */
+ protected PrintWriter out;
- /** Canonical output. */
- protected boolean canonical;
+ /** 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 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 resulting document tree. */
- public static void print(String parserWrapperName, String uri,
- boolean canonical ) {
- try {
- DOMParserWrapper parser =
- (DOMParserWrapper)Class.forName(parserWrapperName).newInstance();
- Document document = parser.parse(uri);
- DOMWriter writer = new DOMWriter(canonical);
- writer.print(document);
- } catch ( Exception e ) {
- //e.printStackTrace(System.err);
- }
-
- } // print(String,String,boolean)
-
-
- /** 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;
+ 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 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 resulting document tree. */
+ public static void print(String parserWrapperName, String uri,
+ boolean canonical ) {
+ try {
+ DOMParserWrapper parser =
+ (DOMParserWrapper)Class.forName(parserWrapperName).newInstance();
+ parser.setFeatures( new Features( setValidation, setNameSpaces,
+ setSchemaSupport, setDeferredDOM ) );
+
+ Document document = parser.parse(uri);
+ DOMWriter writer = new DOMWriter(canonical);
+ writer.print(document);
+ } catch ( Exception e ) {
+ //e.printStackTrace(System.err);
+ }
+
+ } // print(String,String,boolean)
+
+
+ /** 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;
+ 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;
+ 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;
+ 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;
+ 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[]
-
-
- //
- // Main
- //
-
- /** Main program entry point. */
- public static void main(String argv[]) {
-
- // is there anything to do?
- if ( argv.length == 0 ) {
- printUsage();
- System.exit(1);
- }
-
- // vars
- String parserName = DEFAULT_PARSER_NAME;
- boolean canonical = false;
- String encoding = "UTF8"; // default encoding
-
- // check parameters
- for ( int i = 0; i < argv.length; i++ ) {
- String arg = argv[i];
-
- // options
- if ( arg.startsWith("-") ) {
- if ( arg.equals("-p") ) {
- if ( i == argv.length - 1 ) {
- System.err.println("error: missing parser name");
- System.exit(1);
- }
- parserName = argv[++i];
- continue;
- }
-
- if ( arg.equals("-c") ) {
- canonical = true;
- continue;
- }
-
- if ( arg.equals("-h") ) {
- printUsage();
- System.exit(1);
- }
-
- if ( arg.equals("-e") ) {
- if ( i == argv.length - 1 ) {
- System.err.println("error: missing encoding name");
- printValidJavaEncoding();
- System.exit(1);
- } else {
- encoding = argv[++i];
- if ( isValidJavaEncoding( encoding ) )
+ 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[]
+
+
+ //
+ // Main
+ //
+
+ /** Main program entry point. */
+ public static void main(String argv[]) {
+ Arguments argopt = new Arguments();
+ argopt.setUsage( new String[] {
+ "usage: java dom.DOMWriter (options) uri ...","",
+ "options:",
+ " -p name Specify DOM parser wrapper by name.",
+ " Default parser: "+DEFAULT_PARSER_NAME,
+ " -c Canonical XML output.",
+ " -n turn on Namespace - default",
+ " -v turn on Validation - default",
+ " -s turn on Schema support - default",
+ " -d turn on Deferred DOM - default",
+ " -N turn off Namespace",
+ " -V turn off Validation",
+ " -S turn off Schema validation",
+ " -D turn off Deferred DOM",
+ " -h This help screen.",
+ " -e Output Java Encoding.",
+ " Default encoding: UTF-8"} );
+
+
+
+ // is there anything to do?
+ if ( argv.length == 0 ) {
+ argopt.printUsage();
+ System.exit(1);
+ }
+
+ // vars
+ String parserName = DEFAULT_PARSER_NAME;
+ boolean canonical = false;
+ String encoding = "UTF8"; // default encoding
+
+ argopt.parseArgumentTokens(argv);
+
+ int c;
+ while ( (c = argopt.getArguments()) != -1 ){
+ switch (c) {
+ case 'c':
+ canonical = true;
+ break;
+ case 'e':
+ encoding = argopt.getStringParameter();
+ System.out.println(" encoding = " + encoding );
+ if ( encoding != null && isValidJavaEncoding( encoding ) )
setWriterEncoding( encoding );
- else {
+ else {
printValidJavaEncoding();
System.exit( 1 );
- }
- }
- continue;
- }
-
- }
-
- // print uri
- System.err.println(arg+':');
- print(parserName, arg, canonical );
- System.err.println();
- }
-
- } // main(String[])
-
-
- /** 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 ) {
+ }
+ break;
+ case 'v':
+ setValidation = true;
+ break;
+ case 'V':
+ setValidation = false;
+ break;
+ case 'N':
+ setNameSpaces = false;
+ break;
+ case 'n':
+ setNameSpaces = true;
+ break;
+ case 'p':
+ parserName = argopt.getStringParameter();
+ System.out.println( "parserNam =" + parserName );
+ break;
+ case 'd':
+ setDeferredDOM = true;
+ break;
+ case 'D':
+ setDeferredDOM = false;
+ break;
+ case 's':
+ System.out.println( "s" );
+ break;
+ case 'S':
+ System.out.println( "S" );
+ break;
+ case '?':
+ case 'h':
+ case '-':
+ argopt.printUsage();
+ System.exit(1);
+ break;
+ default:
+ break;
+ }
+ }
+
+ // print uri
+ String argument = argopt.getStringParameter();
+ System.err.println(argument+':');
+ print(parserName, argument, canonical );
+ System.err.println();
+ } // main(String[])
+
+
+ /** 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;
- }
+ str.append("<");
+ break;
+ }
case '>': {
- str.append(">");
- break;
- }
+ str.append(">");
+ break;
+ }
case '&': {
- str.append("&");
- break;
- }
+ str.append("&");
+ break;
+ }
case '"': {
- str.append(""");
- break;
- }
+ str.append(""");
+ break;
+ }
case '\r':
case '\n': {
- if ( canonical ) {
- str.append("&#");
- str.append(Integer.toString(ch));
- str.append(';');
- break;
- }
- // else, default append char
- }
+ 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
-
- /** Prints the usage. */
- private static void printUsage() {
-
- System.err.println("usage: java dom.DOMWriter (options) uri ...");
- System.err.println();
- System.err.println("options:");
- System.err.println(" -p name Specify DOM parser wrapper by name.");
- System.err.println(" Default parser: "+DEFAULT_PARSER_NAME);
- System.err.println(" -c Canonical XML output.");
- System.err.println(" -h This help screen.");
- System.err.println(" -e Output Java Encoding.");
- System.err.println(" Default encoding: UTF-8");
-
- } // printUsage()
-
- 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( " " );
- }
- }
+ 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()
+ } // printJavaEncoding()
}
1.1 xml-xerces/java/samples/dom/Features.java
Index: Features.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 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 (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 dom;
public class Features {
private boolean setValidation = true;
private boolean setNameSpaces = true;
private boolean setSchemaSupport = true;
private boolean setDeferredDOM = true;
public Features(){
}
public Features( boolean validation, boolean namespace, boolean schemasupport, boolean deferredDOM ){
setValidationSet( validation );
setNamespaceSet( namespace );
setSchemasupportSet( schemasupport );
setDeferredDOMSet( deferredDOM );
}
public void setValidationSet( boolean validation ){
setValidation = validation;
}
public void setNamespaceSet( boolean namespace ){
setNameSpaces = namespace;
}
public void setSchemasupportSet( boolean schemasupport ){
setSchemaSupport = schemasupport;
}
public void setDeferredDOMSet( boolean deferredDOM ){
setDeferredDOM = deferredDOM;
}
public boolean isValidationSet(){
return setValidation;
}
public boolean isNamespaceSet(){
return setNameSpaces;
}
public boolean isSchemasupportSet(){
return setSchemaSupport;
}
public boolean isDeferredDOMSet(){
return setDeferredDOM;
}
}