1 | package org.ten60.ura.xslt2; |
---|
2 | |
---|
3 | import org.ten60.netkernel.xml.representation.*; |
---|
4 | import org.ten60.netkernel.xml.util.XMLUtils; |
---|
5 | import org.ten60.netkernel.xml.xda.*; |
---|
6 | import org.ten60.netkernel.xml.util.NKFCatalogEntityResolver; |
---|
7 | |
---|
8 | import org.ten60.netkernel.layer1.meta.*; |
---|
9 | import org.ten60.netkernel.layer1.nkf.*; |
---|
10 | import org.ten60.netkernel.layer1.nkf.impl.*; |
---|
11 | |
---|
12 | import com.ten60.netkernel.urii.*; |
---|
13 | import com.ten60.netkernel.urii.representation.ITransrepresentor; |
---|
14 | import com.ten60.netkernel.urii.aspect.*; |
---|
15 | import com.ten60.netkernel.util.NetKernelException; |
---|
16 | |
---|
17 | import org.apache.xml.serializer.*; |
---|
18 | import javax.xml.parsers.*; |
---|
19 | import javax.xml.transform.*; |
---|
20 | import javax.xml.transform.sax.*; |
---|
21 | import javax.xml.transform.stream.*; |
---|
22 | |
---|
23 | import org.xml.sax.*; |
---|
24 | import org.xml.sax.helpers.*; |
---|
25 | import java.io.*; |
---|
26 | import java.util.*; |
---|
27 | import org.w3c.dom.*; |
---|
28 | |
---|
29 | import net.sf.saxon.*; |
---|
30 | import net.sf.saxon.om.*; |
---|
31 | import net.sf.saxon.tinytree.*; |
---|
32 | import net.sf.saxon.trans.*; |
---|
33 | import net.sf.saxon.xpath.*; |
---|
34 | |
---|
35 | /** |
---|
36 | * Creates a TinyTree Aspect from any XML |
---|
37 | * @author maw |
---|
38 | */ |
---|
39 | public class XML2TinyTree extends NKFTransreptorImpl |
---|
40 | { |
---|
41 | public boolean supports(IURRepresentation aFrom, Class aTo) |
---|
42 | { return ( aTo.isAssignableFrom(TinyTreeAspect.class) ); |
---|
43 | } |
---|
44 | |
---|
45 | public void transrepresent(INKFConvenienceHelper aContext) throws NetKernelException |
---|
46 | { try |
---|
47 | { Source aSource=null; |
---|
48 | IURRepresentation aResource = aContext.source(INKFRequestReadOnly.URI_SYSTEM); |
---|
49 | SAXParserFactory spf=SAXParserFactory.newInstance(); |
---|
50 | if(aResource.hasAspect(IAspectReadableBinaryStream.class)) |
---|
51 | { IAspectReadableBinaryStream rbs = (IAspectReadableBinaryStream)aResource.getAspect(IAspectReadableBinaryStream.class); |
---|
52 | XMLReader reader= spf.newSAXParser().getXMLReader(); |
---|
53 | reader.setEntityResolver(new NKFCatalogEntityResolver(aContext)); |
---|
54 | InputSource is= new InputSource(rbs.getInputStream()); |
---|
55 | is.setSystemId(aContext.getThisRequest().getURI()); |
---|
56 | aSource= new SAXSource(reader, is); |
---|
57 | } |
---|
58 | else if(aResource.hasAspect(IAspectBinaryStream.class)) |
---|
59 | { ByteArrayOutputStream baos=new ByteArrayOutputStream(2048); |
---|
60 | IAspectBinaryStream bs=(IAspectBinaryStream)aResource.getAspect(IAspectBinaryStream.class); |
---|
61 | bs.write(baos); |
---|
62 | XMLReader reader= spf.newSAXParser().getXMLReader(); |
---|
63 | reader.setEntityResolver(new NKFCatalogEntityResolver(aContext)); |
---|
64 | InputSource is= new InputSource(new ByteArrayInputStream(baos.toByteArray())); |
---|
65 | is.setSystemId(aContext.getThisRequest().getURI()); |
---|
66 | aSource= new SAXSource(reader, is); |
---|
67 | } |
---|
68 | if(aSource==null) |
---|
69 | { IAspectReadableBinaryStream rbs = (IAspectReadableBinaryStream)aContext.sourceAspect(INKFRequestReadOnly.URI_SYSTEM, IAspectReadableBinaryStream.class); |
---|
70 | XMLReader reader= spf.newSAXParser().getXMLReader(); |
---|
71 | reader.setEntityResolver(new NKFCatalogEntityResolver(aContext)); |
---|
72 | InputSource is= new InputSource(rbs.getInputStream()); |
---|
73 | is.setSystemId(aContext.getThisRequest().getURI()); |
---|
74 | aSource= new SAXSource(reader, is); |
---|
75 | } |
---|
76 | |
---|
77 | XPathEvaluator eval = new XPathEvaluator(XMLToXSLTransformer.getSharedSaxonConfiguration()); |
---|
78 | NodeInfo tree = eval.setSource(aSource); |
---|
79 | |
---|
80 | IURAspect aspect = new TinyTreeAspect(tree); |
---|
81 | |
---|
82 | aContext.createResponseFrom(aspect).setCreationCost(32); |
---|
83 | } catch (Exception e) |
---|
84 | { NetKernelException nke = new NetKernelException("Unhandled Exception in XML2TinyTree"); |
---|
85 | nke.addCause(e); |
---|
86 | throw nke; |
---|
87 | } |
---|
88 | } |
---|
89 | } |
---|