KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > util > ISO9075


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.util;
18
19 import java.util.Collection JavaDoc;
20
21 import org.alfresco.service.namespace.NamespaceException;
22 import org.alfresco.service.namespace.NamespacePrefixResolver;
23 import org.alfresco.service.namespace.NamespaceService;
24 import org.alfresco.service.namespace.QName;
25
26 import com.sun.org.apache.xerces.internal.util.XMLChar;
27
28 /**
29  * Support for the ISO 9075 encoding of XML element names.
30  *
31  * @author Andy Hind
32  */

33 public class ISO9075
34 {
35     /*
36      * Mask for hex encoding
37      */

38     private static final int MASK = (1 << 4) - 1;
39
40     /*
41      * Digits used string encoding
42      */

43     private static final char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e',
44             'f' };
45
46     /**
47      * Private constructor
48      *
49      */

50     private ISO9075()
51     {
52         super();
53     }
54
55     /**
56      * Encode a string according to ISO 9075
57      *
58      * @param toEncode
59      * @return
60      */

61     public static String JavaDoc encode(String JavaDoc toEncode)
62     {
63         if ((toEncode == null) || (toEncode.length() == 0))
64         {
65             return toEncode;
66         }
67         else if (XMLChar.isValidName(toEncode) && (toEncode.indexOf("_x") == -1) && (toEncode.indexOf(':') == -1))
68         {
69             return toEncode;
70         }
71         else
72         {
73             StringBuilder JavaDoc builder = new StringBuilder JavaDoc(toEncode.length());
74             for (int i = 0; i < toEncode.length(); i++)
75             {
76                 char c = toEncode.charAt(i);
77                 // First requires special test
78
if (i == 0)
79                 {
80                     if (XMLChar.isNCNameStart(c))
81                     {
82                         // The first character may be the _ at the start of an
83
// encoding pattern
84
if (matchesEncodedPattern(toEncode, i))
85                         {
86                             // Encode the first _
87
encode('_', builder);
88                         }
89                         else
90                         {
91                             // Just append
92
builder.append(c);
93                         }
94                     }
95                     else
96                     {
97                         // Encode an invalid start character for an XML element
98
// name.
99
encode(c, builder);
100                     }
101                 }
102                 else if (!XMLChar.isNCName(c))
103                 {
104                     encode(c, builder);
105                 }
106                 else
107                 {
108                     if (matchesEncodedPattern(toEncode, i))
109                     {
110                         // '_' must be encoded
111
encode('_', builder);
112                     }
113                     else
114                     {
115                         builder.append(c);
116                     }
117                 }
118             }
119             return builder.toString();
120         }
121
122     }
123
124     private static boolean matchesEncodedPattern(String JavaDoc string, int position)
125     {
126         return (string.length() >= position + 6)
127                 && (string.charAt(position) == '_') && (string.charAt(position + 1) == 'x')
128                 && isHexChar(string.charAt(position + 2)) && isHexChar(string.charAt(position + 3))
129                 && isHexChar(string.charAt(position + 4)) && isHexChar(string.charAt(position + 5))
130                 && (string.charAt(position + 6) == '_');
131     }
132
133     private static boolean isHexChar(char c)
134     {
135         switch (c)
136         {
137         case '0':
138         case '1':
139         case '2':
140         case '3':
141         case '4':
142         case '5':
143         case '6':
144         case '7':
145         case '8':
146         case '9':
147         case 'a':
148         case 'b':
149         case 'c':
150         case 'd':
151         case 'e':
152         case 'f':
153         case 'A':
154         case 'B':
155         case 'C':
156         case 'D':
157         case 'E':
158         case 'F':
159             return true;
160         default:
161             return false;
162         }
163     }
164
165     public static String JavaDoc decode(String JavaDoc toDecode)
166     {
167         if ((toDecode == null) || (toDecode.length() < 7) || (toDecode.indexOf("_x") < 0))
168         {
169             return toDecode;
170         }
171         StringBuffer JavaDoc decoded = new StringBuffer JavaDoc();
172         for (int i = 0, l = toDecode.length(); i < l; i++)
173         {
174             if (matchesEncodedPattern(toDecode, i))
175             {
176                 decoded.append(((char) Integer.parseInt(toDecode.substring(i + 2, i + 6), 16)));
177                 i += 6;
178             }
179             else
180             {
181                 decoded.append(toDecode.charAt(i));
182             }
183         }
184         return decoded.toString();
185     }
186
187     private static void encode(char c, StringBuilder JavaDoc builder)
188     {
189         char[] buf = new char[] { '_', 'x', '0', '0', '0', '0', '_' };
190         int charPos = 6;
191         do
192         {
193             buf[--charPos] = DIGITS[c & MASK];
194             c >>>= 4;
195         }
196         while (c != 0);
197         builder.append(buf);
198     }
199
200     public static String JavaDoc getXPathName(QName qName, NamespacePrefixResolver nspr)
201     {
202
203         Collection JavaDoc<String JavaDoc> prefixes = nspr.getPrefixes(qName.getNamespaceURI());
204         if (prefixes.size() == 0)
205         {
206             throw new NamespaceException("A namespace prefix is not registered for uri " + qName.getNamespaceURI());
207         }
208         String JavaDoc prefix = prefixes.iterator().next();
209         if (prefix.equals(NamespaceService.DEFAULT_PREFIX))
210         {
211             return ISO9075.encode(qName.getLocalName());
212         }
213         else
214         {
215             return prefix + ":" + ISO9075.encode(qName.getLocalName());
216         }
217
218     }
219
220     public static String JavaDoc getXPathName(QName qName)
221     {
222
223         return "{" + qName.getNamespaceURI() + "}" + ISO9075.encode(qName.getLocalName());
224
225     }
226 }
227
Popular Tags