Difference between revisions of "JET FAQ How do I create custom XPath Function?"

From Eclipsepedia

Jump to: navigation, search
m (Answer: indentation)
(Answer)
Line 10: Line 10:
 
2. In the Plug-in manifest editor, go to the Dependencies tab and add : org.eclipse.jet, org.eclipse.core.runtime, org.eclipse.core.resources
 
2. In the Plug-in manifest editor, go to the Dependencies tab and add : org.eclipse.jet, org.eclipse.core.runtime, org.eclipse.core.resources
  
3. In the Plug-in manifest editor, go to the Extensions tab, and add org.eclipse.jet.taglibraries.
+
3. In the Plug-in manifest editor, go to the Extensions tab, and add org.eclipse.jet.xpathfunctions.
  
4. Create a tagLibrary entry under this. Give it an ID like "MyCustomWorkspaceTags", and a default prefix like mcws.
+
4. Add a xpathFunction entry under this. Set the name, and the min/max arguments as you wish.
  
5. Under the tagLibrary entry, create a tag of wanted type and name.
+
5. Click the Implementation link, and specify a Class to implement the function.
  
5. Under the Tag node, add as much attributes you need, and set options (required or note, type).
+
6. Code the class.
  
6. Return the the conditionalTag entry, and click on the 'class' entry to create the Java class for the tag. Specify a class name and check the package, a case problem could happen.
+
The following example is an XPathFunction which name is "resourceExists", and min/max attributes are set to 1.
 
+
7. Code the Java class.
+
 
+
 
+
The following example is an AbstractConditionalTag which name is "resourcesExists". It has an attribute called "path" (required = true, String).
+
  
 
<pre>
 
<pre>
package org.eclipse.jet.example.customtags;
+
package org.eclipse.jet.example.customfunction;
  
 +
import java.util.List;
 
import org.eclipse.core.resources.IResource;
 
import org.eclipse.core.resources.IResource;
 
import org.eclipse.core.resources.ResourcesPlugin;
 
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jet.JET2Context;
+
import org.eclipse.jet.xpath.XPathFunction;
import org.eclipse.jet.taglib.AbstractConditionalTag;
+
import org.eclipse.jet.xpath.XPathUtil;
import org.eclipse.jet.taglib.JET2TagException;
+
import org.eclipse.jet.taglib.TagInfo;
+
  
/**
+
public class ResourceExistsFunction implements XPathFunction {
* Implements &lt;mcws:resourceExists path="somepath"&gt;
+
 
*
+
   public Object evaluate(List args) {
*/
+
     String path = XPathUtil.xpathString(args.get(0));
public class ResourceExistsTag extends AbstractConditionalTag {
+
   public boolean doEvalCondition(TagInfo td, JET2Context context)
+
    throws JET2TagException {
+
     String path = getAttribute("path");
+
 
     IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(path);
 
     IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(path);
     return resource != null && resource.exists();
+
     return Boolean.valueOf(resource != null && resource.exists());
 
   }
 
   }
 
}
 
}
 
</pre>
 
</pre>
  
In your template :
+
In your template, usage would then be:
 
+
<pre>
Taglib declaration :
+
<c:if test="resourceExists('someproject/somefolder/somefile.c')">
<pre><%@taglib id="your-plugin-id.MyCustomWorkspaceTags" prefix="mcws"%></pre>
+
     .. do this if file exists
 
+
</c:if>
Tag usage :
+
<pre><mcws:resourceExists path="someproject/somefolder/somefile.c">
+
     ... do this if exits
+
</mcws:resourceExists>
+
 
</pre>
 
</pre>
  

Revision as of 11:52, 25 June 2007

Contents

Question

JET provides tools for creating custom XPath functions. How does it work ?

Question

JET provides an API for creating custom XPath functions. How can I make mine ?

Answer

1. Create a plug-in project

2. In the Plug-in manifest editor, go to the Dependencies tab and add : org.eclipse.jet, org.eclipse.core.runtime, org.eclipse.core.resources

3. In the Plug-in manifest editor, go to the Extensions tab, and add org.eclipse.jet.xpathfunctions.

4. Add a xpathFunction entry under this. Set the name, and the min/max arguments as you wish.

5. Click the Implementation link, and specify a Class to implement the function.

6. Code the class.

The following example is an XPathFunction which name is "resourceExists", and min/max attributes are set to 1.

package org.eclipse.jet.example.customfunction;

import java.util.List;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jet.xpath.XPathFunction;
import org.eclipse.jet.xpath.XPathUtil;

public class ResourceExistsFunction implements XPathFunction {

  public Object evaluate(List args) {
    String path = XPathUtil.xpathString(args.get(0));
    IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(path);
    return Boolean.valueOf(resource != null && resource.exists());
  }
}

In your template, usage would then be:

<c:if test="resourceExists('someproject/somefolder/somefile.c')">
    .. do this if file exists
</c:if>

See Also

Back to the M2T-JET-FAQ