KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > om > ProcInstParser


1 package net.sf.saxon.om;
2
3
4 /**
5   * ProcInstParser is used to parse pseudo-attributes within Processing Instructions
6   * @author Michael H. Kay
7   * @version 10 July 2000
8   */

9   
10
11 public class ProcInstParser {
12
13     /**
14      * Class is never instantiated
15      */

16
17     private ProcInstParser() {
18     }
19
20     /**
21     * Get a pseudo-attribute. This is useful only if the processing instruction data part
22     * uses pseudo-attribute syntax, which it does not have to. This syntax is as described
23     * in the W3C Recommendation "Associating Style Sheets with XML Documents".
24     * @return the value of the pseudo-attribute if present, or null if not
25     */

26
27     public static String JavaDoc getPseudoAttribute(String JavaDoc content, String JavaDoc name) {
28
29         int pos = 0;
30         while (pos <= content.length()-4) { // minimum length of x=""
31
int nextQuote = -1;
32             for (int q=pos; q<content.length(); q++) {
33                 if (content.charAt(q)=='"' || content.charAt(q)=='\'') {
34                     nextQuote = q;
35                     break;
36                 }
37             }
38             if (nextQuote < 0) return null;
39
40             int closingQuote = content.indexOf(content.charAt(nextQuote), nextQuote+1);
41             if (closingQuote<0) return null;
42             int nextName = content.indexOf(name, pos);
43             if (nextName < 0) return null;
44             if (nextName < nextQuote) {
45                 // check only spaces and equal signs between the name and the quote
46
boolean found = true;
47                 for (int s = nextName + name.length(); s < nextQuote; s++) {
48                     char c = content.charAt(s);
49                     if (!Character.isWhitespace(c) && c!='=') {
50                         found=false;
51                         break;
52                     }
53                 }
54                 if (found) {
55                     String JavaDoc val = content.substring(nextQuote+1, closingQuote);
56                     return unescape(val);
57                 }
58             }
59             pos = closingQuote + 1;
60         }
61         return null;
62     }
63
64     /**
65     * Interpret character references and built-in entity references
66     */

67
68     private static String JavaDoc unescape(String JavaDoc value) {
69         if (value.indexOf('&')<0) return value;
70         FastStringBuffer sb = new FastStringBuffer(value.length());
71         for (int i=0; i<value.length(); i++) {
72             char c = value.charAt(i);
73             if (c=='&') {
74                 if (i+2 < value.length() && value.charAt(i+1)=='#') {
75                     if (value.charAt(i+2)=='x') {
76                         int x = i+3;
77                         int charval = 0;
78                         while (x<value.length() && value.charAt(x)!=';') {
79                             int digit = "0123456789abcdef".indexOf(value.charAt(x));
80                             if (digit<0) {
81                                 digit = "0123456789ABCDEF".indexOf(value.charAt(x));
82                             }
83                             if (digit<0) {
84                                 return null;
85                             }
86                             charval = charval * 16 + digit;
87                             x++;
88                         }
89                         char hexchar = (char)charval;
90                         sb.append(hexchar);
91                         i=x;
92                     } else {
93                         int x = i+2;
94                         int charval = 0;
95                         while (x<value.length() && value.charAt(x)!=';') {
96                             int digit = "0123456789".indexOf(value.charAt(x));
97                             if (digit<0) {
98                                 return null;
99                             }
100                             charval = charval * 10 + digit;
101                             x++;
102                         }
103                         char decchar = (char)charval;
104                         sb.append(decchar);
105                         i=x;
106                     }
107                 } else if (value.substring(i+1).startsWith("lt;")) {
108                     sb.append('<');
109                     i+=3;
110                 } else if (value.substring(i+1).startsWith("gt;")) {
111                     sb.append('>');
112                     i+=3;
113                 } else if (value.substring(i+1).startsWith("amp;")) {
114                     sb.append('&');
115                     i+=4;
116                 } else if (value.substring(i+1).startsWith("quot;")) {
117                     sb.append('"');
118                     i+=5;
119                 } else if (value.substring(i+1).startsWith("apos;")) {
120                     sb.append('\'');
121                     i+=5;
122                 } else {
123                     return null;
124                 }
125
126             } else {
127                 sb.append(c);
128             }
129         }
130         return sb.toString();
131     }
132                                       
133 }
134
135
136 //
137
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
138
// you may not use this file except in compliance with the License. You may obtain a copy of the
139
// License at http://www.mozilla.org/MPL/
140
//
141
// Software distributed under the License is distributed on an "AS IS" basis,
142
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
143
// See the License for the specific language governing rights and limitations under the License.
144
//
145
// The Original Code is: all this file.
146
//
147
// The Initial Developer of the Original Code is Michael H. Kay.
148
//
149
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
150
//
151
// Contributor(s): none.
152
//
153
Popular Tags