com.sun.star.xml.sax.XAttributeList
* where attributes and their values can be added.
*/
public static class AttributeList implements XAttributeList {
private static class Attribute {
public String Name ;
public String Type ;
public String Value ;
}
private HashMaplog
* specified about each XDocumentHandler
method
* call.
*/
public AttributeList(PrintWriter log) {
this.log = log ;
}
public AttributeList(XAttributeList list) {
if (list == null) return ;
for (short i = 0; i < list.getLength(); i++) {
add(list.getNameByIndex(i), list.getTypeByIndex(i),
list.getValueByIndex(i)) ;
}
}
/**
* Adds an attribute with type and value specified.
* @param name The attribute name.
* @param type Value type (usually 'CDATA' used).
* @param value Attribute value.
*/
public void add(String name, String type, String value) {
Attribute attr = new Attribute() ;
attr.Name = name ;
attr.Type = type ;
attr.Value = value ;
attributes.add(attr) ;
attrByName.put(attr.Name, attr) ;
}
/**
* Adds an attribute with value specified. As a type of
* value 'CDATA' string specified.
* @param name The attribute name.
* @param value Attribute value.
*/
public void add(String name, String value) {
add(name, "CDATA", value) ;
}
/**
* Clears all attributes added before.
*/
public void clear() {
attrByName.clear() ;
attributes.clear() ;
}
/***************************************
* XAttributeList methods
****************************************/
public short getLength() {
if (log != null)
log.println("getLength() called -> " + attributes.size()) ;
return (short) attributes.size() ;
}
public String getNameByIndex(short idx) {
String name = attributes.get(idx).Name ;
if (log != null)
log.println("getNameByIndex(" + idx + ") called -> '" +
name + "'") ;
return name ;
}
public String getTypeByIndex(short idx) {
String type = attributes.get(idx).Type ;
if (log != null)
log.println("getTypeByIndex(" + idx + ") called -> '" +
type + "'") ;
return type;
}
public String getTypeByName(String name) {
String type = attrByName.get(name).Type ;
if (log != null)
log.println("getTypeByName('" + name + "') called -> '" +
type + "'") ;
return type;
}
public String getValueByIndex(short idx) {
String value = attributes.get(idx).Value ;
if (log != null)
log.println("getValueByIndex(" + idx + ") called -> '" +
value + "'") ;
return value;
}
public String getValueByName(String name) {
String value = attrByName.get(name).Value ;
if (log != null)
log.println("getValueByName('" + name + "') called -> '" +
value + "'") ;
return value;
}
}
/**
* This class writes all XML data handled into a stream specified
* in the constructor.
*/
public static class XMLWriter implements XDocumentHandler {
private PrintWriter _log = null ;
private String align = "" ;
/**
* Creates a SAX handler which writes all XML data
* handled into a log
stream specified.
*/
public XMLWriter(PrintWriter log) {
_log = log ;
}
/**
* Creates a SAX handler which does nothing.
*/
public XMLWriter() {
}
public void processingInstruction(String appl, String data) {
if (_log == null) return ;
_log.println(align + "" + appl + " " + data + "?>") ;
}
public void startDocument() {
if (_log == null) return ;
_log.println("START DOCUMENT:") ;
}
public void endDocument() {
if (_log == null) return ;
_log.println("END DOCUMENT:") ;
}
public void setDocumentLocator(XLocator loc) {
if (_log == null) return ;
_log.println("DOCUMENT LOCATOR: ('" + loc.getPublicId() +
"','" + loc.getSystemId() + "')") ;
}
public void startElement(String name, XAttributeList attr) {
if (_log == null) return ;
_log.print(align + "<" + name + " ") ;
if (attr != null) {
short attrLen = attr.getLength() ;
for (short i = 0; i < attrLen; i++) {
if (i != 0) _log.print(align + " ") ;
_log.print(attr.getNameByIndex(i) + "[" +
attr.getTypeByIndex(i) + "]=\"" +
attr.getValueByIndex(i) + "\"") ;
if (i+1 != attrLen) {
_log.println() ;
}
}
}
_log.println(">") ;
align += " " ;
}
public void endElement(String name) {
if (_log == null) return ;
align = align.substring(3) ;
_log.println(align + "" + name + ">") ;
}
public void characters(String chars) {
if (_log == null) return ;
_log.println(align + chars) ;
}
public void ignorableWhitespace(String sp) {
if (_log == null) return ;
_log.println(sp) ;
}
}
/**
* Checks if the XML structure is well formed (i.e. all tags opened must be
* closed and all tags opened inside a tag must be closed
* inside the same tag). It also checks parameters passed.
* If any collisions found appropriate error message is
* output into a stream specified. No XML data output, i.e.
* no output will be performed if no errors occur.
* After document is completed there is a way to cehck
* if the XML data and structure was valid.
*/
public static class XMLWellFormChecker extends XMLWriter {
protected boolean docStarted = false ;
protected boolean docEnded = false ;
protected ArrayListouterTag
.
*/
public void addTagEnclosed(String tag, String outerTag) {
tags.put(tag, outerTag) ;
}
/**
* Adds a character data which must be contained in the XML data.
*/
public void addCharacters(String ch) {
chars.put(ch, "") ;
}
/**
* Adds a character data which must be contained in the XML data and
* must be inside the tag with name outerTag
.
*/
public void addCharactersEnclosed(String ch, String outerTag) {
chars.put(ch, outerTag) ;
}
public void startElement(String name, XAttributeList attrs) {
super.startElement(name, attrs) ;
if (tags.containsKey(name)) {
String outerTag = tags.get(name);
if (!outerTag.equals("")) {
boolean isInTag = false ;
for (int i = 0; i < tagStack.size(); i++) {
if (outerTag.equals(tagStack.get(i))) {
isInTag = true ;
break ;
}
}
if (!isInTag) {
printError("Required tag <" + name + "> found, but is not enclosed in tag <" +
outerTag + ">") ;
allOK = false ;
}
}
tags.remove(name) ;
}
}
public void characters(String ch) {
super.characters(ch) ;
if (chars.containsKey(ch)) {
String outerTag = chars.get(ch);
if (!outerTag.equals("")) {
boolean isInTag = false ;
for (int i = 0; i < tagStack.size(); i++) {
if (outerTag.equals(tagStack.get(i))) {
isInTag = true ;
break ;
}
}
if (!isInTag) {
printError("Required characters '" + ch + "' found, but are not enclosed in tag <" +
outerTag + ">") ;
allOK = false ;
}
}
chars.remove(ch) ;
}
}
/**
* Checks if the XML data was valid and well formed and if
* all necessary tags and character data was found.
*/
public boolean checkTags() {
allOK &= isWellFormed() ;
IteratorattrValues[N][0]
element contains the name of Nth
* attribute, and attrValues[N][1]
element contains
* value of Nth attribute, if value is null
then the
* attribute value can be any.
*/
public Tag(String tagName, String[][] attrValues) {
name = tagName ;
attrList = new String[attrValues.length][3] ;
for (int i = 0; i < attrValues.length; i++) {
attrList[i][0] = attrValues[i][0] ;
attrList[i][1] = "CDATA" ;
attrList[i][2] = attrValues[i][1] ;
}
}
/**
* Gets tag String description.
*/
public String toString() {
String ret = "<" + name ;
for (int i = 0; i < attrList.length; i++) {
ret += " " + attrList[i][0] + "=";
if (attrList[i][2] == null) {
ret += "(not specified)";
} else {
ret += "\"" + attrList[i][2] + "\"";
}
}
ret += ">";
return ret ;
}
protected boolean checkAttr(int attrListIdx, XAttributeList list) {
short j = 0 ;
int listLen = list.getLength();
while(j < listLen) {
if (attrList[attrListIdx][0].equals(list.getNameByIndex(j))) {
if (attrList[attrListIdx][2] == null) return true ;
return attrList[attrListIdx][2].equals(list.getValueByIndex(j)) ;
}
j++ ;
}
return false ;
}
/**
* Checks if this tag matches tag passed in parameters.
* I.e. if tag specifies only it's name it mathes if names
* are equal (attributes don't make sense). If there are
* some attributes names specified in this tag method checks
* if all names present in attribute list list
* (attributes' values don't make sense). If attributes specified
* with values method checks if these attributes exist and
* have appropriate values.
*/
public boolean isMatchTo(String tagName, XAttributeList list) {
if (!name.equals(tagName)) return false;
boolean result = true ;
for (int i = 0; i < attrList.length; i++) {
result &= checkAttr(i, list) ;
}
return result ;
}
}
/**
* Class realises extended XML data checking. It has possibilities
* to check if a tag exists, if it has some attributes with
* values, and if this tag is contained in another tag (which
* also can specify any attributes). It can check if some
* character data exists inside any tag specified.
*/
public static class XMLChecker extends XMLWellFormChecker {
protected HashSet