File: VirtuosoExplicitString.java

package info (click to toggle)
virtuoso-opensource 6.1.6%2Bdfsg2-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 260,060 kB
  • ctags: 123,765
  • sloc: ansic: 652,532; sql: 458,419; xml: 282,834; java: 61,031; sh: 40,031; cpp: 36,890; cs: 25,240; php: 12,692; yacc: 9,523; lex: 7,018; makefile: 6,157; jsp: 4,484; awk: 1,643; perl: 1,013; ruby: 1,003; python: 326
file content (271 lines) | stat: -rw-r--r-- 7,054 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
 *  $Id$
 *
 *  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
 *  project.
 *
 *  Copyright (C) 1998-2012 OpenLink Software
 *
 *  This project is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; only version 2 of the License, dated June 1991.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */

package virtuoso.jdbc2;

import java.sql.*;
import java.util.*;
import java.io.*;
import java.math.*;
import openlink.util.*;

public class VirtuosoExplicitString
{
  private int dtp;
  private byte[] bytes;
  private String str;
  private VirtuosoConnection con;

  protected VirtuosoExplicitString (byte [] bytes, int dtp)
    {
      this.dtp = dtp;
      this.bytes = bytes;
      this.str = null;
    }

  protected VirtuosoExplicitString (String str, int dtp, VirtuosoConnection con) throws VirtuosoException
    {
      try
	{
	  this.dtp = dtp;
	  this.con = con;
	  if (dtp == VirtuosoTypes.DV_WIDE ||
	      dtp == VirtuosoTypes.DV_LONG_WIDE ||
	      dtp == VirtuosoTypes.DV_BLOB_WIDE)
	    {
	      // If it's an wide parameter
	      bytes = str.getBytes ("UTF8");
	      if (bytes.length < 256)
		this.dtp = VirtuosoTypes.DV_WIDE;
	      else
		this.dtp = VirtuosoTypes.DV_LONG_WIDE;
	      this.str = str;
	    }
	  else if (dtp == VirtuosoTypes.DV_STRING || dtp == VirtuosoTypes.DV_SHORT_STRING_SERIAL ||
	      dtp == VirtuosoTypes.DV_STRICT_STRING || dtp == VirtuosoTypes.DV_C_STRING ||
	      dtp == VirtuosoTypes.DV_BLOB || dtp == VirtuosoTypes.DV_ANY)
	    {
	      // If it's an narrow parameter
 	      if (con != null && con.charset_utf8)
	        bytes = str.getBytes ("UTF8");
	      else if (con != null && con.charset != null)
		  bytes = con.charsetBytes(str);
	      else
		cli_wide_to_narrow (str, con != null ? con.client_charset_hash : null);

	      if (bytes.length < 256)
		this.dtp = VirtuosoTypes.DV_SHORT_STRING_SERIAL;
	      else
		this.dtp = VirtuosoTypes.DV_STRING;
	    }
	  else if (dtp == VirtuosoTypes.DV_BLOB_BIN || dtp == VirtuosoTypes.DV_BIN)
	    {
	      bytes = str.getBytes ("8859_1");
	      if (bytes.length < 256)
		this.dtp = VirtuosoTypes.DV_SHORT_STRING_SERIAL;
	      else
		this.dtp = VirtuosoTypes.DV_STRING;
	    }
	  else
	    {
	      // all other parameters
	      if (!cli_wide_to_narrow (str, con != null ? con.client_charset_hash : null))
		{
		  bytes = str.getBytes ("UTF8");
		  if (bytes.length < 256)
		    this.dtp = VirtuosoTypes.DV_WIDE;
		  else
		    this.dtp = VirtuosoTypes.DV_LONG_WIDE;
		  this.str = str;
		}
	      else
		{
 	          if (con != null && con.charset_utf8)
	            bytes = str.getBytes ("UTF8");
	          else if (con != null && con.charset != null)
		    bytes = con.charsetBytes(str);

		  if (bytes.length < 256)
		    this.dtp = VirtuosoTypes.DV_SHORT_STRING_SERIAL;
		  else
		    this.dtp = VirtuosoTypes.DV_STRING;
		}
	    }
	  //System.err.println ("Str=[" + str + "] dtp=" + this.dtp);
	}
      catch (java.io.UnsupportedEncodingException e)
	{
	  //e.printStackTrace();
	  if (con != null && con.charset != null)
	    bytes = con.charsetBytes(str);
	  else
	    cli_wide_to_narrow (str, con != null ? con.client_charset_hash : null);
	  if (bytes.length < 256)
	    dtp = VirtuosoTypes.DV_SHORT_STRING_SERIAL;
	  else
	    dtp = VirtuosoTypes.DV_STRING;
	}
    }

