1 package com.icl.saxon.style; 2 import com.icl.saxon.tree.AttributeCollection; 3 import com.icl.saxon.*; 4 import com.icl.saxon.om.Name; 5 import com.icl.saxon.om.NamePool; 6 import com.icl.saxon.om.Namespace; 7 8 import com.icl.saxon.om.NamespaceException; 9 import com.icl.saxon.expr.*; 10 import com.icl.saxon.output.*; 11 import org.xml.sax.*; 12 import java.io.*; 13 import java.net.URL ; 14 import java.util.StringTokenizer ; 15 import java.util.Properties ; 16 17 import javax.xml.transform.*; 18 import javax.xml.transform.stream.StreamResult ; 19 import javax.xml.transform.sax.SAXSource ; 20 import javax.xml.transform.sax.TransformerHandler ; 21 22 34 35 public class XSLDocument extends XSLGeneralOutput { 36 37 41 42 public boolean isInstruction() { 43 return true; 44 } 45 46 50 51 public boolean mayContainTemplateBody() { 52 return true; 53 } 54 55 public void prepareAttributes() throws TransformerConfigurationException { 56 super.prepareAttributes(); 57 if (href==null) { 58 reportAbsence("href"); 59 } 60 } 61 62 public void validate() throws TransformerConfigurationException { 63 if (getURI().equals(Namespace.XSLT)) { 64 if (!forwardsCompatibleModeIsEnabled()) { 66 compileError("To use xsl:document, set xsl:stylesheet version='1.1'"); 67 } 68 } 69 checkWithinTemplate(); 70 } 71 72 public void process(Context context) throws TransformerException 73 { 74 Controller c = context.getController(); 75 Outputter oldOutputter = c.getOutputter(); 76 Properties prevProps = oldOutputter.getOutputProperties(); 77 Properties details = new Properties (prevProps); 78 updateOutputProperties(details, context); 79 Result result = null; 80 FileOutputStream stream; 81 82 86 String outFileName = href.evaluateAsString(context); 87 try { 88 File outFile = new File(outFileName); 89 if (!outFile.exists()) { 90 String parent = outFile.getParent(); if (parent!=null && !Version.isPreJDK12()) { 92 File parentPath = new File(parent); 93 if (parentPath != null && !parentPath.exists()) { 94 parentPath.mkdirs(); 95 } 96 outFile.createNewFile(); } 98 } 99 stream = new FileOutputStream(outFile); 100 result = new StreamResult (stream); 101 } catch (java.io.IOException err) { 102 throw new TransformerException("Failed to create output file " + outFileName, err); 103 } 104 105 if (nextInChain != null) { 106 String href = nextInChain.evaluateAsString(context); 107 TransformerHandler nextStyleSheet = prepareNextStylesheet(href, context); 108 ContentHandlerProxy emitter = new ContentHandlerProxy(); 109 emitter.setSystemId(this.getSystemId()); emitter.setUnderlyingContentHandler(nextStyleSheet); 111 emitter.setRequireWellFormed(false); 112 nextStyleSheet.setResult(result); 113 result = emitter; 114 } 115 116 c.changeOutputDestination(details, result); 117 processChildren(context); 118 c.resetOutputDestination(oldOutputter); 119 try { 120 stream.close(); 121 } catch (java.io.IOException err) { 122 throw new TransformerException("Failed to close output file", err); 123 } 124 } 125 126 } 127 128 | Popular Tags |