KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > navigation > impl > NavigationUtil


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.navigation.impl;
17
18 public class NavigationUtil {
19
20     /**
21      * Filters out characters that might give problems in URL paths.
22      * Returns null if the node ID cannot be made valid (= when its
23      * empty after filtering, or when it starts with a digit which
24      * is preserved for document IDs).
25      *
26      * <p>See also
27      * http://www.w3.org/Addressing/URL/4_URI_Recommentations.html
28      */

29     public static String JavaDoc makeNodeIdValid(String JavaDoc nodeId) {
30         if (nodeId == null || nodeId.length() == 0)
31             return null;
32
33         StringBuffer JavaDoc result = new StringBuffer JavaDoc(nodeId.length());
34
35         for (int i = 0; i < nodeId.length(); i++) {
36             char c = nodeId.charAt(i);
37             switch (c) {
38                 case ':':
39                 case '?':
40                 case '/':
41                 case '#':
42                 case '%':
43                     break;
44                 default:
45                     result.append(c);
46             }
47         }
48
49
50         if (result.length() == 0)
51             return null;
52
53         // IDs starting with a digit are reserved for document IDs
54
if (Character.isDigit(result.charAt(0)))
55             return null;
56
57         return result.toString();
58     }
59 }
60
Popular Tags