Mercurial > hg > mpdl-group
comparison software/eXist/mpdl-modules/src/org/exist/xquery/modules/mpdltext/EncodeBig5.java @ 0:408254cf2f1d
Erstellung
author | Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de> |
---|---|
date | Wed, 24 Nov 2010 17:24:23 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:408254cf2f1d |
---|---|
1 /* | |
2 * eXist Open Source Native XML Database: Extension module | |
3 * Copyright (C) 2008 Josef Willenborg | |
4 * jwillenborg@mpiwg-berlin.mpg.de | |
5 * http://www.mpiwg-berlin.mpg.de | |
6 * | |
7 * This program is free software; you can redistribute it and/or | |
8 * modify it under the terms of the GNU Lesser General Public License | |
9 * as published by the Free Software Foundation; either version 2 | |
10 * of the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 * GNU Lesser General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Lesser General Public License | |
18 * along with this program; if not, write to the Free Software | |
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
20 * | |
21 * $Id: TextModule.java $ | |
22 */ | |
23 package org.exist.xquery.modules.mpdltext; | |
24 | |
25 import java.io.UnsupportedEncodingException; | |
26 | |
27 import org.exist.dom.QName; | |
28 import org.exist.xquery.BasicFunction; | |
29 import org.exist.xquery.Cardinality; | |
30 import org.exist.xquery.FunctionSignature; | |
31 import org.exist.xquery.XPathException; | |
32 import org.exist.xquery.XQueryContext; | |
33 import org.exist.xquery.value.Sequence; | |
34 import org.exist.xquery.value.SequenceType; | |
35 import org.exist.xquery.value.StringValue; | |
36 import org.exist.xquery.value.Type; | |
37 import org.exist.xquery.value.ValueSequence; | |
38 | |
39 public class EncodeBig5 extends BasicFunction { | |
40 | |
41 public final static FunctionSignature signature = | |
42 new FunctionSignature( | |
43 new QName("encode-big5", MPDLTextModule.NAMESPACE_URI, MPDLTextModule.PREFIX), | |
44 "A function which delivers an encoded translation of the big5 input string", | |
45 new SequenceType[] { new SequenceType(Type.STRING, Cardinality.ZERO_OR_MORE) }, | |
46 new SequenceType(Type.STRING, Cardinality.ZERO_OR_MORE)); | |
47 | |
48 public EncodeBig5(XQueryContext context) { | |
49 super(context, signature); | |
50 } | |
51 | |
52 public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException { | |
53 Sequence seqBig5InputStr = args[0]; | |
54 String big5InputStr = ""; | |
55 String charset = "big5"; | |
56 if (seqBig5InputStr.isEmpty()) | |
57 return Sequence.EMPTY_SEQUENCE; | |
58 else | |
59 big5InputStr = seqBig5InputStr.getStringValue(); | |
60 ValueSequence result = null; | |
61 String resultStr = ""; | |
62 try { | |
63 byte[] resultBytes = big5InputStr.getBytes(charset); | |
64 for (int i=0; i < resultBytes.length; i++) { | |
65 byte b = resultBytes[i]; | |
66 int unsigned = unsignedByteToInt(b); | |
67 String hexStr = Integer.toHexString(unsigned); | |
68 resultStr = resultStr + "%" + hexStr; | |
69 } | |
70 result = new ValueSequence(); | |
71 result.add(new StringValue(resultStr)); | |
72 } catch (UnsupportedEncodingException e) { | |
73 | |
74 } | |
75 return result; | |
76 } | |
77 | |
78 private int unsignedByteToInt(byte b) { | |
79 return (int) b & 0xFF; | |
80 } | |
81 } |