[jdom-interest] Re: Mailing List for JDOM

Brett McLaughlin brett.mclaughlin at lutris.com
Thu Sep 14 10:05:44 PDT 2000


Michael Engelhart wrote:
> 
> > The list is fixed... sorry I forgot to get back to you on that.
> Thanks - I saw your or Jason's post to the list about it.
> 
> > Hey, isn't your bug in getAttribute().getValue():
> essentially yes, but I was using the getAttributeValue() helper method.
> 
> > Isn't this simply a case of Java doing the right thing? The backslash
> > ("\") is an escape character, which means that anytime it goes into a
> > String, it essentially gets turned into an escape sequence. So "\\" is a
> > way to output "\" in a program. I think if you want "\\" in your output,
> > you /should/ have to enter in "\\\\" as the value, at least in a Java
> > program. If we did ensure that "\" wasn't treated as an escape value,
> > but instead as a literal, I'm not sure how you would enter in actual
> > escape characters (like \n, \r, etc.). Granted, I'm not sure you would
> > /want/ to in an attribute value, but I know you would in element values.
> > So do we want the value of an Attribute to be set using different rules
> > than an Element? I'm not sure about this.
> >
> > The more I think about it, the more I think this isn't a bug, but just
> > the way Java handles String values and escape sequences. Let me know
> > what you think.
> 
> Well the only reason it doesn't "seem" right to me is that for the last 6
> months I've been using Properties files and ResourceBundles to store these
> regular expressions and never had any trouble when calling something like
> ResourceBundle.getBundle("mybundle").getString("regex1")
> 
> I know ResourceBundle's internally use Properties objects but I'm not sure
> why it would work for ResourceBundle's and get escaped by JDOM?


Hmmm. I'm confused - I can't reproduce this behavior. Here's my XML
document:

<?xml version='1.0'?>

<DOCUMENT>
  <resource key="REGEX" value="m/[a-z].*\\W+/s" />
</DOCUMENT>


Here's my code:

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

public class Test {

    /** Default SAX Driver class to use */
    private static final String DEFAULT_SAX_DRIVER_CLASS =
        "org.apache.xerces.parsers.SAXParser";

    /** SAX Driver Class to use */
    private String saxDriverClass;

    /** <code>{@link SAXBuilder}</code> instance to use */
    private SAXBuilder builder;

    /**
     * <p>
     * This will create an instance of <code>{@link SAXBuilder}</code>
     *   for use in the rest of this program.
     * </p>
     *
     * @param saxDriverClass <code>String</code> name of driver class to
use.
     */
    public Test(String saxDriverClass) {
        this.saxDriverClass = saxDriverClass;
        builder = new SAXBuilder(saxDriverClass);
    }

    /**
     * <p>
     * This will parse the specified filename using SAX and the
     *   SAX driver class specified in the constructor.
     * </p>
     *
     * @param filename <code>String</code> name of file to parse.
     * @param out <code>OutputStream</code> to output to.
     */
    public void testBuilder(String filename, OutputStream out) 
        throws IOException, JDOMException {

        // Build the JDOM Document
        Document doc = builder.build(new File(filename));

        Element testElement = doc.getRootElement().getChild("resource");
        System.out.println(testElement.getName());
        System.out.println(testElement.getAttributeValue("value"));
    }

    /**
     * <p>
     * This provides a static entry point for creating a JDOM
     *   <code>{@link Document}</code> object using a SAX 2.0
     *   parser (an <code>XMLReader</code> implementation).
     * </p>
     *
     * @param args <code>String[]</code>
     *        <ul>
     *         <li>First argument: filename of XML document to
parse</li>
     *         <li>Second argument: optional name of SAX Driver
class</li>
     *        </ul>
     */
    public static void main(String[] args) {
        if ((args.length != 1) && (args.length != 2)) {
            System.out.println("Usage: java
org.jdom.examples.io.SAXBuilderTest " +
                               "[XML document filename] ([SAX Driver
Class])");
            return;
        }

        // Load filename and SAX driver class
        String filename = args[0];
        String saxDriverClass = DEFAULT_SAX_DRIVER_CLASS;
        if (args.length == 2) {
            saxDriverClass = args[1];
        }

        // Create an instance of the tester and test
        try {
            Test test = new Test(saxDriverClass);
            test.testBuilder(filename, System.out);
        } catch (JDOMException e) {
            if (e.getRootCause() != null) {
                e.getRootCause().printStackTrace();
            } else {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }    
    }
}

My result is:


C:\projects\dev\jdom\build\classes>java Test inline.xml
resource
m/[a-z].*\\W+/s

So I'm not getting the same problem. Can you get the latest JDOM from
CVS and try this?

Thanks,
Brett

> 
> Any ideas?
> 
> thanks a lot
> 
> Mike
> 
> > >>>>>>>
> > I have simple XML file with elements like this:
> >
> > <resource key="REGEX" value="m/[a-z].*\\W+/s" />
> >
> >
> > when I read it in and call Element.getAttributeValue("value"), I get
> > this
> > as the string:
> > m/[a-z].*\W+/s
> >
> > where the original has an escaped "\\" in it:
> > m/[a-z].*\\W+/s
> >
> > Any idea what gives?
> > <<<<<<<
> >  >

-- 
Brett McLaughlin, Enhydra Strategist
Lutris Technologies, Inc. 
1200 Pacific Avenue, Suite 300 
Santa Cruz, CA 95060 USA 
http://www.lutris.com
http://www.enhydra.org



More information about the jdom-interest mailing list