KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > style > XSLPreserveSpace


1 package com.icl.saxon.style;
2 import com.icl.saxon.tree.AttributeCollection;
3 import com.icl.saxon.*;
4 import com.icl.saxon.expr.*;
5 import com.icl.saxon.pattern.*;
6 import com.icl.saxon.om.*;
7 import javax.xml.transform.*;
8 import java.util.*;
9
10 /**
11 * An xsl:preserve-space or xsl:strip-space elements in stylesheet.<BR>
12 */

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

49         StringTokenizer st = new StringTokenizer(elements);
50         while (st.hasMoreTokens()) {
51             String JavaDoc s = st.nextToken();
52             try {
53                 if (s.equals("*")) {
54                     stripperRules.addRule(
55                                 AnyNodeTest.getInstance(),
56                                 preserve,
57                                 getPrecedence(),
58                                 -0.5);
59                                 
60                 } else if (s.endsWith(":*")) {
61                     String JavaDoc prefix = s.substring(0, s.length()-2);
62                     stripperRules.addRule(
63                                 new NamespaceTest(
64                                         getNamePool(),
65                                         NodeInfo.ELEMENT,
66                                         getURICodeForPrefix(prefix)),
67                                 preserve,
68                                 getPrecedence(),
69                                 -0.25);
70                 } else {
71                     if (!Name.isQName(s)) {
72                         compileError("Element name " + s + " is not a valid QName");
73                     }
74                     stripperRules.addRule(
75                                 new NameTest(
76                                         NodeInfo.ELEMENT,
77                                         makeNameCode(s, false)),
78                                 preserve,
79                                 getPrecedence(),
80                                 0);
81                 }
82             } catch (NamespaceException err) {
83                 compileError(err.getMessage());
84             }
85         }
86     }
87
88     public void process(Context c) {}
89
90 }
91
92 //
93
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
94
// you may not use this file except in compliance with the License. You may obtain a copy of the
95
// License at http://www.mozilla.org/MPL/
96
//
97
// Software distributed under the License is distributed on an "AS IS" basis,
98
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
99
// See the License for the specific language governing rights and limitations under the License.
100
//
101
// The Original Code is: all this file.
102
//
103
// The Initial Developer of the Original Code is
104
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
105
//
106
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
107
//
108
// Contributor(s): none.
109
//
110
Popular Tags