KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > style > XSLPreserveSpace


1 package net.sf.saxon.style;
2 import net.sf.saxon.expr.Expression;
3 import net.sf.saxon.instruct.Executable;
4 import net.sf.saxon.om.*;
5 import net.sf.saxon.pattern.*;
6 import net.sf.saxon.trans.Mode;
7 import net.sf.saxon.trans.XPathException;
8 import net.sf.saxon.type.Type;
9
10 import javax.xml.transform.TransformerConfigurationException JavaDoc;
11 import java.util.StringTokenizer JavaDoc;
12
13 /**
14 * An xsl:preserve-space or xsl:strip-space elements in stylesheet. <br>
15 */

16
17 public class XSLPreserveSpace extends StyleElement {
18
19     private String JavaDoc elements;
20
21     public void prepareAttributes() throws XPathException {
22
23         AttributeCollection atts = getAttributeList();
24
25         for (int a=0; a<atts.getLength(); a++) {
26             int nc = atts.getNameCode(a);
27             String JavaDoc f = getNamePool().getClarkName(nc);
28             if (f==StandardNames.ELEMENTS) {
29                 elements = atts.getValue(a);
30             } else {
31                 checkUnknownAttribute(nc);
32             }
33         }
34         if (elements==null) {
35             reportAbsence("elements");
36             elements="*"; // for error recovery
37
}
38     }
39
40     public void validate() throws XPathException {
41         checkTopLevel(null);
42     }
43
44     public Expression compile(Executable exec) throws XPathException
45     {
46         Boolean JavaDoc preserve = Boolean.valueOf(getFingerprint() == StandardNames.XSL_PRESERVE_SPACE);
47         Mode stripperRules = getPrincipalStylesheet().getStripperRules();
48
49         // elements is a space-separated list of element names
50

51         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(elements);
52         while (st.hasMoreTokens()) {
53             String JavaDoc s = st.nextToken();
54             NodeTestPattern pat = new NodeTestPattern();
55             // following information is used in conflict warnings
56
pat.setOriginalText(s);
57             pat.setSystemId(getSystemId());
58             pat.setLineNumber(getLineNumber());
59             NodeTest nt;
60             if (s.equals("*")) {
61                 nt = AnyNodeTest.getInstance();
62                 pat.setNodeTest(nt);
63                 stripperRules.addRule(
64                             pat,
65                             preserve,
66                             getPrecedence(),
67                             -0.5);
68
69             } else if (s.endsWith(":*")) {
70                 if (s.length()==2) {
71                     compileError("No prefix before ':*'");
72                 }
73                 String JavaDoc prefix = s.substring(0, s.length()-2);
74                 String JavaDoc uri = getURIForPrefix(prefix, false);
75                 nt = new NamespaceTest(
76                                     getTargetNamePool(),
77                                     Type.ELEMENT,
78                                     uri);
79                 pat.setNodeTest(nt);
80                 stripperRules.addRule(
81                             pat,
82                             preserve,
83                             getPrecedence(),
84                             -0.25);
85             } else if (s.startsWith("*:")) {
86                 if (s.length()==2) {
87                     compileError("No local name after '*:'");
88                 }
89                 String JavaDoc localname = s.substring(2);
90                 nt = new LocalNameTest(
91                                     getTargetNamePool(),
92                                     Type.ELEMENT,
93                                     localname);
94                 pat.setNodeTest(nt);
95                 stripperRules.addRule(
96                             pat,
97                             preserve,
98                             getPrecedence(),
99                             -0.25);
100             } else {
101                 String JavaDoc prefix;
102                 String JavaDoc localName = null;
103                 String JavaDoc uri = null;
104                 try {
105                     String JavaDoc[] parts = Name.getQNameParts(s);
106                     prefix = parts[0];
107                     uri = getURIForPrefix(prefix, false);
108                     if (uri == null) {
109                         undeclaredNamespaceError(prefix, "XTSE0280");
110                         return null;
111                     }
112                     localName = parts[1];
113                 } catch (QNameException err) {
114                     compileError("Element name " + s + " is not a valid QName", "XTSE0280");
115                     return null;
116                 }
117                 NamePool target = getTargetNamePool();
118                 int nameCode = target.allocate("", uri, localName);
119                 nt = new NameTest(Type.ELEMENT, nameCode, getNamePool());
120                 pat.setNodeTest(nt);
121                 stripperRules.addRule(
122                             pat,
123                             preserve,
124                             getPrecedence(),
125                             0);
126             }
127
128         }
129         return null;
130     }
131
132 }
133
134 //
135
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
136
// you may not use this file except in compliance with the License. You may obtain a copy of the
137
// License at http://www.mozilla.org/MPL/
138
//
139
// Software distributed under the License is distributed on an "AS IS" basis,
140
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
141
// See the License for the specific language governing rights and limitations under the License.
142
//
143
// The Original Code is: all this file.
144
//
145
// The Initial Developer of the Original Code is Michael H. Kay.
146
//
147
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
148
//
149
// Contributor(s): none.
150
//
151
Popular Tags