perfsonar-dev - nmwg: r238 - trunk/nmwg/java/org/ggf/ns/nmwg/base/v2_0
Subject: perfsonar development work
List archive
- From:
- To: ,
- Subject: nmwg: r238 - trunk/nmwg/java/org/ggf/ns/nmwg/base/v2_0
- Date: Tue, 3 Jul 2007 08:25:22 -0400
Author: mac
Date: 2007-07-03 08:25:22 -0400 (Tue, 03 Jul 2007)
New Revision: 238
Modified:
trunk/nmwg/java/org/ggf/ns/nmwg/base/v2_0/Handler.java
Log:
Change of error in text values parsing.
Before text values of elements was set each time when SAX characters(...)
method was invoked. But SAX characters(...) method may be run several times
for the same value of XML element. So before the change, text value of the
element was overwritten each time. The effect was cutting the beginning text
values of XML elements. It was quite rare and strange behaviour.
Now the method characters(...) only adds Strings to buffer and the whole
value is trimmed and set in endElement(...) method.
The error was probably related to problems reported by Roman (eXist DB - HTTP
access in RRD MA) and Nina (cutting text when service overloaded)
Modified: trunk/nmwg/java/org/ggf/ns/nmwg/base/v2_0/Handler.java
===================================================================
--- trunk/nmwg/java/org/ggf/ns/nmwg/base/v2_0/Handler.java 2007-06-19
15:24:05 UTC (rev 237)
+++ trunk/nmwg/java/org/ggf/ns/nmwg/base/v2_0/Handler.java 2007-07-03
12:25:22 UTC (rev 238)
@@ -7,50 +7,42 @@
*/
package org.ggf.ns.nmwg.base.v2_0;
-import java.io.FileOutputStream;
+import java.util.Hashtable;
+import java.util.Stack;
-import org.apache.xml.serialize.OutputFormat;
-import org.apache.xml.serialize.XMLSerializer;
-
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
-import org.xml.sax.ContentHandler;
-
-import org.xml.sax.helpers.XMLReaderFactory;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.NamespaceSupport;
+import org.xml.sax.helpers.XMLReaderFactory;
-import java.util.Hashtable;
-import java.util.Stack;
+public class Handler extends DefaultHandler {
-import org.w3c.dom.Document;
-
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-
-import javax.xml.transform.Result;
-import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerFactory;
-
-import javax.xml.transform.stream.StreamResult;
-
-import javax.xml.transform.dom.DOMResult;
-import javax.xml.transform.dom.DOMSource;
-
-public class Handler
- extends DefaultHandler {
-
+
+ // --------------------------------------------------------------
constants
+
+
/* feature ids */
- public static final String NAMESPACES_FEATURE_ID =
"http://xml.org/sax/features/namespaces";
- public static final String NAMESPACE_PREFIXES_FEATURE_ID =
"http://xml.org/sax/features/namespace-prefixes";
+ public static final String NAMESPACES_FEATURE_ID
+ = "http://xml.org/sax/features/namespaces";
+
+ public static final String NAMESPACE_PREFIXES_FEATURE_ID
+ = "http://xml.org/sax/features/namespace-prefixes";
/* settings */
- public static final String DEFAULT_PARSER_NAME =
"org.apache.xerces.parsers.SAXParser";
+ public static final String DEFAULT_PARSER_NAME
+ = "org.apache.xerces.parsers.SAXParser";
+
public static final boolean DEFAULT_NAMESPACES = true;
+
public static final boolean DEFAULT_NAMESPACE_PREFIXES = true;
+
+ // ------------------------------------------------------instance
variables
+
+
/* internal data */
protected long elementCount;
protected long attributeCount;
@@ -64,13 +56,23 @@
protected Stack st;
protected static String configFile = "";
- NamespaceSupport nss;
+ protected NamespaceSupport nss;
+ protected String textValue = "";
+ protected boolean isTextValue = false;
+
+
+ // ------------------------------------------------------------
Constructor
+
+
/**
* Constructor.
*/
public Handler() {}
+
+ // --------------------------------------------------------- public
methods
+
/**
* Preliminaries to deal with when we load an xml document.
*/
@@ -92,15 +94,18 @@
st.push(ne);
ht = new Hashtable(hashElementCount);
+
}
+
/**
* bootstrap the config file
*/
- public static void setConfigFile(String configFile) {
- Handler.configFile = configFile;
- }
+ public static void setConfigFile(String configFile) {
+ Handler.configFile = configFile;
+ }
+
/**
* Do we need this, could deal with namespaces...
*/
@@ -109,34 +114,46 @@
return;
}
+
/**
* Used in conjunction with the characters function
*/
public void ignorableWhitespace(char[] ch, int offset, int length)
throws SAXException {
return;
- }
+ }
+
/**
* Read a text element, we would ideally like to skip
* whitespace.
*/
public void characters(char[] ch, int offset, int length)
throws SAXException {
- String text = new String(ch, offset, length);
- String clean = text.trim();
-
- if(!(clean.equals(""))) {
- ((Element)st.peek()).addText(clean);
- }
+
+ //method characters(..) may be invoked several times for single text
value of
+ //the element, so we need a trick :)
+
+ //We add characters to textValue and set isTextValue = true.
+ //The full text value of the element
+ //will be added in endElement method
+
+ String text = new String(ch, offset, length);
+ textValue += text;
+ isTextValue = true;
+
}
+
/**
* At the start of an element we want to create it (using a helper
class), and
* do some hash magic.
*/
- public void startElement(String uri, String local, String qname,
Attributes attrs) throws SAXException {
- Element newElement = null;
+ public void startElement(
+ String uri, String local, String qname, Attributes attrs)
+ throws SAXException {
+
+ Element newElement = null;
elementCount++;
nss.pushContext();
@@ -145,9 +162,7 @@
if(newElement != null) {
-
-//System.out.println("[start] class is " + newElement.getClass());
-
+ //System.out.println("[start] class is " +
newElement.getClass());
// push last seen element on stack
st.push(newElement);
@@ -157,36 +172,48 @@
// insert newly created object in the hashtable ht
//Integer hc = new Integer(newElement.hashCode());
//ht.put(hc, newElement);
-//XXX
-//ref by id?
+ //XXX
+ // ref by id?
ht.put(newElement.getId(), newElement);
}
-
+
+ //beginning of element, so clear textValue
+ isTextValue = false;
+ textValue = "";
}
+
/**
* Pop from the stack and do validation?
*/
public void endElement(String uri, String localName, String qName)
throws SAXException {
-
-//System.out.println("Pop the pile...");
- Element top = (Element)st.pop();
- nss.popContext();
-
-
-//System.out.println("Element: " + top.getClass() + "\n");
+ //add text value (element is finished so we can do it now)
+ //and then reset buffer (textValue)
+ if (isTextValue == true) {
+ String clean = textValue.trim();
+ ((Element)st.peek()).addText( clean );
+ }
+ isTextValue = false;
+ textValue = "";
+
+ //System.out.println("Pop the pile...");
+ Element top = (Element)st.pop();
+ nss.popContext();
+
+ //System.out.println("Element: " + top.getClass() + "\n");
+
/**
* A Hack for now, we only want to output xml when we pop off
the
* root element.
*/
-/*comment out this section*/
-/*
+ /*comment out this section*/
+ /*
if(top.getLocalName().equals("message") ||
top.getLocalName().equals("store")) {
FileOutputStream fos;
OutputFormat of;
@@ -214,17 +241,18 @@
System.err.println("Error: xml writing error
--> " + e.toString());
}
}
-*/
+ */
/* **************************************
*
* testing a dom format...
*
* **************************************/
-/*
+
+ /*
if(top.getLocalName().equals("message") ||
top.getLocalName().equals("store")) {
try {
-System.out.println("DOM TEST:");
+ System.out.println("DOM TEST:");
DocumentBuilder builder =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.newDocument();
@@ -233,20 +261,19 @@
Result output = new StreamResult(System.out);
doc = top.getDOM(doc);
DOMSource source = new DOMSource(doc);
-xformer.transform(source,output);
+ xformer.transform(source,output);
-System.out.println("END DOM TEST:");
+ System.out.println("END DOM TEST:");
}
catch(Exception e) {
System.err.println("XML DOM Error");
}
}
-*/
-
+ */
+ }
- }
-
+
/**
* Print out the hash, we should do something though...
*/
@@ -310,6 +337,7 @@
return;
}
+
/**
* running from the command line? not with ant we arent...
*/
@@ -318,4 +346,6 @@
System.err.println("usage: java
org.ggf.ns.nmwg.base.v2_0.Handler [input_file] [config_file]");
System.err.println();
}
-}
+
+
+} //Handler
- nmwg: r238 - trunk/nmwg/java/org/ggf/ns/nmwg/base/v2_0, svnlog, 07/03/2007
Archive powered by MHonArc 2.6.16.