  protected boolean cli_wide_to_narrow (String str, Hashtable charset_ht)
    {
      boolean ret = true;
      if (str == null)
        {
	  bytes = new byte[0];
	  return (true);
	}

      bytes = new byte[str.length()];
      if (charset_ht == null) 
	  {
        for (int i = 0; i < str.length(); i++)
	    bytes[i] = (byte)str.charAt(i);
	  }
      else 
       {
        for (int i = 0; i < str.length(); i++)
	  {
	    Character ch = new Character (str.charAt(i));
	    Byte b;
	    b = (Byte)(charset_ht != null ? charset_ht.get (ch) : new Byte ((byte) (ch.charValue())));
	    if (b == null)
	      {
	        bytes[i] = (byte) '?';
	        ret = false;
	      }
	    else
	      bytes[i] = (byte) b.intValue();
	  }
      }
      return ret;
    }


  protected boolean cli_wide_to_escaped (String str, Hashtable ht)
    {
      boolean ret = true;
      if (str == null)
        {
	  bytes = new byte[0];
	  dtp = VirtuosoTypes.DV_SHORT_STRING_SERIAL;
	  return (true);
	}

      //System.err.println ("cli_wide_to_escaped");
      StringBuffer strbuf = new StringBuffer ();
      for (int i = 0; i < str.length(); i++)
	{
	  char curr = str.charAt(i);
	  Byte b = null;

	  if (ht != null)
	    {
	      Character ch = new Character (curr);
	      b = (Byte)ht.get (ch);
	      if (b == null)
		{
		  strbuf.append ("\\x");
		  strbuf.append (Integer.toString (ch.charValue(), 16));
		}
	      else
		{
		  strbuf.append ((char) b.intValue());
		}
	    }
	  else
	    {
	      if (((int)curr) > 256)
		{
		  strbuf.append ("\\x");
		  strbuf.append (Integer.toString ((int) curr, 16));
		}
	      else
		strbuf.append (curr);
	    }
	}
      bytes = new byte[strbuf.length() + 1];
      for (int i = 0; i < strbuf.length(); i++)
	bytes[i] = (byte) strbuf.charAt (i);
      strbuf = null;
      if (bytes.length < 256)
	this.dtp = VirtuosoTypes.DV_SHORT_STRING_SERIAL;
      else
	this.dtp = VirtuosoTypes.DV_STRING;
      return true;
    }


  protected int getDtp() { return this.dtp; };

  protected void write (VirtuosoOutputStream os) throws IOException
    {
      os.write (dtp);
      if (bytes.length < 256)
	os.write (bytes.length);
      else
	os.writelongint (bytes.length);
	os.write (bytes, 0, bytes.length);
    }

  public String toString ()
    {
      if (this.str != null)
	return this.str;
      try
	{
	  if (dtp == VirtuosoTypes.DV_WIDE || dtp == VirtuosoTypes.DV_LONG_WIDE)
	    return new String (bytes, "UTF8");
	  else if (this.con != null && this.con.charset != null)
	    return this.con.uncharsetBytes (new String (bytes, "8859_1"));
	  else
	    return new String (bytes, "8859_1");
	}
      catch (Exception e)
	{
	  char [] chars = new char [bytes.length];
	  for (int i =0 ; i < bytes.length; i++)
	    chars[i] = (char)(bytes[i] & 0xff);
	  return new String (chars);
	}
    }

  public String toParamString ()
    {
      return toString().trim();
    }

  public boolean equals (Object obj)
    {
      if (obj != null && (obj instanceof VirtuosoExplicitString))
	{
	  VirtuosoExplicitString sobj = (VirtuosoExplicitString)obj;
	  if (sobj.dtp != this.dtp)
	    return false;
	  else
	    return sobj.bytes.equals (this.bytes);
	}
      else
	return false;
    }
}