KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > text > syntax > dom > Util


1 /* The contents of this file are subject to the terms of the Common Development
2 /* and Distribution License (the License). You may not use this file except in
3 /* compliance with the License.
4  *
5  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
6 /* or http://www.netbeans.org/cddl.txt.
7 /*
8 /* When distributing Covered Code, include this CDDL Header Notice in each file
9 /* and include the License file at http://www.netbeans.org/cddl.txt.
10 /* If applicable, add the following below the CDDL Header, with the fields
11 /* enclosed by brackets [] replaced by your own identifying information:
12 /* "Portions Copyrighted [year] [name of copyright owner]"
13  *
14  * The Original Software is NetBeans. The Initial Developer of the Original
15 /* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
16  * Microsystems, Inc. All Rights Reserved.
17  */

18
19 package org.netbeans.modules.xml.text.syntax.dom;
20
21 import org.netbeans.editor.TokenItem;
22
23 /**
24  * Library of shared complexities.
25  *
26  * @author Petr Kuzel
27  * @author asgeir@dimonsoftware.com
28  */

29 public class Util {
30
31     public static String JavaDoc[] knownEntityStrings = {"<", ">", "'", """, "&"};
32
33     public static char[] knownEntityChars = {'<', '>', '\'', '"', '&'};
34
35     /**
36      * Handle fuzziness of attribute end detection.
37      * @return TokenItem after attribute value or null.
38      */

39     public static TokenItem skipAttributeValue(TokenItem attribute, char delim) {
40         TokenItem next = attribute;
41         for (; next != null; next = next.getNext()) {
42             String JavaDoc image = next.getImage();
43             if (image.endsWith("" + delim)) {
44                 return next.getNext();
45             }
46         }
47         return null;
48     }
49     
50     /**
51      * This method looks for '<' and '>' characters in attributes values and
52      * returns whitespace-stripped substring which does not contain '<' or '>'.
53      * This method should be used to calculate an attribute value which has
54      * not currently been closed.
55      * @param attributeValue an original attribute value
56      * @return the same value of stripped substring of it.
57      */

58     public static String JavaDoc actualAttributeValue(String JavaDoc attributeValue) {
59         int ltIndex = attributeValue.indexOf('<');
60         int gtIndex = attributeValue.indexOf('>');
61         int firstUnwantedIndex = -1;
62         if (gtIndex != -1) {
63             if (ltIndex != -1 && ltIndex < gtIndex) {
64                 firstUnwantedIndex = ltIndex;
65             } else {
66                 firstUnwantedIndex = gtIndex;
67             }
68         } else {
69             firstUnwantedIndex = ltIndex;
70         }
71         
72         if (firstUnwantedIndex != -1) {
73             char charAtIndex = attributeValue.charAt(firstUnwantedIndex);
74             while (charAtIndex == ' ' || charAtIndex == '\t' || charAtIndex == '\n' ||
75             charAtIndex == '\r' || charAtIndex == '<' || charAtIndex == '>') {
76                 firstUnwantedIndex--;
77                 if (firstUnwantedIndex < 0) {
78                     break;
79                 }
80                 charAtIndex = attributeValue.charAt(firstUnwantedIndex);
81             }
82             
83             return attributeValue.substring(0, firstUnwantedIndex + 1);
84         } else {
85             return attributeValue;
86         }
87     }
88     
89     /**
90      * Replaces "&lt;", "&gt;", "&apos;", "&quot;", "&amp;" with
91      * '<', '>', '\'', '"', '&'.
92      * @param a string that may contain &lt;", "&gt;", "&apos;", "&quot;" and "&amp;"
93      * @return a string that may contain '<', '>', '\'', '"', '&'.
94      */

95     public static String JavaDoc replaceEntityStringsWithChars(String JavaDoc value) {
96         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(value);
97         for (int entity = 0; entity < knownEntityStrings.length; entity++) {
98             String JavaDoc curEntityString = knownEntityStrings[entity];
99             int indexOfEntity = buf.toString().indexOf(curEntityString);
100             while (indexOfEntity != -1) {
101                 buf.replace(indexOfEntity, indexOfEntity + curEntityString.length(),
102                 new String JavaDoc(new char[]{knownEntityChars[entity]}));
103                 indexOfEntity = buf.toString().indexOf(curEntityString);
104             }
105         }
106         
107         return buf.toString();
108     }
109     
110     /**
111      * Replaces '<', '>', '\'', '"', '&' with
112      * "&lt;", "&gt;", "&apos;", "&quot;", "&amp;".
113      * @param a string that may contain '<', '>', '\'', '"', '&'.
114      * @return a string that may contain &lt;", "&gt;", "&apos;", "&quot;" and "&amp;"
115      */

116     public static String JavaDoc replaceCharsWithEntityStrings(String JavaDoc value) {
117         if (value == null) {
118             return null;
119         }
120         StringBuffer JavaDoc replBuf = new StringBuffer JavaDoc(value.length());
121         for (int ind = 0; ind < value.length(); ind++) {
122             boolean charReplaced = false;
123             char curChar = value.charAt(ind);
124             for (int entity = 0; entity < knownEntityChars.length; entity++) {
125                 if (curChar == knownEntityChars[entity]) {
126                     replBuf.append(knownEntityStrings[entity]);
127                     charReplaced = true;
128                     break;
129                 }
130             }
131             
132             if (!charReplaced) {
133                 replBuf.append(curChar);
134             }
135         }
136         
137         return replBuf.toString();
138     }
139     
140 }
141
Popular Tags