1 package com.icl.saxon.style; 2 import com.icl.saxon.Context; 3 import com.icl.saxon.StylesheetStripper; 4 import com.icl.saxon.TransformerFactoryImpl; 5 import com.icl.saxon.StandardURIResolver; 6 import com.icl.saxon.om.NodeInfo; 7 import com.icl.saxon.om.DocumentInfo; 8 import com.icl.saxon.om.NamePool; 9 import com.icl.saxon.tree.TreeBuilder; 10 import com.icl.saxon.tree.DocumentImpl; 11 import com.icl.saxon.tree.ElementImpl; 12 import com.icl.saxon.tree.AttributeCollection; 13 import javax.xml.transform.Source ; 14 15 import javax.xml.transform.TransformerConfigurationException ; 16 import javax.xml.transform.TransformerException ; 17 import javax.xml.transform.URIResolver ; 18 import javax.xml.transform.sax.SAXSource ; 19 import javax.xml.transform.dom.DOMSource ; 20 21 import org.w3c.dom.Node ; 22 23 24 28 29 public abstract class XSLGeneralIncorporate extends StyleElement { 30 31 String href; 32 DocumentImpl includedDoc; 33 34 37 38 public abstract boolean isImport(); 39 40 public void prepareAttributes() throws TransformerConfigurationException { 41 42 StandardNames sn = getStandardNames(); 43 AttributeCollection atts = getAttributeList(); 44 45 for (int a=0; a<atts.getLength(); a++) { 46 int nc = atts.getNameCode(a); 47 int f = nc & 0xfffff; 48 if (f==sn.HREF) { 49 href = atts.getValue(a); 50 } else { 51 checkUnknownAttribute(nc); 52 } 53 } 54 55 if (href==null) { 56 reportAbsence("href"); 57 } 58 } 59 60 public void validate() throws TransformerConfigurationException { 61 checkEmpty(); 64 checkTopLevel(); 65 } 66 67 public XSLStyleSheet getIncludedStyleSheet(XSLStyleSheet importer, int precedence) 68 throws TransformerConfigurationException { 69 70 if (href==null) { 71 return null; 73 } 74 75 checkEmpty(); 76 checkTopLevel(); 77 78 try { 79 XSLStyleSheet thisSheet = (XSLStyleSheet)getParentNode(); 80 DocumentInfo thisDoc = getDocumentRoot(); 81 TransformerFactoryImpl factory = 82 getPreparedStyleSheet().getTransformerFactory(); 83 Source source = factory.getURIResolver().resolve(href, getBaseURI()); 84 85 if (source==null) { 88 source = (new StandardURIResolver (factory)).resolve(href, getBaseURI()); 89 } 90 91 if (source instanceof NodeInfo) { 92 if (source instanceof Node ) { 93 source = new DOMSource ((Node )source); 94 } else { 95 throw new TransformerException ("URIResolver must not return a " + source.getClass()); 96 } 97 } 98 SAXSource saxSource = factory.getSAXSource(source, true); 99 100 102 XSLStyleSheet anc = thisSheet; 103 while(anc!=null) { 104 if (saxSource.getSystemId().equals(anc.getSystemId())) { 105 compileError("A stylesheet cannot " + getLocalName() + " itself"); 106 return null; 107 } 108 anc = anc.getImporter(); 109 } 110 111 113 NamePool pool = getDocumentRoot().getNamePool(); 114 StylesheetStripper styleStripper = new StylesheetStripper(); 115 styleStripper.setStylesheetRules(pool); 116 117 TreeBuilder builder = new TreeBuilder(); 118 builder.setNamePool(pool); 119 builder.setStripper(styleStripper); 120 builder.setNodeFactory(new StyleNodeFactory(pool)); 121 builder.setDiscardCommentsAndPIs(true); 122 builder.setLineNumbering(true); 123 124 includedDoc = (DocumentImpl)builder.build(saxSource); 125 126 128 ElementImpl outermost = (ElementImpl)includedDoc.getDocumentElement(); 129 if (outermost instanceof LiteralResultElement) { 130 includedDoc = ((LiteralResultElement)outermost).makeStyleSheet(getPreparedStyleSheet()); 131 outermost = (ElementImpl)includedDoc.getDocumentElement(); 132 } 133 134 if (!(outermost instanceof XSLStyleSheet)) { 135 compileError("Included document " + href + " is not a stylesheet"); 136 return null; 137 } 138 XSLStyleSheet incSheet = (XSLStyleSheet)outermost; 139 140 incSheet.setPrecedence(precedence); 141 incSheet.setImporter(importer); 142 incSheet.spliceIncludes(); 144 return incSheet; 145 146 } catch (TransformerException err) { 147 compileError(err); 148 return null; 149 } 150 } 151 152 public void process(Context context) 153 { 154 } 157 } 158 159 | Popular Tags |