private String toString(Document doc) { UnitTestUtil.normalizeAttributes(doc.getRootElement()); normalizeDTD(doc.getDocType()); XMLOutputter out = new XMLOutputter(Format.getPrettyFormat()); CharArrayWriter caw = new CharArrayWriter(); try { out.output(doc, caw); } catch (IOException e) { e.printStackTrace(); return null; } return caw.toString(); }
/** * This method tells you the line of the XML file being parsed. For an in-memory document, it's * meaningless. The location is only valid for the current parsing lifecycle, but the document has * already been parsed. Therefore, it returns -1 for both line and column numbers. * * @param document JDOM <code>Document</code>. */ private void documentLocator(Document document) { locator = new JDOMLocator(); String publicID = null; String systemID = null; if (document != null) { DocType docType = document.getDocType(); if (docType != null) { publicID = docType.getPublicID(); systemID = docType.getSystemID(); } } locator.setPublicId(publicID); locator.setSystemId(systemID); locator.setLineNumber(-1); locator.setColumnNumber(-1); contentHandler.setDocumentLocator(locator); }
/** * This parses a DTD declaration to fire the related events towards the registered handlers. * * @param document <code>JDOM Document</code> the DocType is to process. */ private void dtdEvents(Document document) throws JDOMException { DocType docType = document.getDocType(); // Fire DTD-related events only if handlers have been registered. if ((docType != null) && ((dtdHandler != null) || (declHandler != null))) { // Build a dummy XML document that only references the DTD... String dtdDoc = new XMLOutputter().outputString(docType); try { // And parse it to fire DTD events. createDTDParser().parse(new InputSource(new StringReader(dtdDoc))); // We should never reach this point as the document is // ill-formed; it does not have any root element. } catch (SAXParseException e) { // Expected exception: There's no root element in document. } catch (SAXException e) { throw new JDOMException("DTD parsing error", e); } catch (IOException e) { throw new JDOMException("DTD parsing error", e); } } }