Hi all.

Here is a patch to allow jboss to work with a generic xml parser.  This allows
you to use, for instance xerces.jar instead of xml.jar.  Sorry this didn't go
the jboss-dev, but I'm not really in a position where I can justify (to my
employer) being on that list.  In March maybe (ah! the student life!) Anyway,
if someone feels like committing this, that'd be good.

The first patch is to run.sh.

9a10,13
> if [ ! -z XML_DOCUMENT_BUILDER_FACTORY ] ; then
>       
>XML_DOCUMENT_BUILDER_FACTORY=-Djavax.xml.parsers.DocumentBuilderFactoryImpl=$XML_DOCUMENT_BUILDER_FACTORY
> ;
> fi
> 
11c15
< java -server -classpath $CLASSPATH org.jboss.Main $@
---
> java -server $XML_DOCUMENT_BUILDER_FACTORY -classpath $CLASSPATH org.jboss.Main $@

This allows you to set an environment variable, XML_DOCUMENT_BUILDER_FACTORY
which will be used as the document builder factory implementation.  This class
must be in your classpath before you execute run.sh.

The second patch is to org/jboss/configuration/ConfigurationService.java:

22c22
< import com.sun.xml.tree.*;
---
> import javax.xml.parsers.*;
157c157,160
<         XmlDocument doc = new XmlDocument();
---
>         DocumentBuilderFactory xdbFactory = DocumentBuilderFactory.newInstance();
>         DocumentBuilder xdb = xdbFactory.newDocumentBuilder();
> 
>         Document doc = xdb.newDocument();
200c203,204
<         doc.write(out, "UTF-8");
---
>       DOMWriter dWriter = new DOMWriter( true, new PrintWriter( out ) );
>       dWriter.print( doc );
221,227c225,231
<               out = new PrintWriter(new FileOutputStream(confFile.getFile()));<      
      } catch (java.io.FileNotFoundException e) {
<               log.error("Configuration file "+confFile.getFile()+" must be available 
and writable.");
<               log.exception(e);
<            }
<            out.print(xml);
<            out.close();
---
>                 out = new PrintWriter(new FileOutputStream(confFile.getFile()));
>              } catch (java.io.FileNotFoundException e) {
>                      log.error("Configuration file "+confFile.getFile()+" must be 
>available and writable.");
>                      log.exception(e);
>              }
>              out.print(xml);
>              out.close();
248,250c252,253
<        XmlDocumentBuilder xdb = new XmlDocumentBuilder();
<        Parser parser = new com.sun.xml.parser.Parser();
<        xdb.setParser(parser);
---
>        DocumentBuilderFactory xdbFactory = DocumentBuilderFactory.newInstance();
>        DocumentBuilder xdb = xdbFactory.newDocumentBuilder();
254,255c257
<            parser.parse(new InputSource(new StringReader(cfg)));
<            userConf = xdb.getDocument();
---
>            userConf = xdb.parse( new InputSource( new StringReader( cfg ) ) 
>);275,277d276
<           xdb = new XmlDocumentBuilder();
<           parser = new com.sun.xml.parser.Parser();
<           xdb.setParser(parser);
281,282c280
<               parser.parse(new InputSource(new StringReader(cfg)));
<               autoConf = xdb.getDocument();
---
>              autoConf = xdb.parse(new InputSource(new StringReader(cfg)));
342c340
<                               ex = ((ReflectionException)ex).getTargetException();
---
>                                 ex = ((ReflectionException)ex).getTargetException();
404a403
> 

This makes the configuration service use the standard DocumentBuilderFactory
API to parse it's XML documents, instead of using the Sun parser directly.

The third change is the addition of a new class,
org.jboss.configuration.DOMWriter.  This maybe should go somewhere else, but
here's where it's used, so here's where I've put it.  It's based on a sample
from Xerces-J, but is different enough from that that I don't even know if
licensing will be an issue.  Maybe the apache license should go on it, maybe
not.  I don't know enough about the licensing issues, so someone else can add
licensing if they want.

------ DOMWriter.java
/* Adapted from the sample dom.DOMWriter from the Apache Xerces-J samples.
 * See the Xerces-J license for licensing details.
 */

package org.jboss.configuration;

import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * Utility class to serialize a DOM tree to a PrintWriter.
 */
public class DOMWriter
{
    protected boolean canonical;

    protected PrintWriter out;

    /**
     * Only constructor.
     * @param canonical If true, then the DOM tree is serialized in canonical
     *    form, otherwise it is not.
     * @param out The PrintWriter to serialize the DOM tree to.
     */
    public DOMWriter( boolean canonical, PrintWriter out)
    {
        this.out = out;
        this.canonical = canonical;
    }

    /** Prints the specified node, recursively. */
    public void print(Node node) {

        // is there anything to do?
        if ( node == null ) {
            return;
        }

        int type = node.getNodeType();
        switch ( type ) {
        // print document
        case Node.DOCUMENT_NODE: {
                if ( !canonical ) {
                    out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
                }
                //print(((Document)node).getDocumentElement());
                
                NodeList children = node.getChildNodes(); 
                for ( int iChild = 0; iChild < children.getLength(); iChild++ ) { 
                    print(children.item(iChild)); 
                } 
                out.flush();
                break;
            }

            // print element with attributes
        case Node.ELEMENT_NODE: {
                out.print('<');
                out.print(node.getNodeName());
                Attr attrs[] = sortAttributes(node.getAttributes());
                for ( int i = 0; i < attrs.length; i++ ) {
                    Attr attr = attrs[i];
                    out.print(' ');
                    out.print(attr.getNodeName());
                    out.print("=\"");
                    out.print(normalize(attr.getNodeValue()));
                    out.print('"');
                }
                out.print('>');
                NodeList children = node.getChildNodes();
                if ( children != null ) {
                    int len = children.getLength();
                    for ( int i = 0; i < len; i++ ) {
                        print(children.item(i));
                    }
                }
                break;
            }

            // handle entity reference nodes
        case Node.ENTITY_REFERENCE_NODE: {
                if ( canonical ) {
                    NodeList children = node.getChildNodes();
                    if ( children != null ) {
                        int len = children.getLength();
                        for ( int i = 0; i < len; i++ ) {
                            print(children.item(i));
                        }
                    }
                } else {
                    out.print('&');
                    out.print(node.getNodeName());
                    out.print(';');
                }
                break;
            }

            // print cdata sections
        case Node.CDATA_SECTION_NODE: {
                if ( canonical ) {
                    out.print(normalize(node.getNodeValue()));
                } else {
                    out.print("<![CDATA[");
                    out.print(node.getNodeValue());
                    out.print("]]>");
                }
                break;
            }

            // print text
        case Node.TEXT_NODE: {
                out.print(normalize(node.getNodeValue()));
                break;
            }

            // print processing instruction
        case Node.PROCESSING_INSTRUCTION_NODE: {
                out.print("<?");
                out.print(node.getNodeName());
                String data = node.getNodeValue();
                if ( data != null && data.length() > 0 ) {
                    out.print(' ');
                    out.print(data);
                }
                out.println("?>");
                break;
            }
        }

        if ( type == Node.ELEMENT_NODE ) {
            out.print("</");
            out.print(node.getNodeName());
            out.print('>');
        }

        out.flush();

    }

    /** Returns a sorted list of attributes. */
    protected Attr[] sortAttributes(NamedNodeMap attrs) {

        int len = (attrs != null) ? attrs.getLength() : 0;
        Attr array[] = new Attr[len];
        for ( int i = 0; i < len; i++ ) {
            array[i] = (Attr)attrs.item(i);
        }
        for ( int i = 0; i < len - 1; i++ ) {
            String name  = array[i].getNodeName();
            int    index = i;
            for ( int j = i + 1; j < len; j++ ) {
                String curName = array[j].getNodeName();
                if ( curName.compareTo(name) < 0 ) {
                    name  = curName;
                    index = j;
                }
            }
            if ( index != i ) {
                Attr temp    = array[i];
                array[i]     = array[index];
                array[index] = temp;
            }
        }

        return(array);

    }


    /** Normalizes the given string. */
    protected String normalize(String s) {
        StringBuffer str = new StringBuffer();

        int len = (s != null) ? s.length() : 0;
        for ( int i = 0; i < len; i++ ) {
            char ch = s.charAt(i);
            switch ( ch ) {
            case '<': {
                    str.append("&lt;");
                    break;
                }
            case '>': {
                    str.append("&gt;");
                    break;
                }
            case '&': {
                    str.append("&amp;");
                    break;
                }
            case '"': {
                    str.append("&quot;");
                    break;
                }
            case '\r':
            case '\n': {
                    if ( canonical ) {
                        str.append("&#");
                        str.append(Integer.toString(ch));
                        str.append(';');
                        break;
                    }
                    // else, default append char
                }
            default: {
                    str.append(ch);
                }
            }
        }

        return(str.toString());

    }

} 
----- End DOMWriter.java


I hope this is useful to someone else out there (it certainly is to me).

Regards
Tom


--
--------------------------------------------------------------
To subscribe:        [EMAIL PROTECTED]
To unsubscribe:      [EMAIL PROTECTED]
List Help?:          [EMAIL PROTECTED]

Reply via email to