Skip to content

Commit

Permalink
used XmlSlurper to implement XPath
Browse files Browse the repository at this point in the history
  • Loading branch information
chanwit authored and graemerocher committed Apr 16, 2009
1 parent 875fcf0 commit 7e14ff2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package org.codehaus.groovy.grails.commons;

import groovy.lang.GroovyClassLoader;
import groovy.util.XmlSlurper;
import groovy.util.slurpersupport.GPathResult;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.groovy.control.CompilerConfiguration;
Expand All @@ -25,13 +27,7 @@
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -41,6 +37,7 @@
*
* @author Steven Devijver
* @author Graeme Rocher
* @author Chanwit Kaewkasi
*
* @since 0.1
*
Expand Down Expand Up @@ -81,20 +78,19 @@ public void afterPropertiesSet() throws Exception {
inputStream = descriptor.getInputStream();

// Get all the resource nodes in the descriptor.
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList grailsClasses = (NodeList) xpath.evaluate(
"/grails/resources/resource",
new InputSource(inputStream),
XPathConstants.NODESET);
// Xpath: /grails/resources/resource, where root is /grails
GPathResult root = new XmlSlurper().parse(inputStream);
GPathResult resources = (GPathResult) root.getProperty("resources");
GPathResult grailsClasses = (GPathResult) resources.getProperty("resource");

// Each resource node should contain a full class name,
// so we attempt to load them as classes.
for (int i = 0; i < grailsClasses.getLength(); i++) {
Node node = grailsClasses.item(i);
for (int i = 0; i < grailsClasses.size(); i++) {
GPathResult node = (GPathResult) grailsClasses.getAt(i);
try {
classes.add(classLoader.loadClass(node.getTextContent()));
classes.add(classLoader.loadClass(node.text()));
} catch (ClassNotFoundException e) {
LOG.warn("Class with name ["+node.getTextContent()+"] was not found, and hence not loaded. Possible empty class or script definition?");
LOG.warn("Class with name ["+node.text()+"] was not found, and hence not loaded. Possible empty class or script definition?");
}
}
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import grails.util.BuildSettings;
import grails.util.BuildSettingsHolder;
import groovy.util.XmlSlurper;
import groovy.util.slurpersupport.GPathResult;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand All @@ -28,17 +30,8 @@
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;

import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -52,6 +45,7 @@
* set of resources
*
* @author Graeme Rocher
* @author Chanwit Kaewkasi
* @since 0.6
*
* <p/>
Expand Down Expand Up @@ -117,25 +111,21 @@ private void configureMetaManager(Resource[] pluginDescriptors) {

try {
inputStream = pluginDescriptor.getInputStream();
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(inputStream);
Element pluginElement = doc.getDocumentElement();
GPathResult pluginElement = new XmlSlurper().parse(inputStream);

String pluginName = pluginElement.getAttribute("name");
String pluginVersion = pluginElement.getAttribute("version");
String pluginName = ((GPathResult)(pluginElement.getProperty("@name"))).text();
String pluginVersion = ((GPathResult)(pluginElement.getProperty("@version"))).text();

if(StringUtils.isBlank(pluginName)) throw new GrailsConfigurationException("Plug-in descriptor ["+pluginDescriptor+"] doesn't specify a plug-in name. It must be corrupted, try re-install the plug-in");
if(StringUtils.isBlank(pluginVersion)) throw new GrailsConfigurationException("Plug-in descriptor ["+pluginDescriptor+"] with name ["+pluginName+"] doesn't specify a plug-in version. It must be corrupted, try re-install the plug-in");

XPath xpath = XPathFactory.newInstance().newXPath();
NodeList nodes = (NodeList) xpath.evaluate(
"/plugin/resources/resource",
doc,
XPathConstants.NODESET);
// XPath: /plugin/resources/resource, where pluginElement is /plugin
GPathResult resources = (GPathResult) pluginElement.getProperty("resources");
GPathResult nodes = (GPathResult) resources.getProperty("resource");
List pluginResources = new ArrayList();
for (int j = 0; j < nodes.getLength(); j++) {
Node node = nodes.item(j);
pluginResources.add(node.getTextContent());
for (int j = 0; j < nodes.size(); j++) {
GPathResult node = (GPathResult) nodes.getAt(j);
pluginResources.add(node.text());
}

PluginMeta pluginMeta = new PluginMeta(pluginName, pluginVersion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package org.codehaus.groovy.grails.plugins;

import groovy.lang.GroovyClassLoader;
import groovy.util.XmlSlurper;
import groovy.util.slurpersupport.GPathResult;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.groovy.grails.commons.GrailsApplication;
Expand All @@ -26,13 +28,7 @@
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -41,6 +37,7 @@
* A factory bean for loading the GrailsPluginManager instance
*
* @author Graeme Rocher
* @author Chanwit Kaewkasi
* @since 0.4
*
*/
Expand Down Expand Up @@ -87,14 +84,15 @@ public void afterPropertiesSet() throws Exception {

try {
inputStream = descriptor.getInputStream();
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList nodes = (NodeList) xpath.evaluate(
"/grails/plugins/plugin",
new InputSource(inputStream),
XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
final String pluginName = node.getTextContent();

// Xpath: /grails/plugins/plugin, where root is /grails
GPathResult root = new XmlSlurper().parse(inputStream);
GPathResult plugins = (GPathResult) root.getProperty("plugins");
GPathResult nodes = (GPathResult) plugins.getProperty("plugin");

for (int i = 0; i < nodes.size(); i++) {
GPathResult node = (GPathResult) nodes.getAt(i);
final String pluginName = node.text();
classes.add(classLoader.loadClass(pluginName));
}
} finally {
Expand Down

0 comments on commit 7e14ff2

Please sign in to comment.