001: /*
002: * (c) Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: * 1. Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * 3. The name of the author may not be used to endorse or promote products
014: * derived from this software without specific prior written permission.
015:
016: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
017: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
018: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
019: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
020: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
021: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
022: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
023: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
024: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
025: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
026: *
027: * Util.java
028: *
029: * Created on 01 August 2000, 16:31
030: */
031:
032: package com.hp.hpl.jena.rdf.model.impl;
033:
034: import java.util.regex.*;
035:
036: import org.apache.xerces.util.XMLChar;
037:
038: import com.hp.hpl.jena.shared.*;
039:
040: /** Some utility functions.
041: *
042: * @author bwm
043: * @version Release='$Name: $' Revision='$Revision: 1.14 $' Date='$Date: 2008/01/02 12:05:07 $'
044: */
045: public class Util extends Object {
046:
047: public static final String CLASSPATH = "com.hp.hpl.jena";
048:
049: /** Given an absolute URI, determine the split point between the namespace part
050: * and the localname part.
051: * If there is no valid localname part then the length of the
052: * string is returned.
053: * The algorithm tries to find the longest NCName at the end
054: * of the uri, not immediately preceeded by the first colon
055: * in the string.
056: * @param uri
057: * @return the index of the first character of the localname
058: */
059: public static int splitNamespace(String uri) {
060: char ch;
061: int lg = uri.length();
062: if (lg == 0)
063: return 0;
064: int j;
065: int i;
066: for (i = lg - 1; i >= 1; i--) {
067: ch = uri.charAt(i);
068: if (notNameChar(ch))
069: break;
070: }
071: for (j = i + 1; j < lg; j++) {
072: ch = uri.charAt(j);
073: if (XMLChar.isNCNameStart(ch)) {
074: if (uri.charAt(j - 1) == ':'
075: && uri.lastIndexOf(':', j - 2) == -1)
076: continue; // split "mailto:me" as "mailto:m" and "e" !
077: else
078: break;
079: }
080: }
081: return j;
082: }
083:
084: /**
085: answer true iff this is not a legal NCName character, ie, is
086: a possible split-point start.
087: */
088: public static boolean notNameChar(char ch) {
089: return !XMLChar.isNCName(ch);
090: }
091:
092: protected static Pattern standardEntities = Pattern
093: .compile("&|<|>|\t|\n|\r|\'|\"");
094:
095: public static String substituteStandardEntities(String s) {
096: if (standardEntities.matcher(s).find()) {
097: return substituteEntitiesInElementContent(s).replaceAll(
098: "'", "'").replaceAll("\t", "	").replaceAll(
099: "\n", "
").replaceAll("\r", "
")
100: .replaceAll("\"", """);
101: } else
102: return s;
103: }
104:
105: protected static Pattern entityValueEntities = Pattern
106: .compile("&|%|\'|\"");
107:
108: public static String substituteEntitiesInEntityValue(String s) {
109: if (entityValueEntities.matcher(s).find()) {
110: return s.replaceAll("&", "&").replaceAll("'", "'")
111: .replaceAll("%", "%")
112: .replaceAll("\"", """);
113: } else
114: return s;
115: }
116:
117: protected static Pattern elementContentEntities = Pattern
118: .compile("<|>|&|[\0-\37&&[^\n\r\t]]|\uFFFF|\uFFFE");
119:
120: /**
121: Answer <code>s</code> modified to replace <, >, and & by
122: their corresponding entity references.
123:
124: <p>
125: Implementation note: as a (possibly misguided) performance hack,
126: the obvious cascade of replaceAll calls is replaced by an explicit
127: loop that looks for all three special characters at once.
128: */
129: public static String substituteEntitiesInElementContent(String s) {
130: Matcher m = elementContentEntities.matcher(s);
131: if (!m.find())
132: return s;
133: else {
134: int start = 0;
135: StringBuffer result = new StringBuffer();
136: do {
137: result.append(s.substring(start, m.start()));
138: char ch = s.charAt(m.start());
139: switch (ch) {
140: case '<':
141: result.append("<");
142: break;
143: case '&':
144: result.append("&");
145: break;
146: case '>':
147: result.append(">");
148: break;
149: default:
150: throw new CannotEncodeCharacterException(ch, "XML");
151: }
152: start = m.end();
153: } while (m.find(start));
154: result.append(s.substring(start));
155: return result.toString();
156: }
157: }
158:
159: public static String replace(String s, String oldString,
160: String newString) {
161: String result = "";
162: int length = oldString.length();
163: int pos = s.indexOf(oldString);
164: int lastPos = 0;
165: while (pos >= 0) {
166: result = result + s.substring(lastPos, pos) + newString;
167: lastPos = pos + length;
168: pos = s.indexOf(oldString, lastPos);
169: }
170: return result + s.substring(lastPos, s.length());
171: }
172:
173: /** Call System.getProperty and suppresses SecurityException, (simply returns null).
174: *@return The property value, or null if none or there is a SecurityException.
175: */
176: public static String XgetProperty(String p) {
177: return XgetProperty(p, null);
178: }
179:
180: /** Call System.getProperty and suppresses SecurityException, (simply returns null).
181: *@return The property value, or null if none or there is a SecurityException.
182: */
183: public static String XgetProperty(String p, String def) {
184: try {
185: return System.getProperty(p, def);
186: } catch (SecurityException e) {
187: return def;
188: }
189: }
190:
191: }
|