001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.quercus.env;
031:
032: import com.caucho.vfs.WriteStream;
033:
034: import java.util.IdentityHashMap;
035: import java.io.IOException;
036:
037: /**
038: * Represents a 16-bit unicode string value.
039: */
040: abstract public class UnicodeValue extends StringValue {
041: protected UnicodeValue() {
042: }
043:
044: @Override
045: public String toDebugString() {
046: StringBuilder sb = new StringBuilder();
047:
048: int length = length();
049:
050: sb.append("unicode(");
051: sb.append(length);
052: sb.append(") \"");
053:
054: int appendLength = length > 256 ? 256 : length;
055:
056: for (int i = 0; i < appendLength; i++)
057: sb.append(charAt(i));
058:
059: if (length > 256)
060: sb.append(" ...");
061:
062: sb.append('"');
063:
064: return sb.toString();
065: }
066:
067: @Override
068: public void varDumpImpl(Env env, WriteStream out, int depth,
069: IdentityHashMap<Value, String> valueSet) throws IOException {
070: int length = length();
071:
072: if (length < 0)
073: length = 0;
074:
075: out.print("unicode(");
076: out.print(length);
077: out.print(") \"");
078:
079: for (int i = 0; i < length; i++)
080: out.print(charAt(i));
081:
082: out.print("\"");
083: }
084:
085: /**
086: * Convert to a unicode value.
087: */
088: @Override
089: public StringValue toUnicodeValue() {
090: return this ;
091: }
092:
093: /**
094: * Convert to a unicode value.
095: */
096: @Override
097: public StringValue toUnicodeValue(Env env) {
098: return this ;
099: }
100:
101: /**
102: * Decodes from charset and returns UnicodeValue.
103: *
104: * @param env
105: * @param charset
106: */
107: @Override
108: public StringValue toUnicodeValue(Env env, String charset) {
109: return this ;
110: }
111:
112: /**
113: * Converts to a string builder
114: */
115: @Override
116: public StringValue toStringBuilder() {
117: UnicodeBuilderValue sb = new UnicodeBuilderValue();
118:
119: sb.append(this );
120:
121: return sb;
122: }
123:
124: /**
125: * Returns true for UnicodeValue
126: */
127: @Override
128: public boolean isUnicode() {
129: return true;
130: }
131:
132: /**
133: * Returns true for equality
134: */
135: /*
136: @Override
137: public boolean eq(Value rValue)
138: {
139: rValue = rValue.toValue();
140:
141: if (rValue.isBoolean()) {
142: return toBoolean() == rValue.toBoolean();
143: }
144:
145: int type = getNumericType();
146:
147: if (type == IS_STRING) {
148: if (rValue.isUnicode())
149: return equals(rValue);
150: else if (rValue.isBinary())
151: return equals(rValue.toUnicodeValue(Env.getInstance()));
152: else if (rValue.isLongConvertible())
153: return toLong() == rValue.toLong();
154: else
155: return equals(rValue.toStringValue());
156: }
157: else if (rValue.isString() && rValue.length() == 0)
158: return length() == 0;
159: else if (rValue.isNumberConvertible())
160: return toDouble() == rValue.toDouble();
161: else
162: return equals(rValue.toStringValue());
163: }
164: */
165: }
|