KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > rdf > model > impl > Util


1 /*
2  * (c) Copyright 2000, 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * Util.java
28  *
29  * Created on 01 August 2000, 16:31
30  */

31
32 package com.hp.hpl.jena.rdf.model.impl;
33 import org.apache.xerces.util.XMLChar;
34
35 /** Some utility functions.
36  *
37  * @author bwm
38  * @version Release='$Name: $' Revision='$Revision: 1.7 $' Date='$Date: 2005/02/21 12:14:56 $'
39  */

40 public class Util extends Object JavaDoc {
41
42     public static final String JavaDoc CLASSPATH = "com.hp.hpl.jena";
43
44     /** Given an absolute URI, determine the split point between the namespace part
45      * and the localname part.
46      * If there is no valid localname part then the length of the
47      * string is returned.
48      * The algorithm tries to find the longest NCName at the end
49      * of the uri, not immediately preceeded by the first colon
50      * in the string.
51      * @param uri
52      * @return the index of the first character of the localname
53      */

54     public static int splitNamespace(String JavaDoc uri) {
55         char ch;
56         int lg = uri.length();
57         if (lg == 0)
58             return 0;
59         int j;
60         int i;
61         for (i = lg - 1; i >= 1; i--) {
62             ch = uri.charAt(i);
63             if (notNameChar(ch)) break;
64         }
65         for (j = i + 1; j < lg; j++) {
66             ch = uri.charAt(j);
67             if (XMLChar.isNCNameStart(ch)) {
68                 if (uri.charAt(j - 1) == ':'
69                     && uri.lastIndexOf(':', j - 2) == -1)
70                     continue; // split "mailto:me" as "mailto:m" and "e" !
71
else
72                     break;
73             }
74         }
75         return j;
76     }
77
78     /**
79         answer true iff this is not a legal NCName character, ie, is
80         a possible split-point start.
81     */

82     public static boolean notNameChar(char ch)
83         {
84         return !XMLChar.isNCName(ch);
85         }
86
87     public static String JavaDoc substituteStandardEntities(String JavaDoc s) {
88         s = replace(s, "&", "&amp;");
89         s = replace(s, "<", "&lt;");
90         s = replace(s, ">", "&gt;");
91         s = replace(s, "'", "&apos;");
92         s = replace(s, "\t", "&#9;");
93         s = replace(s, "\n", "&#xA;");
94         s = replace(s, "\r", "&#xD;");
95         return replace(s, "\"", "&quot;");
96     }
97     public static String JavaDoc substituteEntitiesInElementContent(String JavaDoc s) {
98         s = replace(s, "&", "&amp;");
99         return replace(s, "<", "&lt;");
100     }
101
102     public static String JavaDoc replace(
103         String JavaDoc s,
104         String JavaDoc oldString,
105         String JavaDoc newString) {
106         String JavaDoc result = "";
107         int length = oldString.length();
108         int pos = s.indexOf(oldString);
109         int lastPos = 0;
110         while (pos >= 0) {
111             result = result + s.substring(lastPos, pos) + newString;
112             lastPos = pos + length;
113             pos = s.indexOf(oldString, lastPos);
114         }
115         return result + s.substring(lastPos, s.length());
116     }
117
118     /** Call System.getProperty and suppresses SecurityException, (simply returns null).
119      *@return The property value, or null if none or there is a SecurityException.
120      */

121     public static String JavaDoc XgetProperty(String JavaDoc p) {
122         return XgetProperty( p, null );
123     }
124     /** Call System.getProperty and suppresses SecurityException, (simply returns null).
125      *@return The property value, or null if none or there is a SecurityException.
126      */

127     public static String JavaDoc XgetProperty(String JavaDoc p, String JavaDoc def) {
128         try {
129             return System.getProperty(p, def);
130         } catch (SecurityException JavaDoc e) {
131             return def;
132         }
133     }
134
135 }
136
Popular Tags