Rewrite XML serialization avoiding string concats
[cql-java-moved-to-github.git] / src / main / java / org / z3950 / zing / cql / CQLTermNode.java
index f9b17ac..dfd0e63 100644 (file)
@@ -1,9 +1,9 @@
 // $Id: CQLTermNode.java,v 1.28 2007-07-03 13:41:24 mike Exp $
 
 package org.z3950.zing.cql;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Properties;
-import java.util.Vector;
-
 
 /**
  * Represents a terminal node in a CQL parse-tree.
@@ -43,6 +43,7 @@ public class CQLTermNode extends CQLNode {
                qual.equals("cql.resultSetName"));
     }
 
+    @Override
     public String getResultSetName() {
        if (isResultSetIndex(index))
            return term;
@@ -50,17 +51,19 @@ public class CQLTermNode extends CQLNode {
            return null;
     }
 
-    public String toXCQL(int level, Vector<CQLPrefix> prefixes,
-                        Vector<ModifierSet> sortkeys) {
-       return (indent(level) + "<searchClause>\n" +
-               renderPrefixes(level+1, prefixes) +
-               indent(level+1) + "<index>" + xq(index) + "</index>\n" +
-               relation.toXCQL(level+1) +
-               indent(level+1) + "<term>" + xq(term) + "</term>\n" +
-               renderSortKeys(level+1, sortkeys) +
-               indent(level) + "</searchClause>\n");
+    @Override
+    void toXCQLInternal(XCQLBuilder b, int level, List<CQLPrefix> prefixes,
+                        List<ModifierSet> sortkeys) {
+      b.indent(level).append("<searchClause>\n");
+      renderPrefixes(b, level + 1, prefixes);
+      b.indent(level + 1).append("<index>").xq(index).append("</index>\n");
+      relation.toXCQLInternal(b, level + 1);
+      b.indent(level + 1).append("<term>").xq(term).append("</term>\n");
+      renderSortKeys(b, level + 1, sortkeys);
+      b.indent(level).append("</searchClause>\n");
     }
 
+    @Override
     public String toCQL() {
        String quotedIndex = maybeQuote(index);
        String quotedTerm = maybeQuote(term);
@@ -79,8 +82,8 @@ public class CQLTermNode extends CQLNode {
     // ### Interaction between this and its callers is not good as
     // regards truncation of the term and generation of truncation
     // attributes.  Change the interface to fix this.
-    private Vector getAttrs(Properties config) throws PQFTranslationException {
-       Vector<String> attrs = new Vector<String>();
+    private List<String> getAttrs(Properties config) throws PQFTranslationException {
+       List<String> attrs = new ArrayList<String>();
 
        // Do this first so that if any other truncation or
        // completeness attributes are generated, they "overwrite"
@@ -96,6 +99,8 @@ public class CQLTermNode extends CQLNode {
 
        attr = config.getProperty("index." + index);
        if (attr == null)
+           attr = config.getProperty("qualifier." + index);
+       if (attr == null)
            throw new UnknownIndexException(index);
        attrs.add(attr);
 
@@ -115,7 +120,7 @@ public class CQLTermNode extends CQLNode {
            throw new UnknownRelationException(rel);
        attrs.add(attr);
 
-       Vector<Modifier> mods = relation.getModifiers();
+       List<Modifier> mods = relation.getModifiers();
        for (int i = 0; i < mods.size(); i++) {
            String type = mods.get(i).type;
            attr = config.getProperty("relationModifier." + type);
@@ -154,6 +159,7 @@ public class CQLTermNode extends CQLNode {
        return attrs;
     }
 
+    @Override
     public String toPQF(Properties config) throws PQFTranslationException {
        if (isResultSetIndex(index)) {
            // Special case: ignore relation, modifiers, wildcards, etc.
@@ -161,12 +167,12 @@ public class CQLTermNode extends CQLNode {
            return "@set " + maybeQuote(term);
        }
 
-       Vector attrs = getAttrs(config);
+       List<String> attrs = getAttrs(config);
 
        String attr, s = "";
        for (int i = 0; i < attrs.size(); i++) {
            attr = (String) attrs.get(i);
-           s += "@attr " + Utils.replaceString(attr, " ", " @attr ") + " ";
+           s += "@attr " + attr.replace(" ", " @attr ") + " ";
        }
 
        String text = term;
@@ -194,12 +200,13 @@ public class CQLTermNode extends CQLNode {
            str.indexOf('/') != -1 ||
            str.indexOf('(') != -1 ||
            str.indexOf(')') != -1) {
-           str = '"' + Utils.replaceString(str, "\"", "\\\"") + '"';
+           str = '"' + str.replace("\"", "\\\"") + '"';
        }
 
        return str;
     }
 
+    @Override
     public byte[] toType1BER(Properties config) throws PQFTranslationException {
        if (isResultSetIndex(index)) {
            // Special case: ignore relation, modifiers, wildcards, etc.
@@ -227,7 +234,7 @@ public class CQLTermNode extends CQLNode {
        if (len > 0 && text.substring(len-1, len).equals("^"))
            text = text.substring(0, len-1);
 
-       String attr, attrList, term = text;
+       String attr, attrList;
        byte[] operand = new byte[text.length()+100];
        int i, j, offset, type, value;
        offset = putTag(CONTEXT, 0, CONSTRUCTED, operand, 0); // op
@@ -237,7 +244,7 @@ public class CQLTermNode extends CQLNode {
        offset = putTag(CONTEXT, 44, CONSTRUCTED, operand, offset); // AttributeList
        operand[offset++] = (byte)(0x80&0xff); // indefinite length
 
-       Vector attrs = getAttrs(config);
+       List<String> attrs = getAttrs(config);
        for(i = 0; i < attrs.size(); i++) {
            attrList = (String) attrs.get(i);
            java.util.StringTokenizer st =
@@ -264,7 +271,7 @@ public class CQLTermNode extends CQLNode {
        operand[offset++] = 0x00;
 
        offset = putTag(CONTEXT, 45, PRIMITIVE, operand, offset); // general Term
-       byte[] t = term.getBytes();
+       byte[] t = text.getBytes();
        offset = putLen(t.length, operand, offset);
        System.arraycopy(t, 0, operand, offset, t.length);
        offset += t.length;