| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.jaxp; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.event.CommentStripper; |
| 12 |
import net.sf.saxon.event.ReceivingContentHandler; |
| 13 |
import net.sf.saxon.event.StartTagBuffer; |
| 14 |
import net.sf.saxon.event.Stripper; |
| 15 |
import net.sf.saxon.om.StylesheetSpaceStrippingRule; |
| 16 |
import net.sf.saxon.s9api.Processor; |
| 17 |
import net.sf.saxon.s9api.SaxonApiException; |
| 18 |
import net.sf.saxon.s9api.XsltCompiler; |
| 19 |
import net.sf.saxon.style.Compilation; |
| 20 |
import net.sf.saxon.style.StyleNodeFactory; |
| 21 |
import net.sf.saxon.style.UseWhenFilter; |
| 22 |
import net.sf.saxon.style.XSLModuleRoot; |
| 23 |
import net.sf.saxon.trans.CompilerInfo; |
| 24 |
import net.sf.saxon.tree.linked.DocumentImpl; |
| 25 |
import net.sf.saxon.tree.linked.ElementImpl; |
| 26 |
import net.sf.saxon.tree.linked.LinkedTreeBuilder; |
| 27 |
import net.sf.saxon.value.NestedIntegerValue; |
| 28 |
import org.xml.sax.Locator; |
| 29 |
|
| 30 |
import javax.xml.transform.Templates; |
| 31 |
import javax.xml.transform.sax.TemplatesHandler; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public class TemplatesHandlerImpl extends ReceivingContentHandler implements TemplatesHandler { |
| 44 |
|
| 45 |
private Processor processor; |
| 46 |
private LinkedTreeBuilder builder; |
| 47 |
private StyleNodeFactory nodeFactory; |
| 48 |
private Templates templates; |
| 49 |
private String systemId; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
protected TemplatesHandlerImpl(Processor processor) { |
| 60 |
|
| 61 |
this.processor = processor; |
| 62 |
Configuration config = processor.getUnderlyingConfiguration(); |
| 63 |
setPipelineConfiguration(config.makePipelineConfiguration()); |
| 64 |
|
| 65 |
CompilerInfo info = new CompilerInfo(config.getDefaultXsltCompilerInfo()); |
| 66 |
Compilation compilation = new Compilation(config, info); |
| 67 |
nodeFactory = compilation.getStyleNodeFactory(true); |
| 68 |
|
| 69 |
builder = new LinkedTreeBuilder(getPipelineConfiguration()); |
| 70 |
builder.setNodeFactory(nodeFactory); |
| 71 |
builder.setLineNumbering(true); |
| 72 |
|
| 73 |
UseWhenFilter useWhenFilter = new UseWhenFilter(compilation, builder, NestedIntegerValue.TWO); |
| 74 |
|
| 75 |
StartTagBuffer startTagBuffer = new StartTagBuffer(useWhenFilter); |
| 76 |
useWhenFilter.setStartTagBuffer(startTagBuffer); |
| 77 |
|
| 78 |
StylesheetSpaceStrippingRule rule = new StylesheetSpaceStrippingRule(config.getNamePool()); |
| 79 |
Stripper styleStripper = new Stripper(rule, startTagBuffer); |
| 80 |
CommentStripper commentStripper = new CommentStripper(styleStripper); |
| 81 |
setReceiver(commentStripper); |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public Templates getTemplates() { |
| 91 |
if (templates == null) { |
| 92 |
DocumentImpl doc = (DocumentImpl) builder.getCurrentRoot(); |
| 93 |
if (doc == null) { |
| 94 |
return null; |
| 95 |
} |
| 96 |
ElementImpl top = doc.getDocumentElement(); |
| 97 |
if (!(top instanceof XSLModuleRoot)) { |
| 98 |
throw new IllegalStateException("Input is not a stylesheet"); |
| 99 |
} |
| 100 |
builder.reset(); |
| 101 |
|
| 102 |
try { |
| 103 |
XsltCompiler compiler = processor.newXsltCompiler(); |
| 104 |
templates = new TemplatesImpl(compiler.compile(doc)); |
| 105 |
} catch (SaxonApiException tce) { |
| 106 |
|
| 107 |
throw new IllegalStateException(tce.getMessage()); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
return templates; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public void setSystemId(String url) { |
| 125 |
systemId = url; |
| 126 |
builder.setSystemId(url); |
| 127 |
super.setDocumentLocator(new Locator() { |
| 128 |
public int getColumnNumber() { |
| 129 |
return -1; |
| 130 |
} |
| 131 |
|
| 132 |
public int getLineNumber() { |
| 133 |
return -1; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
public String getPublicId() { |
| 138 |
return null; |
| 139 |
} |
| 140 |
|
| 141 |
public String getSystemId() { |
| 142 |
return systemId; |
| 143 |
} |
| 144 |
}); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public void setDocumentLocator(final Locator locator) { |
| 152 |
|
| 153 |
|
| 154 |
if (systemId == null) { |
| 155 |
super.setDocumentLocator(locator); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public String getSystemId() { |
| 164 |
return systemId; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
} |
| 169 |
|