/* * DomTemplate.java * * @author David Hausheer * @version 1.0, 10 Apr 2002 * * This Class has been written for the XML lecture at ETH (http://www.ethz.ch/) * during SS 2002 (http://dret.net/lectures/xml-ss02/). * */ import java.io.*; import java.lang.*; import java.util.*; import javax.xml.parsers.*; import org.w3c.dom.*; import org.xml.sax.*; import org.apache.xerces.dom.*; import org.apache.xml.serialize.*; public class DomTemplate { public static void main(String args[]) { try { /* Get an instance of the document builder factory */ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); /* Validate the document */ dbf.setValidating(false); /* Get namespace support */ dbf.setNamespaceAware(true); /* Get a new document builder */ DocumentBuilder db = dbf.newDocumentBuilder(); /* Create an error handler to treat parsing errors */ db.setErrorHandler( new org.xml.sax.ErrorHandler() { // ignore fatal errors (an exception is guaranteed) public void fatalError(SAXParseException spe) throws SAXException { throw spe; } // treat validation errors as fatal public void error(SAXParseException spe) throws SAXParseException { throw spe; } // dump warnings public void warning(SAXParseException spe) throws SAXParseException { System.out.println("[Warning] " + spe.getSystemId() + ", Line " + spe.getLineNumber() + ": " + spe.getMessage()); } } ); /* Parse the document */ Document doc = db.parse(args[0]); /* This is the root element */ Element root = doc.getDocumentElement(); /* Get the in-scope namespaces for one of the elements (step 2) */ Hashtable ns = getInScopeNamespaces(root); /* Set all namespaces in each element of the document (step 3) */ setAllNamespaces(root); /* Output the new document */ XMLSerializer serializer = new XMLSerializer(new OutputFormat(doc,"ISO-8859-1",true)); serializer.setOutputByteStream(System.out); serializer.serialize(doc); } catch (SAXParseException spe) { // Error generated by the parser System.out.println("[Error] " + spe.getSystemId() + ", Line " + spe.getLineNumber() + ": " + spe.getMessage()); // Use the contained exception, if any Exception x = spe; if (spe.getException() != null) x = spe.getException(); x.printStackTrace(); } catch (SAXException sxe) { // Error generated during parsing Exception x = sxe; if (sxe.getException() != null) x = sxe.getException(); x.printStackTrace(); } catch (ParserConfigurationException pce) { // Parser with specified options can't be built pce.printStackTrace(); } catch (IOException ioe) { // I/O error ioe.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } /* This is the method you have to implement in step 2 */ public static Hashtable getInScopeNamespaces(Element element) { return null; } /* This is the method you have to implement in step 3 */ public static void setAllNamespaces(Element element) { } }