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 20:36:42 UTC
cvs commit: xml-xerces/java/samples/dom DOMCount.java DOMFilter.java DOMParserWrapper.java DOMWriter.java
jeffreyr 00/06/22 11:36:40
Modified: java/samples/dom DOMCount.java DOMFilter.java
DOMParserWrapper.java DOMWriter.java
Log:
samples update
Revision Changes Path
1.5 +39 -35 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.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- DOMCount.java 2000/06/22 17:55:42 1.4
+++ DOMCount.java 2000/06/22 18:36:39 1.5
@@ -2,7 +2,7 @@
* The Apache Software License, Version 1.1
*
*
- * Copyright (c) 1999 The Apache Software Foundation. All rights
+ * Copyright (c) 1999, 2000 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -73,7 +73,7 @@
* A sample DOM counter. This sample program illustrates how to
* traverse a DOM tree in order to information about the document.
*
- * @version
+ * @version $id$
*/
public class DOMCount {
@@ -83,7 +83,7 @@
/** Default parser name. */
private static final String
- DEFAULT_PARSER_NAME = "dom.wrappers.DOMParser";
+ DEFAULT_PARSER_NAME = "dom.wrappers.DOMParser";
private static boolean setValidation = false; //defaults
private static boolean setNameSpaces = true;
@@ -118,26 +118,30 @@
try {
DOMParserWrapper parser =
- (DOMParserWrapper)Class.forName(parserWrapperName).newInstance();
+ (DOMParserWrapper)Class.forName(parserWrapperName).newInstance();
DOMCount counter = new DOMCount();
long before = System.currentTimeMillis();
- parser.setFeatures( new Features( setValidation, setNameSpaces,
- setSchemaSupport, setDeferredDOM ) );
+ parser.setFeature( "http://apache.org/xml/features/dom/defer-node-expansion",
+ setDeferredDOM );
+ parser.setFeature( "http://xml.org/sax/features/validation",
+ setValidation );
+ parser.setFeature( "http://xml.org/sax/features/namespaces",
+ setNameSpaces );
+ parser.setFeature( "http://apache.org/xml/features/validation/schema",
+ setSchemaSupport );
+
Document document = parser.parse(uri);
counter.traverse(document);
long after = System.currentTimeMillis();
counter.printResults(uri, after - before);
- }
- catch (org.xml.sax.SAXParseException spe) {
- }
- catch (org.xml.sax.SAXException se) {
+ } catch (org.xml.sax.SAXParseException spe) {
+ } catch (org.xml.sax.SAXException se) {
if (se.getException() != null)
se.getException().printStackTrace(System.err);
else
se.printStackTrace(System.err);
- }
- catch (Exception e) {
+ } catch (Exception e) {
e.printStackTrace(System.err);
}
@@ -157,8 +161,8 @@
int type = node.getNodeType();
switch (type) {
- // print document
- case Node.DOCUMENT_NODE: {
+ // print document
+ case Node.DOCUMENT_NODE: {
elements = 0;
attributes = 0;
characters = 0;
@@ -168,7 +172,7 @@
}
// print element with attributes
- case Node.ELEMENT_NODE: {
+ case Node.ELEMENT_NODE: {
elements++;
NamedNodeMap attrs = node.getAttributes();
if (attrs != null) {
@@ -185,7 +189,7 @@
}
// handle entity reference nodes
- case Node.ENTITY_REFERENCE_NODE: {
+ case Node.ENTITY_REFERENCE_NODE: {
NodeList children = node.getChildNodes();
if (children != null) {
int len = children.getLength();
@@ -197,11 +201,11 @@
}
// print text
- case Node.CDATA_SECTION_NODE: {
+ case Node.CDATA_SECTION_NODE: {
characters += node.getNodeValue().length();
break;
}
- case Node.TEXT_NODE: {
+ case Node.TEXT_NODE: {
if (node instanceof TextImpl) {
if (((TextImpl)node).isIgnorableWhitespace())
ignorableWhitespace += node.getNodeValue().length();
@@ -244,20 +248,20 @@
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",
- " -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." } );
+ "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",
+ " -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?
@@ -313,9 +317,9 @@
}
// count uri
-
- for( int j = 0; j<argopt.stringParameterLeft(); j++){
- count(parserName, argopt.getStringParameter());
+
+ for ( int j = 0; j<argopt.stringParameterLeft(); j++){
+ count(parserName, argopt.getStringParameter());
}
} // main(String[])
1.4 +45 -34 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.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- DOMFilter.java 2000/06/22 16:29:39 1.3
+++ DOMFilter.java 2000/06/22 18:36:39 1.4
@@ -2,7 +2,7 @@
* The Apache Software License, Version 1.1
*
*
- * Copyright (c) 1999 The Apache Software Foundation. All rights
+ * Copyright (c) 1999, 2000 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -56,7 +56,7 @@
*/
package dom;
-
+
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
@@ -73,7 +73,7 @@
* use the Document#getElementsByTagName() method to quickly
* and easily locate elements by name.
*
- * @version
+ * @version $Id: DOMFilter.java,v 1.4 2000/06/22 18:36:39 jeffreyr Exp $
*/
public class DOMFilter {
@@ -83,7 +83,7 @@
/** Default parser name. */
private static final String
- DEFAULT_PARSER_NAME = "dom.wrappers.DOMParser";
+ DEFAULT_PARSER_NAME = "dom.wrappers.DOMParser";
private static boolean setValidation = false; //defaults
private static boolean setNameSpaces = true;
@@ -103,10 +103,22 @@
try {
// parse document
DOMParserWrapper parser =
- (DOMParserWrapper)Class.forName(parserWrapperName).newInstance();
- parser.setFeatures( new Features( setValidation, setNameSpaces,
- setSchemaSupport, setDeferredDOM ) );
+ (DOMParserWrapper)Class.forName(parserWrapperName).newInstance();
+
+ try {
+ parser.setFeature( "http://apache.org/xml/features/dom/defer-node-expansion",
+ setDeferredDOM );
+ parser.setFeature( "http://xml.org/sax/features/validation",
+ setValidation );
+ parser.setFeature( "http://xml.org/sax/features/namespaces",
+ setNameSpaces );
+ parser.setFeature( "http://apache.org/xml/features/validation/schema",
+ setSchemaSupport );
+ } catch (SAXException e) {
+ System.out.println("error in setting up parser feature");
+ }
+
Document document = parser.parse(uri);
// get elements that match
@@ -114,8 +126,7 @@
// print nodes
print(elements, attributeName);
- }
- catch (Exception e) {
+ } catch (Exception e) {
e.printStackTrace(System.err);
}
@@ -188,30 +199,30 @@
for (int i = 0; i < len; i++) {
char ch = s.charAt(i);
switch (ch) {
- case '<': {
+ case '<': {
str.append("<");
break;
}
- case '>': {
+ case '>': {
str.append(">");
break;
}
- case '&': {
+ case '&': {
str.append("&");
break;
}
- case '"': {
+ case '"': {
str.append(""");
break;
}
- case '\r':
- case '\n': {
+ case '\r':
+ case '\n': {
str.append("&#");
str.append(Integer.toString(ch));
str.append(';');
break;
}
- default: {
+ default: {
str.append(ch);
}
}
@@ -230,21 +241,21 @@
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." } );
+ { "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) {
@@ -307,7 +318,7 @@
}
// count uri
-
+
String argument = argopt.getStringParameter();
////
@@ -315,8 +326,8 @@
// check parameters
// print uri
- System.err.println(argument+':');
- print(parserName, argument, elementName, attributeName);
+ System.err.println(argument+':');
+ print(parserName, argument, elementName, attributeName);
} // main(String[])
1.3 +23 -3 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.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- DOMParserWrapper.java 2000/06/22 15:47:38 1.2
+++ DOMParserWrapper.java 2000/06/22 18:36:39 1.3
@@ -2,7 +2,7 @@
* The Apache Software License, Version 1.1
*
*
- * Copyright (c) 1999 The Apache Software Foundation. All rights
+ * Copyright (c) 1999, 2000 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -62,7 +62,7 @@
/**
* Encapsulates a DOM parser.
*
- * @version
+ * @version $id$
*/
public interface DOMParserWrapper {
@@ -73,6 +73,26 @@
/** Parses the specified URI and returns the document. */
public Document parse(String uri) throws Exception;
- public void setFeatures(Features feature );
+ /**
+ * Set the state of a feature.
+ *
+ * Set the state of any feature in a SAX2 parser. The parser
+ * might not recognize the feature, and if it does recognize
+ * it, it might not be able to fulfill the request.
+ *
+ * @param featureId The unique identifier (URI) of the feature.
+ * @param state The requested state of the feature (true or false).
+ *
+ * @exception org.xml.sax.SAXNotRecognizedException If the
+ * requested feature is not known.
+ * @exception org.xml.sax.SAXNotSupportedException If the
+ * requested feature is known, but the requested
+ * state is not supported.
+ * @exception org.xml.sax.SAXException If there is any other
+ * problem fulfilling the request.
+ */
+
+ public void setFeature(String featureId, boolean state)
+ throws SAXNotRecognizedException, SAXNotSupportedException;
} // interface DOMParserWrapper
1.4 +11 -4 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.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- DOMWriter.java 2000/06/22 16:29:40 1.3
+++ DOMWriter.java 2000/06/22 18:36:39 1.4
@@ -2,7 +2,7 @@
* The Apache Software License, Version 1.1
*
*
- * Copyright (c) 1999 The Apache Software Foundation. All rights
+ * Copyright (c) 1999, 2000 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -74,7 +74,7 @@
* A sample DOM writer. This sample program illustrates how to
* traverse a DOM tree in order to print a document that is parsed.
*
- * @version
+ * @version $Id: DOMWriter.java,v 1.4 2000/06/22 18:36:39 jeffreyr Exp $
*/
public class DOMWriter {
@@ -194,8 +194,15 @@
try {
DOMParserWrapper parser =
(DOMParserWrapper)Class.forName(parserWrapperName).newInstance();
- parser.setFeatures( new Features( setValidation, setNameSpaces,
- setSchemaSupport, setDeferredDOM ) );
+
+ parser.setFeature( "http://apache.org/xml/features/dom/defer-node-expansion",
+ setDeferredDOM );
+ parser.setFeature( "http://xml.org/sax/features/validation",
+ setValidation );
+ parser.setFeature( "http://xml.org/sax/features/namespaces",
+ setNameSpaces );
+ parser.setFeature( "http://apache.org/xml/features/validation/schema",
+ setSchemaSupport );
Document document = parser.parse(uri);
DOMWriter writer = new DOMWriter(canonical);