package xml.musicCatalog;
import java.io.*;
import futils.*;
import java.util.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Attr;
import org.w3c.dom.Node;
import org.w3c.dom.Text;
import org.w3c.dom.NodeList;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.DOMException;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class MyJaxpDomParser {
private String xmlFile = null;
private PrintStream out = null;
private Document domDoc = null;
private Element root = null;
final int ELEMENT_NODE = 1;
final int ATTR_NODE = 2;
final int TEXT_NODE = 3;
static final String JAXP_SCHEMA_LANGUAGE =
"http://java.sun.com/xml/jaxp/properties/schemaLanguage";
static final String W3C_XML_SCHEMA =
"http://www.w3.org/2001/XMLSchema";
static final String JAXP_SCHEMA_SOURCE =
"http://java.sun.com/xml/jaxp/properties/schemaSource";
public Document parse (String uri, OutputStream os)
throws Exception {
xmlFile = uri;
out = new PrintStream(os);
return parse(false, false);
}
public Document dtdParse (String uri, OutputStream os)
throws Exception {
xmlFile = uri;
out = new PrintStream(os);
return parse(true, false);
}
public Document xsdParse (String uri, OutputStream os)
throws Exception {
xmlFile = uri;
out = new PrintStream(os);
return parse(false, true);
}
private Document parse (boolean dtdValidate, boolean xsdValidate)
throws Exception {
try {
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
dbf.setIgnoringElementContentWhitespace(false);
dbf.setValidating(dtdValidate || xsdValidate);
if (xsdValidate) {
dbf.setNamespaceAware(true);
try {
dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
}
catch (IllegalArgumentException e) {
System.err.println("Error: " + JAXP_SCHEMA_LANGUAGE
+ " is not recognized by the DocumentBuilderFactory.");
System.exit(1);
}
String schemaSource = null;
if (schemaSource != null)
dbf.setAttribute(JAXP_SCHEMA_SOURCE, new File(schemaSource));
}
DocumentBuilder db = dbf.newDocumentBuilder();
db.setErrorHandler(new MyErrorHandler());
domDoc = db.parse(xmlFile);
root = domDoc.getDocumentElement();
traverse(root);
out.flush();
return domDoc;
}
catch (FactoryConfigurationError e) {
throw new Exception(
"** FactoryConfigurationError\n"
+ e.getMessage());
}
catch (ParserConfigurationException e) {
throw new Exception(
"** ParserConfigurationException\n"
+ e.getMessage());
}
catch (SAXException e) {
Exception ex = e.getException();
String msg = null;
if (ex != null) {
ex.printStackTrace();
msg = ex.getMessage();
}
throw new Exception(
"** SAXException:\n" + e.getMessage());
}
catch (IOException e) {
throw new Exception(
"IOException:\n" + e.getMessage());
}
}
private void traverse (Node elem) {
try {
if (elem.hasAttributes()) {
NamedNodeMap attrs = elem.getAttributes();
int alength = attrs.getLength();
for (int i=0; i<alength; i++) {
Attr attr = (Attr) attrs.item(i);
out.println (attr.getName() + ":\t" + attr.getValue());
}
}
if (elem.hasChildNodes()) {
NodeList children = elem.getChildNodes();
int length = children.getLength();
for (int i=0; i<length; i++) {
Node n = children.item(i);
String name = n.getNodeName();
if (n.getNodeType() == ELEMENT_NODE) {
if (name.equals("Item"))
out.println("\n");
else
out.print(name + ":\t");
traverse(n); }
else
if (n.getNodeType() == TEXT_NODE) {
String txt = n.getNodeValue().trim();
if (! txt.equals("")) {
out.println(txt);
}
}
}
return;
}
}
catch (DOMException e) {
System.out.println("*** DOMException\n"
+ e.getMessage());
}
}
public Item createItem () {
return new Item();
}
public void addItem (Item item) throws Exception {
Element itemElem = null;
Element elem = null;
Text txtNode = null;
itemElem = domDoc.createElement("Item");
itemElem.setAttribute("media", item.getMedia());
root.appendChild(itemElem);
elem = domDoc.createElement("Artist");
txtNode = domDoc.createTextNode(item.getArtist ());
elem.appendChild(txtNode);
itemElem.appendChild(elem);
elem = domDoc.createElement("Title");
txtNode = domDoc.createTextNode(item.getTitle());
elem.appendChild(txtNode);
itemElem.appendChild(elem);
elem = domDoc.createElement("Year");
txtNode = domDoc.createTextNode(item.getYear());
elem.appendChild(txtNode);
itemElem.appendChild(elem);
traverse(root);
}
public void toHtml (File stylesheet) throws Exception {
System.out.println("\nXML File: " + xmlFile);
String fn = xmlFile.substring(0, xmlFile.lastIndexOf("."));
File htmlFile = new File(fn + ".html");
BufferedWriter out = new BufferedWriter(
new FileWriter(htmlFile));
System.out.println("\nStylesheet: " + stylesheet);
System.out.println("\nOutput: " + htmlFile);
XmlTransformer.transform(domDoc, stylesheet, out);
}
public void toXml ()
throws Exception {
XmlTransformer.writeXml(domDoc, xmlFile);
System.out.println("\nOutput should have been written to : "
+ xmlFile);
}
private class MyErrorHandler implements ErrorHandler {
public void warning (SAXParseException spe) throws SAXException {
System.out.println("Warning: " + getParseExceptionInfo(spe));
}
public void error (SAXParseException spe) throws SAXException {
String message = "Error: " + getParseExceptionInfo(spe);
throw new SAXException(message);
}
public void fatalError (SAXParseException spe) throws SAXException {
String message = "Fatal Error: " + getParseExceptionInfo(spe);
throw new SAXException(message);
}
private String getParseExceptionInfo (SAXParseException spe) {
String systemId = spe.getSystemId();
if (systemId == null) {
systemId = "null";
}
String info = "URI=" + systemId +
"\nLine=" + spe.getLineNumber() +
"\n" + spe.getMessage();
return info;
}
}
}