KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > language > markup > xsp > XSLTExtension


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
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.apache.cocoon.components.language.markup.xsp;
17
18 /**
19  * This class is used as a XSLT extension class. It is used by the XSP
20  * generation stylesheet to escape XML characters to make a valid Java strings.
21  *
22  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
23  * @version CVS $Id: XSLTExtension.java 30932 2004-07-29 17:35:38Z vgritsenko $
24  */

25 public class XSLTExtension {
26
27     /**
28      * Escapes '"' and '\' characters in a String (add a '\' before them) so that it can
29      * be inserted in java source + quote special characters as UTF-8
30      */

31     public static String JavaDoc escapeJavaString(String JavaDoc string) {
32         char chr[] = string.toCharArray();
33         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
34
35         for (int i = 0; i < chr.length; i++) {
36             char c = chr[i];
37             switch (c) {
38                 case '\t':
39                     buffer.append("\\t");
40                     break;
41                 case '\r':
42                     buffer.append("\\r");
43                     break;
44                 case '\n':
45                     buffer.append("\\n");
46                     break;
47                 case '\b':
48                     buffer.append("\\b");
49                     break;
50                 case '\f':
51                     buffer.append("\\f");
52                     break;
53                 case '"':
54                 case '\\':
55                     buffer.append('\\');
56                     buffer.append(c);
57                     break;
58                 default:
59                     if (' ' <= c && c <= 127) {
60                         buffer.append(c);
61                     } else {
62                         buffer.append("\\u");
63                         buffer.append(int2digit(c >> 12));
64                         buffer.append(int2digit(c >> 8));
65                         buffer.append(int2digit(c >> 4));
66                         buffer.append(int2digit(c));
67                     }
68                     break;
69             }
70         }
71
72         final String JavaDoc encoded = buffer.toString();
73         return encoded;
74     }
75
76     /**
77      * Quote special characters as UTF-8
78      *
79      * TC: It's code duplication but that way we don't
80      * have to iterate through the StringBuffer twice
81      */

82     public static String JavaDoc escapeString(String JavaDoc string) {
83         char chr[] = string.toCharArray();
84         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
85         for (int i = 0; i < chr.length; i++) {
86             char c = chr[i];
87             if (c <= 127) {
88                 buffer.append(c);
89             } else {
90                 buffer.append("\\u");
91                 buffer.append(int2digit(c >> 12));
92                 buffer.append(int2digit(c >> 8));
93                 buffer.append(int2digit(c >> 4));
94                 buffer.append(int2digit(c));
95             }
96         }
97
98         final String JavaDoc encoded = buffer.toString();
99         return encoded;
100     }
101
102     private static char int2digit(int x) {
103         x &= 0xF;
104         if (x <= 9) return (char)(x + '0');
105         else return (char)(x - 10 + 'A');
106     }
107
108     /**
109      * @see #escapeString(String)
110      */

111     public String JavaDoc escape(String JavaDoc string) {
112         return escapeString(string);
113     }
114
115     /**
116      * This method used by Java XSP core logicsheet.
117      * @see #escapeJavaString(String)
118      */

119     public String JavaDoc escapeJava(String JavaDoc string) {
120         return escapeJavaString(string);
121     }
122
123     /**
124      * Counts amount of spaces in the input line from the beginning
125      * to the first new line symbol and returns a string with this
126      * amount of spaces.
127      *
128      * Used by the Python XSP core logicsheet.
129      */

130     public String JavaDoc prefix(String JavaDoc string) {
131         char chr[] = string.toCharArray();
132         int i;
133         for (i = 0; i < chr.length; i++) {
134             if (chr[i] == '\n' || chr[i] == '\r')
135                 break;
136         }
137         if (i == chr.length) {
138             return "";
139         }
140
141         int j = 0;
142         for (; i < chr.length; i++) {
143             if (chr[i] == '\n' || chr[i] == '\r') {
144                 j = 0;
145             } else if (!Character.isSpaceChar(chr[i])) {
146                 break;
147             } else {
148                 j ++;
149             }
150         }
151
152         // System.out.println("<" + string + "> prefix:" + j);
153
StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
154         for (i = 0; i < j; i++) {
155             buffer.append(' ');
156         }
157         return buffer.toString();
158     }
159
160     /**
161      * Counts amount of spaces in the input line from the end
162      * to the last new line symbol and returns a string with this
163      * amount of spaces.
164      *
165      * Used by the Python XSP core logicsheet.
166      */

167     public String JavaDoc suffix(String JavaDoc string) {
168         char chr[] = string.toCharArray();
169
170         int j = 0;
171         for (int i = chr.length-1; i >=0; i--) {
172             if (chr[i] == '\n' || chr[i] == '\r')
173                 break;
174             if (!Character.isSpaceChar(chr[i]))
175                 return "";
176             j ++;
177         }
178
179         // System.out.println("<" + string + "> suffix:" + j);
180
StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
181         for (int i = 0; i < j; i++) {
182             buffer.append(' ');
183         }
184         return buffer.toString();
185     }
186 }
187
Popular Tags