Skip to content

Commit

Permalink
Merge pull request #479 from jsight/xslt_fixes
Browse files Browse the repository at this point in the history
WINDUP-465: Fixed some issues... some related files were missed in the i...
  • Loading branch information
jsight committed Feb 24, 2015
2 parents 66729a5 + bcf1baa commit 6615b65
Show file tree
Hide file tree
Showing 17 changed files with 91 additions and 867 deletions.
Expand Up @@ -2,12 +2,17 @@

import static org.joox.JOOX.$;

import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import org.jboss.forge.furnace.Furnace;
import org.jboss.forge.furnace.addons.Addon;
import org.jboss.forge.furnace.proxy.Proxies;
Expand All @@ -19,11 +24,12 @@
import org.jboss.windup.util.exception.WindupException;
import org.ocpsoft.rewrite.config.ConfigurationRuleBuilderPerform;
import org.ocpsoft.rewrite.config.ConfigurationRuleParameterWhere;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
* Handles maintaining the list of handlers associated with each tag/namespace pair, as well as selecting the right
* handler for element. This also maintains the current {@link WindupRuleProviderBuilder} being constructed.
* Handles maintaining the list of handlers associated with each tag/namespace pair, as well as selecting the right handler for element. This also
* maintains the current {@link WindupRuleProviderBuilder} being constructed.
*/
public class ParserContext
{
Expand All @@ -34,15 +40,18 @@ public class ParserContext
private final Map<HandlerId, ElementHandler<?>> handlers = new HashMap<>();

/**
* The addon containing the xml file currently being parsed. This is needed mainly because of the classloader that loaded the Addon (XSLTTransformation needs it.)
* The addon containing the xml file currently being parsed. This is needed mainly because of the classloader that loaded the Addon
* (XSLTTransformation needs it.)
*/
private Addon addonContainingInputXML;
/**
* The folder containing the xml file currently being parse. This should be the root folder from which any other
* resource lookups should be based. Eg, it may be the user scripts folder.
* The folder containing the xml file currently being parse. This should be the root folder from which any other resource lookups should be based.
* Eg, it may be the user scripts folder.
*
* If this is set, it should take precedent over the Addon for resource lookups.
*/
private Path xmlInputRootPath;

private Path xmlInputPath;

/**
Expand Down Expand Up @@ -89,6 +98,36 @@ public <T> T processElement(Element element) throws ConfigurationException
+ "] in namespace: [" + namespace + "]");
}

/**
* Processes the XML document at the provided {@link URL} and returns a result from the namespace element handlers.
*/
public <T> T processDocument(URI uri) throws ConfigurationException
{
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
dbFactory.setNamespaceAware(true);
DocumentBuilder dBuilder = null;

try
{
dBuilder = dbFactory.newDocumentBuilder();
}
catch (Exception e)
{
throw new WindupException("Failed to build xml parser due to: " + e.getMessage(), e);
}

try
{
Document doc = dBuilder.parse(uri.toString());
return processElement(doc.getDocumentElement());
}
catch (Exception e)
{
throw new WindupException("Failed to parse document at: " + uri + ", due to: " + e.getMessage(), e);
}

}

/**
* Gets a {@link List} of all {@link RuleProviders} found so far.
*/
Expand Down Expand Up @@ -170,24 +209,40 @@ public Addon getAddonContainingInputXML()
}

/**
* The folder containing the xml file currently being parse. This should be the root folder from which any other
* resource lookups should be based. Eg, it may be the user scripts folder.
*
* If this is set, it should take precedent over the Addon for resource lookups.
* The path to the rule xml file itself (eg, /path/to/rule.windup.xml).
*/
public void setXmlInputPath(Path xmlInputPath)
{
this.xmlInputPath = xmlInputPath;
}

/**
* The folder containing the xml file currently being parse. This should be the root folder from which any other
* resource lookups should be based. Eg, it may be the user scripts folder.
* The path to the rule xml file itself (eg, /path/to/rule.windup.xml).
*/
public Path getXmlInputPath()
{
return this.xmlInputPath;
}

/**
* The folder containing the xml file currently being parsed. This should be the root folder from which any other resource lookups should be
* based. Eg, it may be the user scripts folder.
*
* If this is set, it should take precedent over the Addon for resource lookups.
*/
public Path getXmlInputPath()
public void setXmlInputRootPath(Path xmlRootInputPath)
{
this.xmlInputRootPath = xmlRootInputPath;
}

