[Jsxe-cvs] SF.net SVN: jsxe: [1239] branches/jsxe2/src/net/sourceforge/jsxe/dom
Status: Inactive
Brought to you by:
ian_lewis
From: <ian...@us...> - 2006-09-06 19:27:02
|
Revision: 1239 http://svn.sourceforge.net/jsxe/?rev=1239&view=rev Author: ian_lewis Date: 2006-09-06 12:26:53 -0700 (Wed, 06 Sep 2006) Log Message: ----------- Moved serialization classes to net.sourceforge.jsxe.dom.ls Added Paths: ----------- branches/jsxe2/src/net/sourceforge/jsxe/dom/ls/DOMSerializer.java branches/jsxe2/src/net/sourceforge/jsxe/dom/ls/DOMSerializerConfiguration.java branches/jsxe2/src/net/sourceforge/jsxe/dom/ls/DOMSerializerException.java Removed Paths: ------------- branches/jsxe2/src/net/sourceforge/jsxe/dom/DOMSerializer.java branches/jsxe2/src/net/sourceforge/jsxe/dom/DOMSerializerConfiguration.java branches/jsxe2/src/net/sourceforge/jsxe/dom/DOMSerializerException.java Deleted: branches/jsxe2/src/net/sourceforge/jsxe/dom/DOMSerializer.java =================================================================== --- branches/jsxe2/src/net/sourceforge/jsxe/dom/DOMSerializer.java 2006-09-06 19:20:54 UTC (rev 1238) +++ branches/jsxe2/src/net/sourceforge/jsxe/dom/DOMSerializer.java 2006-09-06 19:26:53 UTC (rev 1239) @@ -1,825 +0,0 @@ -/* -DOMSerializer.java -:tabSize=4:indentSize=4:noTabs=true: -:folding=explicit:collapseFolds=1: - -This attempts to conform to the DOM3 implementation in Xerces. It tries to -conform to DOM3 as of Xerces 2.6.0. I'm not one to stay on the bleeding edge -but it is as close to a standard interface for load & save as you can get -and I didn't want to work around the fact that current serializers aren't -very good. - -Copyright (C) 2002 Ian Lewis (Ian...@me...) - -This program is free software; you can redistribute it and/or -modify it under the terms of the GNU General Public License -as published by the Free Software Foundation; either version 2 -of the License, or (at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -Optionally, you may find a copy of the GNU General Public License -from http://www.fsf.org/copyleft/gpl.txt -*/ - -package net.sourceforge.jsxe.dom; - -//{{{ imports -/* -All classes are listed explicitly so -it is easy to see which package it -belongs to. -*/ - -//{{{ DOM classes -import org.w3c.dom.Attr; -import org.w3c.dom.Document; -import org.w3c.dom.DocumentType; -import org.w3c.dom.DOMException; -import org.w3c.dom.NamedNodeMap; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; -import org.w3c.dom.Notation; -import org.w3c.dom.ls.LSSerializer; -import org.w3c.dom.ls.LSSerializerFilter; -import org.w3c.dom.ls.LSOutput; -import org.w3c.dom.DOMConfiguration; -import org.w3c.dom.DOMLocator; -import org.w3c.dom.DOMError; -import org.w3c.dom.DOMErrorHandler; -//}}} - -//{{{ Java base classes -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.io.StringWriter; -import java.io.UnsupportedEncodingException; -import java.io.Writer; -import java.net.URL; -import java.net.URLConnection; -import java.net.MalformedURLException; -import java.util.ArrayList; -import java.util.Hashtable; -import java.util.Vector; -//}}} - -//}}} - -/** - * An implementation of the DOM3 LSSerializer interface. This class supports - * everything that is supported by the DOMSerializerConfiguration class. Clients - * can check if a feature is supported by calling canSetParameter() on the - * appropriate DOMSerializerConfiguration object. - * - * @author <a href="mailto:IanLewis at member dot fsf dot org">Ian Lewis</a> - * @version $Id$ - */ -public class DOMSerializer implements LSSerializer { - - //{{{ DOMSerializer constructor - /** - * Creates a default DOMSerializer using the default options. - */ - public DOMSerializer() { - config = new DOMSerializerConfiguration(); - m_newLine = System.getProperty("line.separator"); - }//}}} - - //{{{ DOMSerializer constructor - /** - * Creates a DOMSerializer that uses the configuration specified. - * @param config The configuration to be used by this DOMSerializer object - */ - public DOMSerializer(DOMSerializerConfiguration config) { - this.config = config; - m_newLine = System.getProperty("line.separator"); - }//}}} - - //{{{ Implemented LSSerializer methods - - //{{{ getConfig() - - public DOMConfiguration getDomConfig() { - return config; - }//}}} - - //{{{ getFilter() - - public LSSerializerFilter getFilter() { - return m_filter; - }//}}} - - //{{{ getNewLine() - - public String getNewLine() { - return m_newLine; - }//}}} - - //{{{ setFilter() - - public void setFilter(LSSerializerFilter filter) { - m_filter=filter; - }//}}} - - //{{{ setNewLine() - - public void setNewLine(String newLine) { - m_newLine=newLine; - }//}}} - - //{{{ write() - - public boolean write(Node nodeArg, LSOutput destination) { - if (m_filter == null || m_filter.acceptNode(nodeArg) == 1) { - - //{{{ try to get the Writer object for our destination - Writer writer = destination.getCharacterStream(); - String encoding = null; - - if (writer == null) { - //no character stream specified, try the byte stream. - OutputStream out = destination.getByteStream(); - if (out != null) { - - try { - writer = new OutputStreamWriter(out, destination.getEncoding()); - encoding = destination.getEncoding(); - } catch (UnsupportedEncodingException uee) { - DefaultDOMLocator loc = new DefaultDOMLocator(nodeArg, 1, 1, 0, null); - try { - throwError(loc, "unsupported-encoding", DOMError.SEVERITY_FATAL_ERROR, uee); - } catch (DOMSerializerException e) {/*we know this will happen*/} - //This is a fatal error, quit. - return false; - } - } else { - //no char stream or byte stream, try the uri - String id = destination.getSystemId(); - if (id != null) { - - try { - //We use URL since outputing to any other type of URI - //is not possible. - URL uri = new URL(id); - URLConnection con = uri.openConnection(); - - try { - //We want to try to output to the URI - con.setDoOutput(true); - //I don't see a problem with using caches - //do you? - con.setUseCaches(true); - } catch (IllegalStateException ise) { - //we are guaranteed to not be connected - } - - con.connect(); - - writer = new OutputStreamWriter(con.getOutputStream(), destination.getEncoding()); - - } catch (MalformedURLException mue) { - DefaultDOMLocator loc = new DefaultDOMLocator(nodeArg, 1, 1, 0, null); - try { - throwError(loc, "bad-uri", DOMError.SEVERITY_FATAL_ERROR, mue); - } catch (DOMSerializerException e) {/*we know this will happen*/} - //this is a fatal error - return false; - } catch (IOException ioe) { - DefaultDOMLocator loc = new DefaultDOMLocator(nodeArg, 1, 1, 0, null); - try { - throwError(loc, "io-error", DOMError.SEVERITY_FATAL_ERROR, ioe); - } catch (DOMSerializerException e) {/*we know this will happen*/} - //this is a fatal error - return false; - } - - } else { - DefaultDOMLocator loc = new DefaultDOMLocator(nodeArg, 1, 1, 0, null); - try { - throwError(loc, "no-output-specified", DOMError.SEVERITY_FATAL_ERROR, null); - } catch (DOMSerializerException e) {/*we know this will happen*/} - //this is a fatal error - return false; - } - } - }//}}} - - BufferedWriter bufWriter = new BufferedWriter(writer, IO_BUFFER_SIZE); - - try { - serializeNode(bufWriter, nodeArg, encoding); - bufWriter.close(); - return true; - } catch (IOException ioe) { - Object rawHandler = config.getParameter(DOMSerializerConfiguration.ERROR_HANDLER); - if (rawHandler != null) { - DOMErrorHandler handler = (DOMErrorHandler)rawHandler; - DefaultDOMLocator loc = new DefaultDOMLocator(nodeArg, 1, 1, 0, null); - DOMSerializerError error = new DOMSerializerError(loc, ioe, DOMError.SEVERITY_FATAL_ERROR, "io-error"); - handler.handleError(error); - } - } catch (DOMSerializerException dse) { - Object rawHandler = config.getParameter(DOMSerializerConfiguration.ERROR_HANDLER); - if (rawHandler != null) { - DOMErrorHandler handler = (DOMErrorHandler)rawHandler; - DOMError error = dse.getError(); - handler.handleError(error); - } - //This is a fatal error, quit. - } - } - return false; - }//}}} - - //{{{ writeToString() - - public String writeToString(Node nodeArg) throws DOMException { - StringWriter writer = new StringWriter(); - try { - serializeNode(writer, nodeArg); - //flush the output-stream. Without this - //files are sometimes not written at all. - writer.flush(); - } catch (DOMSerializerException dse) { - throw new DOMException(DOMException.INVALID_STATE_ERR, dse.getMessage()); - } - return writer.toString(); - }//}}} - - //{{{ writeToURI() - - public boolean writeToURI(Node nodeArg, java.lang.String uri) { - return write(nodeArg, new DOMOutput(uri, "UTF-8")); - }//}}} - - //}}} - - //{{{ Private static members - private static final int IO_BUFFER_SIZE = 32768; - //}}} - - //{{{ Private members - - //{{{ DOMSerializerError class - - private static class DOMSerializerError implements DOMError { - - //{{{ DOMSerializerError constructor - - public DOMSerializerError(DOMLocator locator, Exception e, short s, String type) { - m_exception = e; - m_location = locator; - m_severity = s; - m_type = type; - }//}}} - - //{{{ getLocation() - - public DOMLocator getLocation() { - return m_location; - }//}}} - - //{{{ getMessage() - - public String getMessage() { - return m_exception.getMessage(); - }//}}} - - //{{{ getRelatedData() - - public Object getRelatedData() { - return m_location.getRelatedNode(); - }//}}} - - //{{{ getRelatedException() - - public Object getRelatedException() { - return m_exception; - }//}}} - - //{{{ getSeverity() - - public short getSeverity() { - return m_severity; - }//}}} - - //{{{ getType() - - public String getType() { - return m_type; - }//}}} - - //{{{ Private members - - private Exception m_exception; - private DOMLocator m_location; - private short m_severity; - private String m_type; - //}}} - - }//}}} - - //{{{ serializeNode() - - private void serializeNode(Writer writer, Node node) throws DOMSerializerException { - serializeNode(writer, node, null); - }//}}} - - //{{{ serializeNode() - /** - * Serializes the node to the writer specified - */ - private void serializeNode(Writer writer, Node node, String encoding) throws DOMSerializerException { - rSerializeNode(writer, node, encoding, "", 1, 1, 0); - }//}}} - - //{{{ rSerializeNode() - /** - * Designed to be called recursively and maintain the state of the - * serialization. - */ - private void rSerializeNode(Writer writer, Node node, String encoding, String currentIndent, int line, int column, int offset) throws DOMSerializerException { - - boolean formatting = config.getFeature(DOMSerializerConfiguration.FORMAT_XML); - // boolean whitespace = config.getFeature(DOMSerializerConfiguration.WS_IN_ELEMENT_CONTENT); - - //This is used many times below as a temporary variable. - String str = ""; - - if (m_filter == null || m_filter.acceptNode(node) == 1) { - switch (node.getNodeType()) { - case Node.DOCUMENT_NODE://{{{ - if (config.getFeature(DOMSerializerConfiguration.XML_DECLARATION)) { - String header = "<?xml version=\"1.0\""; - String realEncoding = (String)config.getParameter(DOMSerializerConfiguration.XML_ENCODING); - if (realEncoding == null) { - realEncoding = encoding; - } - if (realEncoding != null) - header += " encoding=\""+realEncoding+"\""; - header +="?>"; - doWrite(writer, header, node, line, column, offset); - offset += header.length(); - column += header.length(); - - - //if not formatting write newLine here. - if (!formatting) { - column = 0; - line += 1; - doWrite(writer, m_newLine, node, line, column, offset); - offset += m_newLine.length(); - } - } - - NodeList nodes = node.getChildNodes(); - if (nodes != null) { - for (int i=0; i<nodes.getLength(); i++) { - rSerializeNode(writer, nodes.item(i), encoding, currentIndent, line, column, offset); - } - } - - break;//}}} - case Node.ELEMENT_NODE://{{{ - String nodeName = node.getLocalName(); - String nodePrefix = node.getPrefix(); - if (nodeName == null) { - nodeName = node.getNodeName(); - } - - if (formatting) { - //set to zero here for error handling (if doWrite throws exception). - column = 0; - str = m_newLine + currentIndent; - doWrite(writer, str, node, line, column, offset); - column += currentIndent.length(); - offset += str.length(); - } - - if (config.getFeature(DOMSerializerConfiguration.NAMESPACES) && nodePrefix != null) { - str = "<" + nodePrefix + ":" + nodeName; - } else { - str = "<" + nodeName; - } - - doWrite(writer, str, node, line, column, offset); - column += str.length(); - offset += str.length(); - - NamedNodeMap attr = node.getAttributes(); - for (int i=0; i<attr.getLength(); i++) { - Attr currentAttr = (Attr)attr.item(i); - boolean writeAttr = false; - - /* - if we discard default content check if the attribute - was specified in the original document. - */ - if (config.getFeature(DOMSerializerConfiguration.DISCARD_DEFAULT_CONTENT)) { - if (currentAttr.getSpecified()) { - writeAttr = true; - } - } else { - writeAttr = true; - } - - if (writeAttr) { - str = " " + currentAttr.getNodeName() + "=\"" + normalizeCharacters(currentAttr.getNodeValue()) + "\""; - doWrite(writer, str, node, line, column, offset); - column += str.length(); - offset += str.length(); - } - } - - NodeList children = node.getChildNodes(); - if (children != null) { - - //check if element is empty or has - //only whitespace-only nodes - boolean elementEmpty = false; - if (children.getLength() <= 0) { - elementEmpty = true; - } else { - if (!config.getFeature(DOMSerializerConfiguration.WS_IN_ELEMENT_CONTENT)) { - boolean hasWSOnlyElements = true; - for(int i=0; i<children.getLength();i++) { - hasWSOnlyElements = hasWSOnlyElements && - children.item(i).getNodeType()==Node.TEXT_NODE && - children.item(i).getNodeValue().trim().equals(""); - } - elementEmpty = formatting && hasWSOnlyElements; - } - } - if (!elementEmpty) { - - str = ">"; - doWrite(writer, str, node, line, column, offset); - column += str.length(); - offset += str.length(); - - String indentUnit = ""; - - if (formatting) { - if (config.getFeature(DOMSerializerConfiguration.SOFT_TABS)) { - //get the indent size and use it when serializing the children nodes. - Integer indentSize = (Integer)config.getParameter("indent"); - if (indentSize != null) { - int size = indentSize.intValue(); - StringBuffer buf = new StringBuffer(); - for (int i=0; i < size; i++) { - buf.append(" "); - } - indentUnit = buf.toString(); - } - } else { - indentUnit = "\t"; - } - } - - - for(int i=0; i<children.getLength();i++) { - rSerializeNode(writer, children.item(i), encoding, currentIndent + indentUnit, line, column, offset); - } - - //don't add a new line if there is only text node children - if (formatting) { - - boolean allText = true; - for(int i=0; i<children.getLength();i++) { - if (!(children.item(i).getNodeType()==Node.TEXT_NODE) && - !(children.item(i).getNodeType()==Node.CDATA_SECTION_NODE)) - { - allText = false; - break; - } - } - - if (!allText) { - //set to zero here for error handling (if doWrite throws exception). - column = 0; - str = m_newLine + currentIndent; - doWrite(writer, str, node, line, column, offset); - column += currentIndent.length(); - offset += str.length(); - } - } - if (config.getFeature(DOMSerializerConfiguration.NAMESPACES) && nodePrefix != null) { - str = "</" + nodePrefix + ":" +nodeName + ">"; - } else { - str = "</" + nodeName + ">"; - } - doWrite(writer, str, node, line, column, offset); - column += str.length(); - offset += str.length(); - - } else { - str = "/>"; - doWrite(writer, str, node, line, column, offset); - column += str.length(); - offset += str.length(); - } - } - break;//}}} - case Node.TEXT_NODE://{{{ - String text = node.getNodeValue(); - //formatting implies no whitespace - //but to be explicit... - // if (!whitespace || formatting) { - // text = text.trim(); - // } - if (!text.equals("")) { - if (formatting) { - if (text.trim().equals("")) { - //ignore this whitespace only text if formatting - return; - } - - /* - don't format text nodes - if (node.getNextSibling() != null || node.getPreviousSibling() != null) { - line++; - column=0; - doWrite(writer, m_newLine, node, line, column, offset); - offset += m_newLine.length(); - } - */ - } - - //TODO: This is a dumb quick fix and should probably be changed. - for (int i=0; i<text.length();i++) { - //this must be first or it picks up the other - //entities. - str = text.substring(i, i+1); - if (str.equals("&")) { - str = "&"; - } - if (str.equals(">")) { - str = ">"; - } - if (str.equals("<")) { - str = "<"; - } - if (str.equals("\'")) { - str = "'"; - } - if (str.equals("\"")) { - str = """; - } - if (str.equals(m_newLine)) { - line++; - column=0; - doWrite(writer, m_newLine, node, line, column, offset); - offset += m_newLine.length(); - } else { - doWrite(writer, str, node, line, column, offset); - column += str.length(); - offset += str.length(); - } - } - } - break;//}}} - case Node.CDATA_SECTION_NODE://{{{ - if (config.getFeature(DOMSerializerConfiguration.CDATA_SECTIONS)) { - //shouldn't add newlines for - // if (formatting) { - // //set to zero here for error handling (if doWrite throws exception) - // column = 0; - // str = m_newLine + currentIndent; - // doWrite(writer, str, node, line, column, offset); - // column += currentIndent.length(); - // offset += str.length(); - // } - str = "<![CDATA["; - doWrite(writer, str, node, line, column, offset); - column += str.length(); - offset += str.length(); - - String cdata = node.getNodeValue(); - for (int i=0; i<cdata.length(); i++) { - - str = cdata.substring(i, i+1); - if (str.equals("]") && i+3 < cdata.length() && cdata.substring(i, i+3).equals("]]>")) { - //split the cdata - DefaultDOMLocator loc = new DefaultDOMLocator(node, line, column, offset, null); - if (config.getFeature(DOMSerializerConfiguration.SPLIT_CDATA)) { - i+=2; - str = "]]]]>"; - /* - if (formatting) { - str += (m_newLine + currentIndent); - column = currentIndent.length(); - offset += (m_newLine.length() + currentIndent.length()); - } - */ - str += "<![CDATA[>"; - throwError(loc, "cdata-sections-splitted", DOMError.SEVERITY_WARNING, null); - } else { - throwError(loc, "invalid-data-in-cdata-section", DOMError.SEVERITY_FATAL_ERROR, null); - } - } - if (str.equals(m_newLine)) { - line++; - column=0; - doWrite(writer, m_newLine, node, line, column, offset); - offset += m_newLine.length(); - } else { - doWrite(writer, str, node, line, column, offset); - column += str.length(); - offset += str.length(); - } - - } - - str = "]]>"; - doWrite(writer, str, node, line, column, offset); - column += str.length(); - offset += str.length(); - } else { - //transform to text node. - Node textNode = node.getOwnerDocument().createTextNode(node.getNodeValue()); - rSerializeNode(writer, textNode, encoding, currentIndent, line, column, offset); - } - break;//}}} - case Node.COMMENT_NODE://{{{ - if (config.getFeature("comments")) { - if (formatting) { - //set to zero here for error handling (if doWrite throws exception) - column = 0; - str = m_newLine + currentIndent; - doWrite(writer, str, node, line, column, offset); - column += currentIndent.length(); - offset += str.length(); - } - str = "<!--"+node.getNodeValue()+"-->"; - doWrite(writer, str, node, line, column, offset); - column += str.length(); - offset += str.length(); - } - break;//}}} - case Node.PROCESSING_INSTRUCTION_NODE://{{{ - - if (formatting) { - //set to zero here for error handling (if doWrite throws exception) - column = 0; - str = m_newLine + currentIndent; - doWrite(writer, str, node, line, column, offset); - column += currentIndent.length(); - offset += str.length(); - } - - str = "<?" + node.getNodeName() + " " + node.getNodeValue() + "?>"; - doWrite(writer, str, node, line, column, offset); - column += str.length(); - offset += str.length(); - - break;//}}} - case Node.ENTITY_REFERENCE_NODE://{{{ - str = "&" + node.getNodeName() + ";"; - doWrite(writer, str, node, line, column, offset); - column += str.length(); - offset += str.length(); - break;//}}} - case Node.DOCUMENT_TYPE_NODE://{{{ - DocumentType docType = (DocumentType)node; - - if (formatting) { - //set to zero here for error handling (if doWrite throws exception). - column = 0; - str = m_newLine + currentIndent; - doWrite(writer, str, node, line, column, offset); - column += currentIndent.length(); - offset += str.length(); - } - - str = "<!DOCTYPE " + docType.getName(); - doWrite(writer, str, node, line, column, offset); - column += str.length(); - offset += str.length(); - if (docType.getPublicId() != null) { - str = " PUBLIC \"" + docType.getPublicId() + "\" "; - doWrite(writer, str, node, line, column, offset); - column += str.length(); - offset += str.length(); - } - if (docType.getSystemId() != null) { - if (docType.getPublicId() == null) { - str = " SYSTEM "; - } else { - str = ""; - } - str += "\"" + docType.getSystemId() + "\""; - doWrite(writer, str, node, line, column, offset); - column += str.length(); - offset += str.length(); - } - - String internalSubset = docType.getInternalSubset(); - if (internalSubset != null && !internalSubset.equals("")) { - str = " [ "+internalSubset+" ]"; - doWrite(writer, str, node, line, column, offset); - column += str.length(); - offset += str.length(); - } - - str = ">"; - doWrite(writer, str, node, line, column, offset); - column += str.length(); - offset += str.length(); - - //need to add a newline so that the next item is on a new line - //since text nodes are not included outside of the root element - if (!formatting) { - column = 0; - str = m_newLine + currentIndent; - doWrite(writer, str, node, line, column, offset); - column += currentIndent.length(); - offset += str.length(); - } - - break;//}}} - } - } - }//}}} - - //{{{ doWrite() - /** - * Performs an actual write and implements error handling. - */ - private void doWrite(Writer writer, String str, Node wnode, int line, int column, int offset) throws DOMSerializerException { - try { - writer.write(str, 0, str.length()); - } catch (IOException ioe) { - - DefaultDOMLocator loc = new DefaultDOMLocator(wnode, line, column, offset, null); - - throwError(loc, "io-error", DOMError.SEVERITY_FATAL_ERROR, ioe); - } - }//}}} - - //{{{ throwError() - /** - * Throws an error, notifying the ErrorHandler object if necessary. - * @return the value returned by the error handler or false if the severity was SEVERITY_FATAL_ERROR - */ - private void throwError(DOMLocator loc, String type, short severity, Exception e) throws DOMSerializerException { - Object rawHandler = config.getParameter(DOMSerializerConfiguration.ERROR_HANDLER); - boolean handled = false; - if (severity == DOMError.SEVERITY_WARNING) { - handled = true; - } - DOMSerializerError error = new DOMSerializerError(loc, e, severity, type); - if (rawHandler != null) { - DOMErrorHandler handler = (DOMErrorHandler)rawHandler; - handled = handler.handleError(error); - } - - if ((severity == DOMError.SEVERITY_ERROR && !handled) || severity == DOMError.SEVERITY_FATAL_ERROR) { - throw new DOMSerializerException(error); - } - }//}}} - - //{{{ normalizeCharacters() - - private String normalizeCharacters(String text) { - StringBuffer newText = new StringBuffer(); - //This is a dumb quick fix and should be changed. - for (int i=0; i<text.length();i++) { - //this must be first or it picks up the other - //entities. - String str = text.substring(i, i+1); - if (str.equals("&")) { - str = "&"; - } - if (str.equals(">")) { - str = ">"; - } - if (str.equals("<")) { - str = "<"; - } - if (str.equals("\'")) { - str = "'"; - } - if (str.equals("\"")) { - str = """; - } - newText.append(str); - } - return newText.toString(); - }//}}} - - private DOMSerializerConfiguration config; - private LSSerializerFilter m_filter; - private String m_newLine; - - //}}} -} Deleted: branches/jsxe2/src/net/sourceforge/jsxe/dom/DOMSerializerConfiguration.java =================================================================== --- branches/jsxe2/src/net/sourceforge/jsxe/dom/DOMSerializerConfiguration.java 2006-09-06 19:20:54 UTC (rev 1238) +++ branches/jsxe2/src/net/sourceforge/jsxe/dom/DOMSerializerConfiguration.java 2006-09-06 19:26:53 UTC (rev 1239) @@ -1,478 +0,0 @@ -/* -DOMSerializerConfiguration.java -:tabSize=4:indentSize=4:noTabs=true: -:folding=explicit:collapseFolds=1: - -This attempts to conform to the DOM3 implementation in Xerces. It conforms -to DOM3 as of Xerces 2.3.0. I'm not one to stay on the bleeding edge but -there is as close to a standard interface for load & save as you can get and I -didn't want to work around the fact that current serializers aren't very good. - -Copyright (C) 2002 Ian Lewis (Ian...@me...) - -This program is free software; you can redistribute it and/or -modify it under the terms of the GNU General Public License -as published by the Free Software Foundation; either version 2 -of the License, or (at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -Optionally, you may find a copy of the GNU General Public License -from http://www.fsf.org/copyleft/gpl.txt -*/ - -package net.sourceforge.jsxe.dom; - -//{{{ imports - -//{{{ DOM classes -import org.w3c.dom.DOMException; -import org.w3c.dom.DOMConfiguration; -import org.w3c.dom.DOMError; -import org.w3c.dom.DOMErrorHandler; -import org.w3c.dom.DOMLocator; -import org.w3c.dom.DOMStringList; -//}}} - -//{{{ Java classes -import java.util.ArrayList; -import java.util.Hashtable; -import java.util.Iterator; -//}}} - -import net.sourceforge.jsxe.util.Log; - -//}}} - -/** - * <p>DOMSerializerConfiguration is the default implementation of the DOMConfiguration - * interface to be used with the DOMSerializer class.</p> - * - * <p>Currently, this class only supports the required options with few exceptions. - * The <code>"format-pretty-print"</code> option is supported. - * A <code>"soft-tabs"</code> option is supported which specifies whether - * to emulate tabs with spaces. - * An <code>"indent"</code> option is supported to specify the indent/tab - * size when the <code>"soft-tabs"</code> feature is true. This has no effect - * if <code>"soft-tabs"</code> is false.</p> - * - * @author <a href="mailto:IanLewis at member dot fsf dot org">Ian Lewis</a> - * @version $Id$ - * @see DOMSerializer - */ -public class DOMSerializerConfiguration implements DOMConfiguration { - - //{{{ DOMConfiguration defined parameters - public static final String CANONICAL_FORM = "canonical-form"; - public static final String CDATA_SECTIONS = "cdata-sections"; - public static final String CHAR_NORMALIZATION = "check-character-normalization"; - public static final String COMMENTS = "comments"; - public static final String DATATYPE_NORMALIZATION = "datatype-normalization"; - public static final String ENTITIES = "entities"; - public static final String ERROR_HANDLER = "error-handler"; - public static final String INFOSET = "infoset"; - public static final String NAMESPACES = "namespaces"; - public static final String NAMESPACE_DECLARATIONS = "namespace-declarations"; - public static final String NORMALIZE_CHARS = "normalize-characters"; - public static final String SPLIT_CDATA = "split-cdata-sections"; - public static final String VALIDATE_XML = "validate"; - public static final String VALIDATE_IF_SCHEMA = "validate-if-schema"; - public static final String WELL_FORMED = "well-formed"; - public static final String WS_IN_ELEMENT_CONTENT = "element-content-whitespace"; - //}}} - - //{{{ LSSerializer defined parameters - public static final String DISCARD_DEFAULT_CONTENT = "discard-default-content"; - public static final String FORMAT_XML = "format-pretty-print"; - public static final String IGNORE_UNKNOWN_CHAR_DENORM = "ignore-unknown-character-denormalizations"; - public static final String XML_DECLARATION = "xml-declaration"; - //}}} - - //{{{ Additional parameters supported by DOMSerializerConfiguration - /** - * Use spaces instead of tabs if this is true. - */ - public static final String SOFT_TABS = "soft-tabs"; - /** - * The number of spaces to use as an tab when SOFT_TABS is true. - */ - public static final String INDENT = "indent"; - /** - * The encoding to use in the XML declaration. - */ - public static final String XML_ENCODING = "encoding"; - //}}} - - //{{{ DOMSerializerConfiguration constructor - - public DOMSerializerConfiguration() { - - //set the default boolean parameters for a DOMConfiguration - setFeature(CANONICAL_FORM, false); - setFeature(CDATA_SECTIONS, true); - setFeature(CHAR_NORMALIZATION, false); - setFeature(COMMENTS, true); - setFeature(DATATYPE_NORMALIZATION, false); - setFeature(ENTITIES, true); - //infoset is not present because it is determined - //by checking the values of other features. - setFeature(NAMESPACES, true); - setFeature(NAMESPACE_DECLARATIONS, true); - setFeature(NORMALIZE_CHARS, true); - setFeature(SPLIT_CDATA, true); - setFeature(VALIDATE_XML, false); - setFeature(VALIDATE_IF_SCHEMA, false); - setFeature(WELL_FORMED, true); - setFeature(WS_IN_ELEMENT_CONTENT, true); - - //LSSeraializer features - setFeature(DISCARD_DEFAULT_CONTENT, true); - setFeature(FORMAT_XML, false); - setFeature(IGNORE_UNKNOWN_CHAR_DENORM, true); - setFeature(XML_DECLARATION, true); - - //DOMSerializer parameters - setFeature(SOFT_TABS, false); - setParameter(INDENT, new Integer(4)); - setParameter(XML_ENCODING, null); - }//}}} - - //{{{ DOMSerializerConfiguration constructor - - public DOMSerializerConfiguration(DOMConfiguration config) throws DOMException { - this(); - Iterator iterator = m_supportedParameters.iterator(); - while (iterator.hasNext()) { - String param = iterator.next().toString(); - setParameter(param, config.getParameter(param)); - } - }//}}} - - //{{{ Implemented DOMConfiguration methods - - //{{{ canSetParameter() - - public boolean canSetParameter(String name, Object value) { - - if (value == null) { - return (m_supportedParameters.indexOf(name) != -1); - } - - if (value instanceof Boolean) { - boolean booleanValue = ((Boolean)value).booleanValue(); - - //couldn't think of a slicker way to do this - //that was worth the time to implement - //and extra processing. - if (name.equals(CANONICAL_FORM)) { - return !booleanValue; - } - if (name.equals(CDATA_SECTIONS)) { - return true; - } - if (name.equals(CHAR_NORMALIZATION)) { - return !booleanValue; - } - if (name.equals(COMMENTS)) { - return true; - } - if (name.equals(DATATYPE_NORMALIZATION)) { - return true; - } - if (name.equals(ENTITIES)) { - return true; - } - if (name.equals(WELL_FORMED)) { - return true; - } - if (name.equals(INFOSET)) { - return true; - } - if (name.equals(NAMESPACES)) { - return true; - } - if (name.equals(NAMESPACE_DECLARATIONS)) { - return true; - } - if (name.equals(NORMALIZE_CHARS)) { - return true; - } - if (name.equals(SPLIT_CDATA)) { - return true; - } - if (name.equals(VALIDATE_XML)) { - return !booleanValue; - } - if (name.equals(VALIDATE_IF_SCHEMA)) { - return !booleanValue; - } - if (name.equals(WS_IN_ELEMENT_CONTENT)) { - return true; - } - - if (name.equals(DISCARD_DEFAULT_CONTENT)) { - return true; - } - if (name.equals(FORMAT_XML)) { - return true; - } - if (name.equals(IGNORE_UNKNOWN_CHAR_DENORM)) { - return booleanValue; - } - if (name.equals(XML_DECLARATION)) { - return true; - } - if (name.equals(SOFT_TABS)) { - return true; - } - - return false; - } else { - if (name.equals(ERROR_HANDLER)) { - if (value instanceof DOMErrorHandler) { - return true; - } - } - if (name.equals(INDENT)) { - if (value instanceof Integer) { - return true; - } - } - if (name.equals(XML_ENCODING)) { - if (value instanceof String) { - return true; - } - } - } - return false; - }//}}} - - //{{{ getParameter() - - public Object getParameter(String name) throws DOMException { - - if (m_supportedParameters.indexOf(name) != -1) { - - if (name.equals("infoset")) { - boolean namespaceDeclarations = getFeature(NAMESPACE_DECLARATIONS); - boolean validateIfSchema = getFeature(VALIDATE_IF_SCHEMA); - boolean entities = getFeature(ENTITIES); - boolean datatypeNormalization = getFeature(DATATYPE_NORMALIZATION); - boolean cdataSections = getFeature(CDATA_SECTIONS); - - boolean whitespace = getFeature(WS_IN_ELEMENT_CONTENT); - boolean comments = getFeature(COMMENTS); - boolean namespaces = getFeature(NAMESPACES); - - return (Boolean.valueOf(!namespaceDeclarations && - !validateIfSchema && - !entities && - !datatypeNormalization && - !cdataSections && - whitespace && - comments && - namespaces)); - } else { - return m_parameters.get(name); - } - - } else { - - throw new DOMException(DOMException.NOT_FOUND_ERR ,"NOT_FOUND_ERR: Parameter "+name+" not recognized"); - - } - }//}}} - - //{{{ getParameterNames() - - public DOMStringList getParameterNames() { - return new DOMStringListImpl(m_supportedParameters); - }//}}} - - //{{{ setParameter() - - public void setParameter(String name, Object value) throws DOMException { - - if (value instanceof String && - (value.toString().equalsIgnoreCase("true") || - value.toString().equalsIgnoreCase("false"))) - { - Log.log(Log.WARNING,this, "Possibly setting XML serializer config boolean feature "+name+" with string value"); - } - - if (m_supportedParameters.indexOf(name) != -1) { - if ( value != null ) { - if (canSetParameter(name, value)) { - /* - if the parameter is infoset - then force the other parameters to - values that the infoset option - requires. - */ - if (name.equals(INFOSET)) { - setFeature(NAMESPACE_DECLARATIONS,false); - setFeature(VALIDATE_IF_SCHEMA, false); - setFeature(ENTITIES, false); - setFeature(DATATYPE_NORMALIZATION,false); - setFeature(CDATA_SECTIONS, false); - - setFeature(WS_IN_ELEMENT_CONTENT, true); - setFeature(COMMENTS, true); - setFeature(NAMESPACES, true); - return; - } - if (name.equals(FORMAT_XML) && ((Boolean)value).booleanValue()) { - /* - The element-content-whitespace parameter is ignored - when serializing since the parameter only makes sense - when the document is validated by DTD or Schema that - specifies that an element MUST have only child elements - (element-content). - - Also if the DOM validates this info when being edited - then the serializer could never write out whitespace - in element content without invalidating the document. - See - http://xml.apache.org/xerces2-j/javadocs/dom3-api/index.html - Section 2.10 - */ - // setFeature(WS_IN_ELEMENT_CONTENT, false); - setFeature(CANONICAL_FORM, false); - } - // if (name.equals(WS_IN_ELEMENT_CONTENT) && ((Boolean)value).booleanValue()) { - // setFeature(FORMAT_XML, false); - // } - - m_parameters.put(name, value); - - } else { - throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "Parameter "+name+" and value "+value.toString()+" not supported."); - } - } else { - m_parameters.remove(name); - } - } else { - throw new DOMException(DOMException.NOT_FOUND_ERR, "Parameter "+name+" is not recognized."); - } - }//}}} - - //}}} - - //{{{ getFeature() - - /** - * <p>A convenience method to retrieve the value that a boolean - * parameter (feature) is set to.</p> - * @param name The name of the feature to get the value of - * @return The current setting for the given feature - */ - public boolean getFeature(String name) throws DOMException { - Object parameter = getParameter(name); - - if (name.equals("error-handler") || name.equals("indent") || !(parameter instanceof Boolean)) { - throw new DOMException(DOMException.NOT_FOUND_ERR, "NOT_FOUND_ERR: "+name+" is not a feature."); - } - return ((Boolean)parameter).booleanValue(); - - }//}}} - - //{{{ setFeature() - - /** - * <p>A convenience method to set the value of a boolean parameter (feature)</p> - * @param name The feature to set the value of - * @param value The boolean value to set to the feature - */ - public void setFeature(String name, boolean value) throws DOMException { - setParameter(name, Boolean.valueOf(value)); - }//}}} - - //{{{ Private static members - - private static ArrayList m_supportedParameters = null; - - static { - //create a vector of the supported parameters - m_supportedParameters = new ArrayList(22); - - //DOMConfiguration defined parameters - m_supportedParameters.add(CANONICAL_FORM); - m_supportedParameters.add(CDATA_SECTIONS); - m_supportedParameters.add(CHAR_NORMALIZATION); - m_supportedParameters.add(COMMENTS); - m_supportedParameters.add(DATATYPE_NORMALIZATION); - m_supportedParameters.add(ENTITIES); - m_supportedParameters.add(ERROR_HANDLER); - m_supportedParameters.add(INFOSET); - m_supportedParameters.add(NAMESPACES); - m_supportedParameters.add(NAMESPACE_DECLARATIONS); - m_supportedParameters.add(NORMALIZE_CHARS); - m_supportedParameters.add(SPLIT_CDATA); - m_supportedParameters.add(VALIDATE_XML); - m_supportedParameters.add(VALIDATE_IF_SCHEMA); - m_supportedParameters.add(WELL_FORMED); - m_supportedParameters.add(WS_IN_ELEMENT_CONTENT); - - //LSSerializer defined parameters - m_supportedParameters.add(DISCARD_DEFAULT_CONTENT); - m_supportedParameters.add(FORMAT_XML); - m_supportedParameters.add(IGNORE_UNKNOWN_CHAR_DENORM); - m_supportedParameters.add(XML_DECLARATION); - - //Additional parameters supported by DOMSerializerConfiguration - m_supportedParameters.add(SOFT_TABS); - m_supportedParameters.add(INDENT); - m_supportedParameters.add(XML_ENCODING); - }//}}} - - //{{{ Private members - - //{{{ DOMStringListImpl class - - private static class DOMStringListImpl implements DOMStringList { - - //{{{ DOMStringListImpl constructor - - public DOMStringListImpl(ArrayList list) { - m_list = list; - }//}}} - - //{{{ contains() - - public boolean contains(String str) { - for (int i=0; i<m_list.size(); i++) { - if (m_list.get(i).toString().equals(str)) { - return true; - } - } - return false; - }//}}} - - //{{{ getLength() - - public int getLength() { - return m_list.size(); - }//}}} - - //{{{ item() - - public String item(int index) { - return m_list.get(index).toString(); - }//}}} - - //{{{ Private members - private ArrayList m_list; - //}}} - - }//}}} - - private Hashtable m_parameters = new Hashtable(16); - - //}}} -} Deleted: branches/jsxe2/src/net/sourceforge/jsxe/dom/DOMSerializerException.java =================================================================== --- branches/jsxe2/src/net/sourceforge/jsxe/dom/DOMSerializerException.java 2006-09-06 19:20:54 UTC (rev 1238) +++ branches/jsxe2/src/net/sourceforge/jsxe/dom/DOMSerializerException.java 2006-09-06 19:26:53 UTC (rev 1239) @@ -1,65 +0,0 @@ -/* -DOMSerializerException.java -:tabSize=4:indentSize=4:noTabs=true: -:folding=explicit:collapseFolds=1: - -Copyright (C) 2002 Ian Lewis (Ian...@me...) - -This program is free software; you can redistribute it and/or -modify it under the terms of the GNU General Public License -as published by the Free Software Foundation; either version 2 -of the License, or (at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -Optionally, you may find a copy of the GNU General Public License -from http://www.fsf.org/copyleft/gpl.txt -*/ - -package net.sourceforge.jsxe.dom; - -//{{{ imports -/* -All classes are listed explicitly so -it is easy to see which package it -belongs to. -*/ - -//{{{ DOM classes -import org.w3c.dom.DOMError; -//}}} - -//}}} - -/** - * Signals that a serialization error of some kind has occurred. - * @author <a href="mailto:IanLewis at member dot fsf dot org">Ian Lewis</a> - * @version $Id$ - * @see DOMSerializer - */ -public class DOMSerializerException extends Exception { - - //{{{ DOMSerializerException constructor - - public DOMSerializerException(DOMError err) { - super(((Throwable)err.getRelatedException()).getMessage()); - error = err; - }//}}} - - //{{{ getError() - - public DOMError getError() { - return error; - }//}}} - - //{{{ Private members - private DOMError error; - //}}} - -} Copied: branches/jsxe2/src/net/sourceforge/jsxe/dom/ls/DOMSerializer.java (from rev 1232, branches/jsxe2/src/net/sourceforge/jsxe/dom/DOMSerializer.java) =================================================================== --- branches/jsxe2/src/net/sourceforge/jsxe/dom/ls/DOMSerializer.java (rev 0) +++ branches/jsxe2/src/net/sourceforge/jsxe/dom/ls/DOMSerializer.java 2006-09-06 19:26:53 UTC (rev 1239) @@ -0,0 +1,825 @@ +/* +DOMSerializer.java +:tabSize=4:indentSize=4:noTabs=true: +:folding=explicit:collapseFolds=1: + +This attempts to conform to the DOM3 implementation in Xerces. It tries to +conform to DOM3 as of Xerces 2.6.0. I'm not one to stay on the bleeding edge +but it is as close to a standard interface for load & save as you can get +and I didn't want to work around the fact that current serializers aren't +very good. + +Copyright (C) 2002 Ian Lewis (Ian...@me...) + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +Optionally, you may find a copy of the GNU General Public License +from http://www.fsf.org/copyleft/gpl.txt +*/ + +package net.sourceforge.jsxe.dom.ls; + +//{{{ imports +/* +All classes are listed explicitly so +it is easy to see which ... [truncated message content] |