Author: nbelaevski
Date: 2008-04-06 15:53:31 -0400 (Sun, 06 Apr 2008)
New Revision: 7622
Modified:
trunk/cdk/generator/src/main/java/org/ajax4jsf/builder/generator/ResourcesConfigGenerator.java
Log:
http://jira.jboss.com/jira/browse/RF-2944
Modified:
trunk/cdk/generator/src/main/java/org/ajax4jsf/builder/generator/ResourcesConfigGenerator.java
===================================================================
---
trunk/cdk/generator/src/main/java/org/ajax4jsf/builder/generator/ResourcesConfigGenerator.java 2008-04-06
16:44:18 UTC (rev 7621)
+++
trunk/cdk/generator/src/main/java/org/ajax4jsf/builder/generator/ResourcesConfigGenerator.java 2008-04-06
19:53:31 UTC (rev 7622)
@@ -27,6 +27,7 @@
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
+import java.io.StringReader;
import java.io.Writer;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@@ -37,6 +38,7 @@
import java.util.List;
import java.util.Set;
+import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
@@ -56,6 +58,7 @@
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.xml.sax.Attributes;
+import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
@@ -69,6 +72,8 @@
private File resourcesConfig;
private File templatesDirectory;
+ private static final SAXParserFactory parserFactory = SAXParserFactory.newInstance();
+
public ResourcesConfigGenerator(JSFGeneratorConfiguration task, Logger log) {
super(task, log);
}
@@ -262,6 +267,50 @@
}
}
+ private void parseXCSSResource(String resourcePath, final ResourcesConfigGeneratorBean
bean) throws SAXException, IOException, ParserConfigurationException {
+ InputStream resourceStream = getClassLoader().getResourceAsStream(resourcePath);
+ if (resourceStream != null) {
+ debug("XCSS file exists in classpath");
+
+ try {
+ SAXParser parser = parserFactory.newSAXParser();
+ parser.parse(resourceStream, new DefaultHandler() {
+ @Override
+ public void startElement(String uri,
+ String localName, String name,
+ Attributes attributes)
+ throws SAXException {
+
+ super.startElement(uri, localName, name, attributes);
+
+ if ("f:resource".equals(name)) {
+ String value = attributes.getValue("f:key");
+
+ if (value != null) {
+ debug("Adding resource: " + value);
+
+ addResource(value, "", bean);
+ }
+ }
+ }
+ });
+
+ } finally {
+ try {
+ resourceStream.close();
+ } catch (IOException e) {
+ getLog().error(e.getLocalizedMessage(), e);
+ }
+ }
+ } else {
+ getLog().error("Resource " + resourcePath + " hasn't been
found!");
+ }
+ }
+
+ private boolean isXCSSPath(String resourcePath) {
+ return resourcePath != null && resourcePath.endsWith(".xcss");
+ }
+
public void createFiles(BuilderConfig config) throws GeneratorException {
VelocityContext context = new VelocityContext();
Template template = getTemplate();
@@ -282,50 +331,66 @@
addResources(bean, rendererBean, config);
}
- Set<String> pathResourcesSet = new
LinkedHashSet<String>(bean.getPathResources().values());
- for (Iterator<String> iterator = pathResourcesSet.iterator(); iterator
- .hasNext();) {
- String resourcePath = iterator.next();
-
- if (resourcePath != null && resourcePath.endsWith(".xcss")) {
- debug("XCSS file detected: " + resourcePath);
+ String includedContent = getIncludeContent();
+ if (includedContent != null && includedContent.length() != 0) {
+ String parseableContent = "<?xml version=\"1.0\"
encoding=\"UTF-8\"?><resource-config>" + includedContent +
+ "</resource-config>";
+
+ SAXParser parser = parserFactory.newSAXParser();
+ parser.parse(new InputSource(new StringReader(parseableContent)), new
DefaultHandler() {
+ private StringBuilder path;
- InputStream resourceStream = getClassLoader().getResourceAsStream(resourcePath);
- if (resourceStream != null) {
- debug("XCSS file exists in classpath");
+ @Override
+ public void startElement(String uri,
+ String localName, String name,
+ Attributes attributes)
+ throws SAXException {
- try {
- SAXParserFactory parserFactory = SAXParserFactory.newInstance();
- SAXParser parser = parserFactory.newSAXParser();
- parser.parse(resourceStream, new DefaultHandler() {
- @Override
- public void startElement(String uri,
- String localName, String name,
- Attributes attributes)
- throws SAXException {
+ super.startElement(uri, localName, name, attributes);
+
+ if ("path".equals(name)) {
+ this.path = new StringBuilder();
+ }
+ }
+
+ public void characters(char[] ch, int start, int length) throws SAXException {
+ if (this.path != null) {
+ this.path.append(ch, start, length);
+ }
+ };
+
+ @Override
+ public void endElement(String uri, String localName, String name) throws
SAXException {
+ super.endElement(uri, localName, name);
- super.startElement(uri, localName, name, attributes);
-
- if ("f:resource".equals(name)) {
- String value = attributes.getValue("f:key");
-
- if (value != null) {
- debug("Adding resource: " + value);
-
- addResource(value, "", bean);
- }
- }
- }
- });
-
- } finally {
+ if ("resource".equals(name)) {
+ if (this.path != null && this.path.length() != 0) {
+ String resourcePath = this.path.toString().trim();
+ if (isXCSSPath(resourcePath)) {
+ debug("XCSS file detected: " + resourcePath);
try {
- resourceStream.close();
+ parseXCSSResource(resourcePath, bean);
} catch (IOException e) {
- getLog().error(e.getLocalizedMessage(), e);
+ throw new SAXException(e.getLocalizedMessage(), e);
+ } catch (ParserConfigurationException e) {
+ throw new SAXException(e.getLocalizedMessage(), e);
}
+ }
}
+ this.path = null;
+ }
}
+ });
+ }
+
+ Set<String> pathResourcesSet = new
LinkedHashSet<String>(bean.getPathResources().values());
+ for (Iterator<String> iterator = pathResourcesSet.iterator(); iterator
+ .hasNext();) {
+ String resourcePath = iterator.next();
+
+ if (isXCSSPath(resourcePath)) {
+ debug("XCSS file detected: " + resourcePath);
+ parseXCSSResource(resourcePath, bean);
}
}