1 | /****************************************************************************** |
---|
2 | * (c) Copyright 2002,2003, 1060 Research Ltd |
---|
3 | * |
---|
4 | * This Software is licensed to You, the licensee, for use under the terms of |
---|
5 | * the 1060 Public License v1.0. Please read and agree to the 1060 Public |
---|
6 | * License v1.0 [www.1060research.com/license] before using or redistributing |
---|
7 | * this software. |
---|
8 | * |
---|
9 | * In summary the 1060 Public license has the following conditions. |
---|
10 | * A. You may use the Software free of charge provided you agree to the terms |
---|
11 | * laid out in the 1060 Public License v1.0 |
---|
12 | * B. You are only permitted to use the Software with components or applications |
---|
13 | * that provide you with OSI Certified Open Source Code [www.opensource.org], or |
---|
14 | * for which licensing has been approved by 1060 Research Limited. |
---|
15 | * You may write your own software for execution by this Software provided any |
---|
16 | * distribution of your software with this Software complies with terms set out |
---|
17 | * in section 2 of the 1060 Public License v1.0 |
---|
18 | * C. You may redistribute the Software provided you comply with the terms of |
---|
19 | * the 1060 Public License v1.0 and that no warranty is implied or given. |
---|
20 | * D. If you find you are unable to comply with this license you may seek to |
---|
21 | * obtain an alternative license from 1060 Research Limited by contacting |
---|
22 | * license@1060research.com or by visiting www.1060research.com |
---|
23 | * |
---|
24 | * NO WARRANTY: THIS SOFTWARE IS NOT COVERED BY ANY WARRANTY. SEE 1060 PUBLIC |
---|
25 | * LICENSE V1.0 FOR DETAILS |
---|
26 | * |
---|
27 | * THIS COPYRIGHT NOTICE IS *NOT* THE 1060 PUBLIC LICENSE v1.0. PLEASE READ |
---|
28 | * THE DISTRIBUTED 1060_Public_License.txt OR www.1060research.com/license |
---|
29 | * |
---|
30 | * File: $RCSfile: XQueryCompiler.java,v $ |
---|
31 | * Version: $Name: $ $Revision: 1.6 $ |
---|
32 | * Last Modified: $Date: 2008/03/25 11:04:19 $ |
---|
33 | *****************************************************************************/ |
---|
34 | package org.ten60.ura.xquery; |
---|
35 | |
---|
36 | import com.ten60.netkernel.urii.IURRepresentation; |
---|
37 | import com.ten60.netkernel.util.NetKernelException; |
---|
38 | import com.ten60.netkernel.urii.aspect.IAspectBinaryStream; |
---|
39 | import com.ten60.netkernel.urii.aspect.IAspectReadableBinaryStream; |
---|
40 | import com.ten60.netkernel.urii.representation.ITransrepresentor; |
---|
41 | import com.ten60.netkernel.urii.*; |
---|
42 | import com.ten60.netkernel.urrequest.*; |
---|
43 | import com.ten60.netkernel.scheduler.*; |
---|
44 | import com.ten60.netkernel.module.ModuleDefinition; |
---|
45 | |
---|
46 | import org.ten60.netkernel.layer1.transrepresentation.ComplexTransreptorImpl; |
---|
47 | import org.ten60.netkernel.layer1.meta.DependencyMeta; |
---|
48 | import org.ten60.netkernel.layer1.representation.*; |
---|
49 | |
---|
50 | import org.ten60.netkernel.layer1.util.SuperStackClassLoader; |
---|
51 | |
---|
52 | import org.ten60.netkernel.layer1.nkf.*; |
---|
53 | import org.ten60.netkernel.layer1.nkf.impl.*; |
---|
54 | import com.ten60.netkernel.urii.aspect.*; |
---|
55 | |
---|
56 | import org.ten60.netkernel.xml.representation.*; |
---|
57 | import org.ten60.netkernel.xml.xda.*; |
---|
58 | import com.ten60.netkernel.util.SysLogger; |
---|
59 | |
---|
60 | import javax.xml.transform.*; |
---|
61 | import java.net.*; |
---|
62 | import org.w3c.dom.*; |
---|
63 | import java.io.*; |
---|
64 | |
---|
65 | /** |
---|
66 | * XMLToXSLTransformer |
---|
67 | * @author tab |
---|
68 | */ |
---|
69 | public class XQueryCompiler extends NKFTransreptorImpl |
---|
70 | { |
---|
71 | public boolean supports(IURRepresentation aFrom, Class aTo) |
---|
72 | { return aTo.isAssignableFrom(XQueryAspect.class); |
---|
73 | } |
---|
74 | |
---|
75 | protected void transrepresent(INKFConvenienceHelper context) throws Exception |
---|
76 | { IURRepresentation aFrom=context.source(INKFRequestReadOnly.URI_SYSTEM); |
---|
77 | String mime=aFrom.getMeta().getMimeType(); |
---|
78 | String query=null; |
---|
79 | if (mime.equals("text/xml") || mime.equals("application/xml") || aFrom.hasAspect(IAspectXDA.class) || aFrom.hasAspect(IAspectXmlObject.class) || aFrom.hasAspect(IAspectJDOM.class)) |
---|
80 | { // must be a wrapped script |
---|
81 | IAspectXDA xa=(IAspectXDA)context.sourceAspect(INKFRequestReadOnly.URI_SYSTEM,IAspectXDA.class); |
---|
82 | try |
---|
83 | { query=xa.getXDA().getText("/xquery", false); |
---|
84 | } |
---|
85 | catch(XPathLocationException e) |
---|
86 | { SysLogger.log(SysLogger.WARNING, this, "XML source found but no <xquery> element - attempting to use document as plain text query"); |
---|
87 | query=xa.getXDA().toString(); |
---|
88 | } |
---|
89 | } |
---|
90 | else |
---|
91 | { IAspectString sa=(IAspectString)context.sourceAspect(INKFRequestReadOnly.URI_SYSTEM,IAspectString.class); |
---|
92 | query=sa.getString(); |
---|
93 | } |
---|
94 | INKFResponse resp=context.createResponseFrom(aFrom); |
---|
95 | resp.addAspect(new XQueryAspect(query, context, getResolver(context), getClassLoader(context))); |
---|
96 | context.setResponse(resp); |
---|
97 | } |
---|
98 | |
---|
99 | private ClassLoader getClassLoader(INKFConvenienceHelper aContext) throws Exception |
---|
100 | { return new SuperStackClassLoader(aContext.getKernelHelper().getThisKernelRequest()); |
---|
101 | |
---|
102 | } |
---|
103 | |
---|
104 | private URIResolver getResolver(INKFConvenienceHelper aContext) |
---|
105 | { final INKFConvenienceHelper context = aContext; |
---|
106 | return new URIResolver() |
---|
107 | { public Source resolve(String href, String base) throws TransformerException |
---|
108 | { URI resource=URI.create(href); |
---|
109 | if(!resource.isAbsolute() && context.getCWU()!=null) |
---|
110 | { resource=URI.create(context.getCWU().toString()).resolve(resource); |
---|
111 | } |
---|
112 | try |
---|
113 | { URIdentifier uri = new URIdentifier(resource.toString()); |
---|
114 | Document d = ((IAspectDOM)context.sourceAspect(resource.toString(), IAspectDOM.class)).getReadOnlyDocument(); |
---|
115 | return new javax.xml.transform.dom.DOMSource(d); |
---|
116 | } |
---|
117 | catch(NetKernelException e) |
---|
118 | { throw new TransformerException("Resource "+base+" "+href+" could not be accessed"); |
---|
119 | } |
---|
120 | } |
---|
121 | }; |
---|
122 | } |
---|
123 | } |
---|