Regular Expression Parser Source Code
package com.architech.parser;
import
com.architech.entry.Attribute; import
com.architech.entry.Entry; import java.io.BufferedWriter; import
java.util.StringTokenizer; import java.util.Vector;
// import clases from the "gnu.regexp"
package for Regular Expressions import gnu.regexp.RE; import gnu.regexp.REMatch;
public class rspRegExpParser
extends rspParser { private final static String
PARAM_REG_EXPR = "regularExpression"; private final
static String PARAM_ATTR_NAMES = "attributeNames";
private final static String ATTR_NAMES_TOKEN = ";";
private final static String DEFAULT_ATTR_NAME_PREFIX =
"ATTR_NAME_"; private RE
mRegExp; private Vector
mAttributeNames; private Vector
mFullAttributeNames;
// Reads
Parser's parameters and creates the regular expression object
// and the attribute names
Vectors.
public void initParser() throws
Exception
{ String regExpPattern =
getParam(PARAM_REG_EXPR); if
(regExpPattern == null)
{
regExpPattern = "";
} mRegExp =
new RE(regExpPattern);
mAttributeNames = new
Vector(); String attrNames =
getParam(PARAM_ATTR_NAMES);
if (attrNames != null)
{
StringTokenizer st = new StringTokenizer(attrNames,
ATTR_NAMES_TOKEN);
while (st.hasMoreTokens())
{
mAttributeNames.add(st.nextToken());
}
}
mFullAttributeNames =
(Vector)mAttributeNames.clone();
int missingAttrNum = mRegExp.getNumSubs() -
mFullAttributeNames.size();
for (int i=0; i<missingAttrNum; i++)
{
mFullAttributeNames.add(DEFAULT_ATTR_NAME_PREFIX +
i); } }
// Reads a line
from the input and parses it against the regular
expression // If the line matches the regular
expression returns an Entry object // populated
with Attributes matching the subexpressions; otherwise
- // returns an Entry object with no
Attributes.
public Entry readEntry() throws
Exception
{ String line =
getReader().readLine(); if
(line == null)
{
return null;
}
Entry entry = new
Entry(); if
(mRegExp.isMatch(line))
{
REMatch match =
mRegExp.getMatch(line);
for (int i=1; i<=mRegExp.getNumSubs(); i++)
{
String attrValue =
match.toString(i);
entry.addAttributeValue(mFullAttributeNames.elementAt(i-1),
attrValue);
}
} return
entry; }
// This method
actually iterates the Attributes corresponding to the
// "mAttributeNames" elements and forms a single
string from their values. // If the resulting
string matches the regular expression it is writen on a
// single line in the output; otherwise - nothing
is written in the output.
public void
writeEntry(Entry entry) throws Exception
{ String line =
""; for (int i=0;
i<mAttributeNames.size(); i++)
{
Attribute attr =
entry.getAttribute((String)mAttributeNames.elementAt(i));
if (attr != null)
{
line = line +
attr.getValue();
}
}
if
(mRegExp.isMatch(line))
{
BufferedWriter out =
getWriter();
out.write(line);
out.newLine();
} else
{
debug("Entry (" + line + ") did not match the regular
expression.");
} } }
|