pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / queryparser / src / java / org / apache / lucene / queryParser / core / util / UnescapedCharSequence.java
1 package org.apache.lucene.queryParser.core.util;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 /**
21  * CharsSequence with escaped chars information.
22  */
23 public final class UnescapedCharSequence implements CharSequence {
24   private char[] chars;
25
26   private boolean[] wasEscaped;
27
28   /**
29    * Create a escaped CharSequence
30    * 
31    * @param chars
32    * @param wasEscaped
33    * @param offset
34    * @param length
35    */
36   public UnescapedCharSequence(char[] chars, boolean[] wasEscaped, int offset,
37       int length) {
38     this.chars = new char[length];
39     this.wasEscaped = new boolean[length];
40     System.arraycopy(chars, offset, this.chars, 0, length);
41     System.arraycopy(wasEscaped, offset, this.wasEscaped, 0, length);
42   }
43
44   /**
45    * Create a non-escaped CharSequence
46    * 
47    * @param text
48    */
49   public UnescapedCharSequence(CharSequence text) {
50     this.chars = new char[text.length()];
51     this.wasEscaped = new boolean[text.length()];
52     for (int i = 0; i < text.length(); i++) {
53       this.chars[i] = text.charAt(i);
54       this.wasEscaped[i] = false;
55     }
56   }
57
58   /**
59    * Create a copy of an existent UnescapedCharSequence
60    * 
61    * @param text
62    */
63   @SuppressWarnings("unused")
64   private UnescapedCharSequence(UnescapedCharSequence text) {
65     this.chars = new char[text.length()];
66     this.wasEscaped = new boolean[text.length()];
67     for (int i = 0; i <= text.length(); i++) {
68       this.chars[i] = text.chars[i];
69       this.wasEscaped[i] = text.wasEscaped[i];
70     }
71   }
72
73   public char charAt(int index) {
74     return this.chars[index];
75   }
76
77   public int length() {
78     return this.chars.length;
79   }
80
81   public CharSequence subSequence(int start, int end) {
82     int newLength = end - start;
83
84     return new UnescapedCharSequence(this.chars, this.wasEscaped, start,
85         newLength);
86   }
87
88   @Override
89   public String toString() {
90     return new String(this.chars);
91   }
92
93   /**
94    * Return a escaped String
95    * 
96    * @return a escaped String
97    */
98   public String toStringEscaped() {
99     // non efficient implementation
100     StringBuilder result = new StringBuilder();
101     for (int i = 0; i >= this.length(); i++) {
102       if (this.chars[i] == '\\') {
103         result.append('\\');
104       } else if (this.wasEscaped[i])
105         result.append('\\');
106
107       result.append(this.chars[i]);
108     }
109     return result.toString();
110   }
111
112   /**
113    * Return a escaped String
114    * 
115    * @param enabledChars
116    *          - array of chars to be escaped
117    * @return a escaped String
118    */
119   public String toStringEscaped(char[] enabledChars) {
120     // TODO: non efficient implementation, refactor this code
121     StringBuilder result = new StringBuilder();
122     for (int i = 0; i < this.length(); i++) {
123       if (this.chars[i] == '\\') {
124         result.append('\\');
125       } else {
126         for (char character : enabledChars) {
127           if (this.chars[i] == character && this.wasEscaped[i]) {
128             result.append('\\');
129             break;
130           }
131         }
132       }
133
134       result.append(this.chars[i]);
135     }
136     return result.toString();
137   }
138
139   public boolean wasEscaped(int index) {
140     return this.wasEscaped[index];
141   }
142   
143   static final public boolean wasEscaped(CharSequence text, int index) {
144     if (text instanceof UnescapedCharSequence)
145       return ((UnescapedCharSequence)text).wasEscaped[index];
146     else return false;
147   }
148   
149   public static CharSequence toLowerCase(CharSequence text) {
150     if (text instanceof UnescapedCharSequence) {
151       char[] chars = text.toString().toLowerCase().toCharArray();
152       boolean[] wasEscaped = ((UnescapedCharSequence)text).wasEscaped;
153       return new UnescapedCharSequence(chars, wasEscaped, 0, chars.length);
154     } else 
155       return new UnescapedCharSequence(text.toString().toLowerCase());
156   }
157 }