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 ; 11 import java.util.StringTokenizer ; 12 13 16 17 public class XSLPreserveSpace extends StyleElement { 18 19 private String 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 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="*"; } 38 } 39 40 public void validate() throws XPathException { 41 checkTopLevel(null); 42 } 43 44 public Expression compile(Executable exec) throws XPathException 45 { 46 Boolean preserve = Boolean.valueOf(getFingerprint() == StandardNames.XSL_PRESERVE_SPACE); 47 Mode stripperRules = getPrincipalStylesheet().getStripperRules(); 48 49 51 StringTokenizer st = new StringTokenizer (elements); 52 while (st.hasMoreTokens()) { 53 String s = st.nextToken(); 54 NodeTestPattern pat = new NodeTestPattern(); 55 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 prefix = s.substring(0, s.length()-2); 74 String 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 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 prefix; 102 String localName = null; 103 String uri = null; 104 try { 105 String [] 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 | Popular Tags |