morgand 01/08/29 16:15:23
Modified: latka/src/java/org/apache/commons/latka/xml
XMLPreprocessor.java
Log:
added method to find all the variables in an unprocessed XML suite
Revision Changes Path
1.8 +101 -12
jakarta-commons/latka/src/java/org/apache/commons/latka/xml/XMLPreprocessor.java
Index: XMLPreprocessor.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/latka/src/java/org/apache/commons/latka/xml/XMLPreprocessor.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- XMLPreprocessor.java 2001/08/23 20:13:45 1.7
+++ XMLPreprocessor.java 2001/08/29 23:15:23 1.8
@@ -65,15 +65,39 @@
import java.io.Reader;
import java.io.StringReader;
+import java.util.HashSet;
import java.util.Properties;
+import java.util.Set;
import org.apache.commons.latka.LatkaProperties;
import org.apache.regexp.RE;
import org.apache.regexp.RESyntaxException;
+/**
+ * Methods for massaging the Latka XML. Includes methods
+ * for finding and substituting variables in the XML
+ * stream with Latka variables.
+ *
+ * @author Morgan Delagrange
+ * @see org.apache.commons.latka.LatkaProperties
+ */
public class XMLPreprocessor {
+ protected static final String _variableExpr = "\\$\\{(.*?)\\}";
+
+ /**
+ * Processes a Stream, preparing it for the final XML
+ * processing. In essence, this consists of finding
+ * variables in the XML, looking for corresponding Latka
+ * properties, and performing the substitutions.
+ *
+ * @param reader XML stream to preprocess
+ * @return Stream containing the processed XML
+ * @exception IOException
+ * thrown if the stream could not be read,
+ * or if variables could not be substituted
+ */
public Reader preprocessReader(Reader reader) throws IOException {
BufferedReader buffReader = new BufferedReader(reader);
@@ -91,48 +115,95 @@
return new StringReader(processedString);
}
+ /**
+ * Reads a file URI instead of a Reader, but otherwise
+ * similar to preprocessReader(Reader).
+ *
+ * @param uri File URI that will be processed
+ * @return Reader containing processed data
+ * @exception IOException
+ * If the URI could not be found, or if variables could
+ * not be substituted.
+ * @see #preprocessReader(Reader)
+ */
public Reader preprocessURI(String uri) throws IOException {
return preprocessReader(new FileReader(uri));
}
+
+ /**
+ * Given an XML suite, find the names of all variables that
+ * must be set in the LatkaProperties.
+ *
+ * @param xmlString XML String representing a Latka suite
+ * @return Array of variables that must be set. If no variables
+ * are present in the suite, a zero-length array is
+ * returned.
+ * @see org.apache.commons.latka.LatkaProperties
+ */
+ public Set findVariables(String xmlString) {
+ xmlString = stripXmlComments(xmlString);
+
+ HashSet set = new HashSet();
- protected String resolveVariables(String string) {
+ try {
+ RE r = new RE(_variableExpr); // Compile expression
- if (string == null) {
- return null;
+ //scan the input string match by match
+ int bufIndex = 0;
+
+ while (r.match(xmlString, bufIndex)) {
+ set.add(r.getParen(1));
+ bufIndex = r.getParenEnd(0);
+ }
+ } catch (RESyntaxException e) {
+ e.printStackTrace();
}
+ return set;
+
+ }
+
+ /**
+ * Given an XML suite, find the corresponding values
+ * of all variables and write them into the stream.
+ *
+ * @param xmlString XML String representing a Latka suite
+ * @return XML String with all variables resolved
+ * @exception IOException
+ * Thrown if a Latka variables could not be resolved
+ */
+ protected String resolveVariables(String xmlString) throws IOException {
+
StringBuffer output = new StringBuffer();
Properties props = LatkaProperties.getProperties();
try {
-
- //remove anything commented out
- RE removeComments = new RE("<!--.*?-->",RE.MATCH_SINGLELINE);
- string = removeComments.subst(string,"");
+ xmlString = stripXmlComments(xmlString);
+
// now, replace the remaining variables
- RE r = new RE("\\$\\{(.*?)\\}"); // Compile expression
+ RE r = new RE(_variableExpr); // Compile expression
//scan the input string match by match, writing to the buffer
int bufIndex = 0;
- while (r.match(string, bufIndex)) {
+ while (r.match(xmlString, bufIndex)) {
// append everything to the beginning of the match
- output.append(string.substring(bufIndex,r.getParenStart(0)));
+ output.append(xmlString.substring(bufIndex,r.getParenStart(0)));
// move marker to the end of the match
bufIndex = r.getParenEnd(0);
String prop = props.getProperty(r.getParen(1));
if (prop == null) {
- throw new NullPointerException("Property " + r.getParen(1) + " was not
defined.");
+ throw new IOException("Property " + r.getParen(1) + " was not defined.");
}
// append the substituted value to the end of the output
output.append(prop);
}
// grab anything remaining that did not match
- output.append(string.substring(bufIndex,string.length()));
+ output.append(xmlString.substring(bufIndex,xmlString.length()));
} catch (RESyntaxException e) {
e.printStackTrace();
@@ -143,6 +214,24 @@
return outputString;
+ }
+
+ /**
+ * Remove any comments from the XmlString
+ *
+ * @param xmlString String with XML comments
+ * @return String with no XML comments
+ */
+ protected String stripXmlComments(String xmlString) {
+ try {
+ //remove anything commented out
+ RE removeComments = new RE("<!--.*?-->",RE.MATCH_SINGLELINE);
+ xmlString = removeComments.subst(xmlString,"");
+ } catch (RESyntaxException e) {
+ e.printStackTrace();
+ }
+
+ return xmlString;
}
}