001 package jcxCreate;
002
003 import com.bea.control.*;
004 import com.bea.xml.XmlCursor;
005 import com.bea.xml.XmlObject;
006 import java.lang.reflect.Method;
007 import java.lang.reflect.InvocationTargetException;
008 import java.io.File;
009 import java.io.InputStream;
010 import java.io.FileInputStream;
011
012 /**
013 * The XQuery control illustrates how to create a control
014 * that generates a JCX file. A JCX file enables the control's
015 * user to customize the control's interface.
016 *
017 * This control also includes a dialog for editing an attribute
018 * containing the XQuery expression. Each method of the control's
019 * extended interface is annotation with this attribute.
020 *
021 * @jcs:control-tags file="XQuery-tags.xml"
022 * @jcs:jc-jar label="XQuery"
023 * insert-wizard-class="jcxCreate.ide.XQueryWizard"
024 * version="0.8"
025 * icon-16="/images/xml.gif"
026 * palette-priority="5"
027 * group-name="Feature Sample Controls"
028 * description="Extensible control that creates a jcx and runs an XQuery."
029 * @editor-info:code-gen control-interface="true"
030 */
031 public class XQueryImpl implements XQuery, com.bea.control.Extensible, com.bea.control.ControlSource
032 {
033 /**
034 * @common:context
035 */
036 com.bea.control.ControlContext context;
037
038 /*
039 * A String to hold the XQuery expression specified by the control's
040 * user.
041 */
042 String m_expression = null;
043
044 /*
045 * Constants representing annotation and attribute names. The
046 * jc:query annotation will be written into the JCX file for
047 * each XQuery expression the control's user associates with a
048 * control method.
049 */
050 public static final String TAG_XQUERY = "jc:query";
051 public static final String ATTR_EXPRESSION = "expression";
052
053 /*
054 * The invoke method is implemented from the Extensible interface.
055 * It is a key aspect of any control that provides a customizable
056 * interface. When a method on a control's JCX file is called, that
057 * call is delegated to the invoke method. The name and arguments for
058 * the JCX method called are passed to invoke as arguments. Within
059 * the invoke method, you extract and process the method and arguments,
060 * returning a value if appropriate.
061 *
062 * A JCX file for the XQuery control would contain methods whose argument
063 * is an object containing the XML on which to execute the query. An
064 * annotation on that method would specify the XQuery expression to use.
065 */
066 public Object invoke(Method method, Object[] args) throws Throwable {
067 XmlCursor resultCursor;
068 try{
069 // Create an instance of the class designed to handle the XQuery.
070 XQueryUtil queryUtil = new XQueryUtil();
071
072 // Retrieve the XQuery expression from the annotation in the JCX.
073 m_expression = context.getMethodAttribute(TAG_XQUERY, ATTR_EXPRESSION);
074
075 // Get the XML Stream named by the first parameter.
076 // First look to see if it is a resource in the jar that will be created from this project.
077 // Any non-source files in this project will be moved to the jar by default
078 String filePath = new String().valueOf(args[0]);
079 InputStream iXml= null;
080 if (filePath.substring(0,1).equals("/"))
081 iXml= this.getClass().getClassLoader().getResourceAsStream(filePath);
082 // try as a file
083 if (iXml==null)
084 {
085 File f=new File(filePath);
086 if (f.exists())
087 iXml = new FileInputStream(f);
088 else
089 {
090 throw new ControlException("file not found at " + f.getAbsolutePath());
091 }
092 }
093 // Load the XML using the XMLBeans API.
094 XmlObject instanceDoc = XmlObject.Factory.parse(iXml);
095
096 /*
097 * Pass the value to the XQuery utility class, and return the results
098 * in an XMLBeans XmlCursor object.
099 */
100 resultCursor = queryUtil.runXQueryExpression(instanceDoc, m_expression);
101 return resultCursor;
102 } catch (InvocationTargetException ite) {
103 throw new ControlException("Error while executing the query: " + ite.getMessage(), ite);
104 } catch (Exception e) {
105 throw new ControlException("XQueryControl: Exception while executing expression.", e);
106 }
107 }
108
109 /*
110 * Implement onException for any unhandled exceptions.
111 */
112 public void context_onException(Exception e, String methodName, Object [] args){
113 throw new ControlException("XQuery control exception in " + methodName + ": ", e);
114 }
115 }
|