/**
* The folder containing the xml file currently being parsed. This should be the root folder from which any other resource lookups should be
* based. Eg, it may be the user scripts folder.
*
* If this is set, it should take precedent over the Addon for resource lookups.
*/
public Path getXmlInputRootPath()
{
return xmlInputPath;
return xmlInputRootPath;
}
}
Expand Up @@ -116,7 +116,8 @@ public List<WindupRuleProvider> getProviders(GraphContext context)
ParserContext parser = new ParserContext(furnace);

String userRulesPath = userRulesFileModel.getFilePath();
parser.setXmlInputPath(Paths.get(userRulesPath));
parser.setXmlInputPath(Paths.get(resource.toURI()));
parser.setXmlInputRootPath(Paths.get(userRulesPath));

parser.processElement(doc.getDocumentElement());
List<WindupRuleProvider> parsedProviders = parser.getRuleProviders();
Expand Down
Expand Up @@ -93,12 +93,12 @@ private void checkForDuplicates(List<WindupRuleProvider> providers)
String typeMessage;
if (previousProvider.getClass().equals(provider.getClass()))
{
typeMessage = " (type: " + Proxies.unwrapProxyClassName(provider.getClass()) + ")";
typeMessage = " (type: " + previousProvider.getOrigin() + " and " + provider.getOrigin() + ")";
}
else
{
typeMessage = " (types: " + Proxies.unwrapProxyClassName(previousProvider.getClass()) + " and "
+ Proxies.unwrapProxyClassName(provider.getClass()) + ")";
typeMessage = " (types: " + Proxies.unwrapProxyClassName(previousProvider.getClass()) + " at " + previousProvider.getOrigin()
+ " and " + Proxies.unwrapProxyClassName(provider.getClass()) + " at " + provider.getOrigin() + ")";
}

throw new WindupException("Found two providers with the same id: " + provider.getID() + typeMessage);
Expand Down
Expand Up @@ -2,8 +2,8 @@

import static org.joox.JOOX.$;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -15,7 +15,6 @@
import org.jboss.windup.config.parser.ParserContext;
import org.jboss.windup.rules.apps.xml.operation.xslt.XSLTTransformation;
import org.jboss.windup.util.exception.WindupException;
import org.jboss.windup.util.xml.NamespaceEntry;
import org.ocpsoft.rewrite.config.Condition;
import org.w3c.dom.Element;

Expand Down Expand Up @@ -64,7 +63,7 @@ public XSLTTransformation processElement(ParserContext handlerManager, Element e
parameters.put(param.getKey(), param.getValue());
}

Path pathContainingXml = handlerManager.getXmlInputPath();
Path pathContainingXml = handlerManager.getXmlInputRootPath();
if (pathContainingXml != null)
{
String fullPath;
Expand All @@ -74,18 +73,30 @@ public XSLTTransformation processElement(ParserContext handlerManager, Element e
}
else
{
fullPath = pathContainingXml.resolve(template).toAbsolutePath().toString();
Path path = pathContainingXml.resolve(template).toAbsolutePath();

// If we can't find it at the default location, try relative to the rule xml file itself
if (!Files.exists(path))
{
// This contains the parent of the rule itself (eg, /path/to/rules/rule.windup.xml would result in /path/to/rules/)
Path rulesParentPath = handlerManager.getXmlInputPath().getParent();
fullPath = rulesParentPath.resolve(template).normalize().toAbsolutePath().toString();
}
else
{
fullPath = path.normalize().toString();
}
}
if (of != null)
{
return (XSLTTransformation)XSLTTransformation
return (XSLTTransformation) XSLTTransformation
.of(of)
.usingFilesystem(fullPath)
.withDescription(description)
.withExtension(extension)
.withParameters(parameters);
}
return (XSLTTransformation)XSLTTransformation
return (XSLTTransformation) XSLTTransformation
.usingFilesystem(fullPath)
.withDescription(description)
.withExtension(extension)
Expand All @@ -96,7 +107,7 @@ public XSLTTransformation processElement(ParserContext handlerManager, Element e
ClassLoader xmlFileAddonClassLoader = handlerManager.getAddonContainingInputXML().getClassLoader();
if (of != null)
{
return (XSLTTransformation)XSLTTransformation
return (XSLTTransformation) XSLTTransformation
.of(of)
.using(template, xmlFileAddonClassLoader)
.withDescription(description)
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 6615b65

Please sign in to comment.