due: Wednesday, Oct 22
Expand the given Lab3.java program to perform the following modifications to a
provided XML file.
- If present, remove the DOCTYPE entry before it reaches the SAX parser.
Without such a modification the program will abort upon encountering
a request for a non-existent dtd file (for instance, the ‘lab3.dtd’ file in
Figure 1(a)).
- Transform the XML file into an equivalent normalized XML file.
Two XML files are considered here to be equivalent XML files if
- The files may differ only in the definitions and usages of namespace
prefixes.
- The namespaces of corresponding elements and attributes are identical.
Figure 1(a) shows two equivalent XML files. The namespaces of their elements
and attributes are shown in Figure 1(b). The startElement method of section 5.6
shows how the namespaces can be traced.
A XML file is considered here to be a normalized XML file if
- Only the root element may contain namespace prefix definitions.
- Distinct non-empty prefixes are associated with different namespaces.
- At least one element name uses an empty prefix.
- The root element lists only namespaces used within the document.
The second XML file in Figure 1(a) is an example of a normalized XML
file.
import java.io.BufferedReader;
import java.io.Reader;
import java.io.FileReader;
import javax.xml.parsers.*;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
class Lab3 {
static public void main(String[] args) throws Exception {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
Reader reader = MyReader.getReader( args[0] );
InputSource in = new InputSource (reader);
xmlReader.parse( in );
} }
<.Lab3 reader.> -_-_-
class MyReader extends BufferedReader {
MyReader( FileReader fr ){
super( fr );
}
public int read() throws java.io.IOException {
int ch = super.read();
return ch;
}
public int read(char[] cbuf, int off, int len)
throws java.io.IOException {
char [] chs = new char[cbuf.length];
int n = 0;
while ( n < len ) {
int k = super.read(chs, off + n, len - n);
if( k < 0 ) { break; }
n += k;
if( !super.ready() ){ break; }
}
for( int i=0; i<n; i++ ){ cbuf[i] = chs[i]; }
return n;
}
public String readLine() throws java.io.IOException{
String s = super.readLine();
return s;
}
static MyReader getReader( String fileName ) throws Exception{
FileReader fr = new FileReader( fileName );
return new MyReader( fr );
}
}
-_-_-