sandygao 2002/10/25 09:42:31
Modified: java/src/org/apache/xerces/impl/dv XSSimpleType.java
java/src/org/apache/xerces/impl/dv/xs XSSimpleTypeDecl.java
Log:
Performance: adding a new validate method that takes an Object as the string
value. This enables passing a StringBuffer to such method, and possibly avoid
some object creations. Without this method, we need to convert StringBuffer
to String before calling validate method, then create a StringBuffer in the
validate method for normalization, and finally convert the StringBuffer to a
String again after normalization:
buffer -> string -> buffer -> string
With this method, the original StringBuffer can be used to hold the normalized
value. This saves 2 object creations:
buffer -> string
Revision Changes Path
1.10 +15 -1 xml-xerces/java/src/org/apache/xerces/impl/dv/XSSimpleType.java
Index: XSSimpleType.java
===================================================================
RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/impl/dv/XSSimpleType.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- XSSimpleType.java 18 Jul 2002 20:48:43 -0000 1.9
+++ XSSimpleType.java 25 Oct 2002 16:42:30 -0000 1.10
@@ -152,6 +152,20 @@
throws InvalidDatatypeValueException;
/**
+ * validate a given string value, represented by content.toString().
+ * note that if content is a StringBuffer, for performance reasons,
+ * it's possible that the content of the string buffer is modified.
+ *
+ * @param content the string value that needs to be validated
+ * @param context the validation context
+ * @param validatedInfo used to store validation result
+ *
+ * @return the actual value (QName, Boolean) of the string value
+ */
+ public Object validate(Object content, ValidationContext context, ValidatedInfo
validatedInfo)
+ throws InvalidDatatypeValueException;
+
+ /**
* validate an actual value against this simple type.
*
* @param value the actual value that needs to be validated
1.19 +78 -4
xml-xerces/java/src/org/apache/xerces/impl/dv/xs/XSSimpleTypeDecl.java
Index: XSSimpleTypeDecl.java
===================================================================
RCS file:
/home/cvs/xml-xerces/java/src/org/apache/xerces/impl/dv/xs/XSSimpleTypeDecl.java,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -r1.18 -r1.19
--- XSSimpleTypeDecl.java 18 Sep 2002 15:14:18 -0000 1.18
+++ XSSimpleTypeDecl.java 25 Oct 2002 16:42:30 -0000 1.19
@@ -1235,6 +1235,26 @@
if (context == null)
context = fEmptyContext;
+
+ if (validatedInfo == null)
+ validatedInfo = new ValidatedInfo();
+
+ // first normalize string value, and convert it to actual value
+ Object ob = getActualValue(content, context, validatedInfo);
+
+ validate(context, validatedInfo);
+
+ return ob;
+
+ }
+
+ /**
+ * validate a value, and return the compiled form
+ */
+ public Object validate(Object content, ValidationContext context, ValidatedInfo
validatedInfo) throws InvalidDatatypeValueException {
+
+ if (context == null)
+ context = fEmptyContext;
if (validatedInfo == null)
validatedInfo = new ValidatedInfo();
@@ -1415,7 +1435,7 @@
}// checkExtraRules()
//we can still return object for internal use.
- private Object getActualValue(String content, ValidationContext context,
ValidatedInfo validatedInfo) throws InvalidDatatypeValueException{
+ private Object getActualValue(Object content, ValidationContext context,
ValidatedInfo validatedInfo) throws InvalidDatatypeValueException{
if (fVariety == VARIETY_ATOMIC) {
@@ -1423,7 +1443,7 @@
if (context==null ||context.needToNormalize()) {
nvalue = normalize(content, fWhiteSpace);
} else {
- nvalue = content;
+ nvalue = content.toString();
}
// update normalized value
@@ -1480,7 +1500,7 @@
if (context==null ||context.needToNormalize()) {
nvalue = normalize(content, fWhiteSpace);
} else {
- nvalue = content;
+ nvalue = content.toString();
}
StringTokenizer parsedList = new StringTokenizer(nvalue);
@@ -1611,6 +1631,60 @@
// if it's not a leading or tailing ws, then append a space
if (i < len - 1 && !isLeading)
sb.append((char)0x20);
+ }
+ }
+ }
+
+ return sb.toString();
+ }
+
+ // normalize the string according to the whiteSpace facet
+ protected static String normalize(Object content, short ws) {
+ if (content == null)
+ return null;
+
+ if (!(content instanceof StringBuffer)) {
+ String strContent = content.toString();
+ return normalize(strContent, ws);
+ }
+
+ StringBuffer sb = (StringBuffer)content;
+ int len = sb.length();
+ if (len == 0)
+ return "";
+ if (ws == WS_PRESERVE)
+ return sb.toString();
+
+ if (ws == WS_REPLACE) {
+ char ch;
+ // when it's replace, just replace #x9, #xa, #xd by #x20
+ for (int i = 0; i < len; i++) {
+ ch = sb.charAt(i);
+ if (ch == 0x9 || ch == 0xa || ch == 0xd)
+ sb.setCharAt(i, (char)0x20);
+ }
+ } else {
+ char ch;
+ int i, j = 0;
+ boolean isLeading = true;
+ // when it's collapse
+ for (i = 0; i < len; i++) {
+ ch = sb.charAt(i);
+ // append real characters, so we passed leading ws
+ if (ch != 0x9 && ch != 0xa && ch != 0xd && ch != 0x20) {
+ sb.setCharAt(j++, ch);
+ isLeading = false;
+ }
+ else {
+ // for whitespaces, we skip all following ws
+ for (; i < len-1; i++) {
+ ch = sb.charAt(i+1);
+ if (ch != 0x9 && ch != 0xa && ch != 0xd && ch != 0x20)
+ break;
+ }
+ // if it's not a leading or tailing ws, then append a space
+ if (i < len - 1 && !isLeading)
+ sb.setCharAt(j++, (char)0x20);
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]