KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > om > ProcInstParser


1 package com.icl.saxon.om;
2
3
4 /**
5   * ProcInstParser is used to parse pseudo-attributes within Processing Instructions
6   * @author Michael H. Kay (mhkay@iclway.co.uk)
7   * @version 10 July 2000
8   */

9   
10
11 public class ProcInstParser {
12
13     /**
14     * Get a pseudo-attribute. This is useful only if the processing instruction data part
15     * uses pseudo-attribute syntax, which it does not have to. This syntax is as described
16     * in the W3C Recommendation "Associating Style Sheets with XML Documents".
17     * @return the value of the pseudo-attribute if present, or null if not
18     */

19
20     public static String JavaDoc getPseudoAttribute(String JavaDoc content, String JavaDoc name) {
21
22         boolean inquotes = false;
23         int pos = 0;
24         while (pos <= content.length()-4) { // minimum length of x=""
25
int nextQuote = -1;
26             for (int q=pos; q<content.length(); q++) {
27                 if (content.charAt(q)=='"' || content.charAt(q)=='\'') {
28                     nextQuote = q;
29                     break;
30                 }
31             }
32             if (nextQuote < 0) return null;
33             //if (nextQuote+1 == name.length()) return null;
34

35             int closingQuote = content.indexOf(content.charAt(nextQuote), nextQuote+1);
36             if (closingQuote<0) return null;
37             int nextName = content.indexOf(name, pos);
38             if (nextName < 0) return null;
39             if (nextName < nextQuote) {
40                 // check only spaces and equal signs between the name and the quote
41
boolean found = true;
42                 for (int s = nextName + name.length(); s < nextQuote; s++) {
43                     char c = content.charAt(s);
44                     if (!Character.isWhitespace(c) && c!='=') {
45                         found=false;
46                         break;
47                     }
48                 }
49                 if (found) {
50                     String JavaDoc val = content.substring(nextQuote+1, closingQuote);
51                     return unescape(val);
52                 }
53             }
54             pos = closingQuote + 1;
55         }
56         return null;
57     }
58
59     /**
60     * Interpret character references and built-in entity references
61     */

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