1 /*
2 WSMO Studio - a Semantic Web Service Editor
3 Copyright (c) 2004-2006, OntoText Lab. / SIRMA Group
4
5 This library is free software; you can redistribute it and/or modify it under
6 the terms of the GNU Lesser General Public License as published by the Free
7 Software Foundation; either version 2.1 of the License, or (at your option)
8 any later version.
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12 details.
13 You should have received a copy of the GNU Lesser General Public License along
14 with this library; if not, write to the Free Software Foundation, Inc.,
15 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18 /***
19 * <p>Title: WSMO Studio</p>
20 * <p>Description: Semantic Web Service Editor</p>
21 * <p>Copyright: Copyright (c) 2004-2006</p>
22 * <p>Company: OntoText Lab. / SIRMA </p>
23 */
24
25 package org.wsmostudio.runtime;
26
27 import org.wsmo.common.IRI;
28 import org.wsmo.common.NFP;
29
30 public class IRIUtils {
31
32 public static IRI titleNFP = WSMORuntime.getRuntime().getWsmoFactory().createIRI(NFP.DC_TITLE);
33 public static IRI descrNFP = WSMORuntime.getRuntime().getWsmoFactory().createIRI(NFP.DC_DESCRIPTION);
34
35 /***
36 *
37 * @param iri
38 * @return The position of the first invalid character if any or -1
39 */
40 public static int validateIRI(String iri) {
41 for(int i = 0; i < iri.length(); i++) {
42 if (false == isValidChar(iri.charAt(i))) {
43 return i;
44 }
45 }
46 return -1;
47 }
48
49 public static boolean isValidChar(char ch) {
50 if (ch < 33 || ch >= 55296) {
51 return false;
52 }
53 switch (ch) {
54 case 34: // "
55 case 37: // %
56 case 60: // <
57 case 62: // >
58 case 91: // [
59 case 92: // \
60 case 93: // ]
61 case 94: // ^
62 case 96: // `
63 case 123: // {
64 case 124: // |
65 case 125: // }
66 return false;
67 }
68 if (ch >= 127 && ch < 160) {
69 return false;
70 }
71 return true;
72 }
73
74 public static String createFilenameFromIRI(String ns, String iri) {
75
76 if (iri.indexOf(":") == -1) {
77 iri = ns + iri;
78 }
79 StringBuffer result = new StringBuffer(iri.length());
80 if (Character.isJavaIdentifierStart(iri.charAt(0))) {
81 result.append(iri.charAt(0));
82 }
83 else {
84 result.append('_');
85 }
86
87 char lastChar = 0;
88 for(int i = 1; i < iri.length(); i++) {
89 if (isValidFilenamePart(iri.charAt(i))) {
90 lastChar = iri.charAt(i);
91 result.append(lastChar);
92 }
93 else {
94 if (lastChar != '_') {
95 result.append('_');
96 lastChar = '_';
97 }
98 }
99 }
100 return result.append(".wsml").toString();
101 }
102
103 public static boolean isValidFilenamePart(char ch) {
104 if (Character.isJavaIdentifierPart(ch)) {
105 return ch < 256;
106 }
107 else {
108 return ch == '.'
109 || ch == '@'
110 || ch == '&'
111 || ch == '='
112 || ch == '+'
113 || ch == '!'
114 || ch == '%'
115 || ch == '^'
116 || ch == '~'
117 || ch == '#'
118 || ch == '$';
119 }
120 }
121
122 }
123
124 /*
125 * $Log$
126 * Revision 1.3 2007/02/13 15:50:12 alex_simov
127 * fixes
128 *
129 * Revision 1.2 2006/04/13 13:24:43 alex_simov
130 * related to RFE[1418614]: converter method from IRI to filename added
131 *
132 * Revision 1.1 2006/04/11 12:23:08 alex_simov
133 * bugfix[1466316]: Utility class offering checks for SOME invalid
134 * symbols for IRIs
135 *
136 */