Listing 7: Processing translated XML subdocuments
/**
* Create a properties file using the specified XML subdocument based on the lookup
* keys of the argument table.
*/
public void processSubDocument(String uri, Vector translatedKeys)
{
setTranslatedKeys(translatedKeys);
SAXParser parser = new SAXParser();
parser.setContentHandler(this);
parser.setErrorHandler(new SAXErrorHandler());
try
{
parser.parse(uri);
}
catch (SAXException e)
{
MessageLogger.logMessage(MessageCodes.SAX_EXCEPTION, true, false, e);
Exception embeddedException = e.getException();
MessageLogger.logMessage(MessageCodes.JAVA_EXCEPTION, true, false, embeddedException);
}
catch (Exception e)
{
MessageLogger.logMessage(MessageCodes.JAVA_EXCEPTION, true, true, e);
}
return writePropertiesFileFromTable(getTranslatedStrings());
}
/**
* Handle the start element event.
*
* See if the element parsed was referenced in the main XML document. If so,
* set a flag to catch it in the characters event and remember the tag.
*/
public void startElement(String uri, String localName, String rawName, Attributes attrs)
{
if (getTranslatedKeys().containsValue(localName))
{
isTargetKey = true;
currentLookupKey = localName;
}
}
/**
* Handle the characters event.
*
* Check the flag to see if the element for this data was referenced in the
* main XML document. If so, grab the data and save it as a string
* with its element tag as the key.
*/
public void characters(char ch[], int start, int length)
{
if (isTargetKey)
{
String translation = (new String(ch, start, length)).trim();
getTranslatedStrings().put(currentLookupKey, translation);
isTargetKey = false;
currentLookupKey = null;
}
}
|
