| Method from sax.SAX2Writer Detail: |
public void characters(char[] ch,
int start,
int length) {
out.print(normalize(new String(ch, start, length)));
out.flush();
}
|
public void endElement(String uri,
String local,
String raw) {
out.print("< /");
out.print(raw);
out.print(' >");
out.flush();
}
|
public void error(SAXParseException ex) {
System.err.println("[Error] "+
getLocationString(ex)+": "+
ex.getMessage());
}
|
public void fatalError(SAXParseException ex) throws SAXException {
System.err.println("[Fatal Error] "+
getLocationString(ex)+": "+
ex.getMessage());
throw ex;
}
|
public void ignorableWhitespace(char[] ch,
int start,
int length) {
characters(ch, start, length);
out.flush();
}
|
public static void main(String[] argv) {
///
Arguments argopt = new Arguments();
argopt.setUsage( new String[] {
"usage: java sax.SAX2Writer (options) uri ...","",
"options:",
" -n | -N Turn on/off namespace [default=on]",
" -v | -V Turn on/off validation [default=on]",
" -s | -S Turn on/off Schema support [default=on]",
" -c Canonical XML output.",
" -h This help screen."} );
// is there anything to do?
if (argv.length == 0) {
argopt.printUsage();
System.exit(1);
}
// vars
boolean canonical = false;
String parserName = DEFAULT_PARSER_NAME;
argopt.parseArgumentTokens(argv, new char[] { 'p"} );
int c;
String arg = null;
while ( ( arg = argopt.getlistFiles() ) != null ) {
outer:
while ( (c = argopt.getArguments()) != -1 ){
switch (c) {
case 'c":
canonical = true;
break;
case 'C":
canonical = false;
break;
case 'v":
setValidation = true;
break;
case 'V":
setValidation = false;
break;
case 'N":
setNameSpaces = false;
break;
case 'n":
setNameSpaces = true;
break;
case 'p":
parserName = argopt.getStringParameter();
break;
case 's":
setSchemaSupport = true;
break;
case 'S":
setSchemaSupport = false;
break;
case '?":
case 'h":
case '-":
argopt.printUsage();
System.exit(1);
break;
case -1:
break outer;
default:
break;
}
}
// print
System.err.println(arg+':");
print(parserName, arg, canonical);
}
}
Main program entry point. |
protected String normalize(String s) {
StringBuffer str = new StringBuffer();
int len = (s != null) ? s.length() : 0;
for (int i = 0; i < len; i++) {
char ch = s.charAt(i);
switch (ch) {
case '< ": {
str.append("<");
break;
}
case ' >": {
str.append(">");
break;
}
case '&": {
str.append("&");
break;
}
case '"": {
str.append(""");
break;
}
case '\r":
case '\n": {
if (canonical) {
str.append("");
str.append(Integer.toString(ch));
str.append(';");
break;
}
// else, default append char
}
default: {
str.append(ch);
}
}
}
return str.toString();
}
Normalizes the given string. |
public static void print(String parserName,
String uri,
boolean canonical) {
try {
DefaultHandler handler = new SAX2Writer(canonical);
XMLReader parser = (XMLReader)Class.forName(parserName).newInstance();
parser.setFeature( "http://xml.org/sax/features/validation",
setValidation);
parser.setFeature( "http://xml.org/sax/features/namespaces",
setNameSpaces );
parser.setFeature( "http://apache.org/xml/features/validation/schema",
setSchemaSupport );
parser.setContentHandler(handler);
parser.setErrorHandler(handler);
parser.parse(uri);
}
catch (Exception e) {
e.printStackTrace(System.err);
}
}
Prints the output from the SAX callbacks. |
public void processingInstruction(String target,
String data) {
out.print("< ?");
out.print(target);
if (data != null && data.length() > 0) {
out.print(' ");
out.print(data);
}
out.print("? >");
out.flush();
}
|
protected Attributes sortAttributes(Attributes attrs) {
AttributesImpl attributes = new AttributesImpl();
int len = (attrs != null) ? attrs.getLength() : 0;
for (int i = 0; i < len; i++) {
String name = attrs.getQName(i);
int count = attributes.getLength();
int j = 0;
while (j < count) {
if (name.compareTo(attributes.getQName(j)) < 0) {
break;
}
j++;
}
attributes.insertAttributeAt(j, name, attrs.getType(i),
attrs.getValue(i));
}
return attributes;
}
Returns a sorted list of attributes. |
public void startDocument() {
if (!canonical) {
out.println("< ?xml version=\"1.0\" encoding=\"UTF-8\"? >");
out.flush();
}
}
|
public void startElement(String uri,
String local,
String raw,
Attributes attrs) {
out.print('< ");
out.print(raw);
if (attrs != null) {
attrs = sortAttributes(attrs);
int len = attrs.getLength();
for (int i = 0; i < len; i++) {
out.print(' ");
out.print(attrs.getQName(i));
out.print("=\"");
out.print(normalize(attrs.getValue(i)));
out.print('"");
}
}
out.print(' >");
out.flush();
}
|
public void warning(SAXParseException ex) {
System.err.println("[Warning] "+
getLocationString(ex)+": "+
ex.getMessage());
}
|