Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doctype support #5

Open
mathieucarbou opened this issue Jul 29, 2013 · 1 comment
Open

Doctype support #5

mathieucarbou opened this issue Jul 29, 2013 · 1 comment

Comments

@mathieucarbou
Copy link
Owner

Reported by mikenereson, Sep 12, 2011
Is it possible to set the doctype?

I have an english properties file that I translate to several other languages. It seems in order for Properties#loadFromXML to work I need to include a doctype:

  <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">

Properties#loadFromXML

http://download.oracle.com/javase/1,5.0/docs/api/java/util/Properties.html#loadFromXML(java.io.InputStream)
Sep 12, 2011 Delete comment #1 mikenereson
Not your problem, not information nontheless, here is the stacktrace from trying to load the file created with XML tool

Exception in thread "main" java.util.InvalidPropertiesFormatException: org.xml.sax.SAXParseException: Document root element "properties", must match DOCTYPE root "null".
    at java.util.XMLUtils.load(XMLUtils.java:59)
    at java.util.Properties.loadFromXML(Properties.java:852)
    at com.retrieve.XmlApp.main(XmlApp.java:61)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: org.xml.sax.SAXParseException: Document root element "properties", must match DOCTYPE root "null".

And here is a sample of the file being loaded.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<properties>
    <entry key="i18n.language.abbreviation">pl</entry>
    <entry key="i18n.language.name">Polish</entry>
    <entry key="application.header.home">Dom</entry>

Sep 12, 2011 Delete comment #2 mikenereson
My solution, for the record, while waiting to hear if this functionality is available in XML tool, was to write the XML file out, then read it back in add the doctype and write it out again.

http://www.exampledepot.com/egs/javax.xml.transform/SetDocType.html

    private static void addPropertiesDoctype(final File xmlFile)
    {
        final Document document = BasicDom.parseXmlFile(xmlFile.getAbsolutePath(), false);

        try
        {
            final Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "http://java.sun.com/dtd/properties.dtd");

            final Source source = new DOMSource(document);
            final Result result = new StreamResult(xmlFile);
            transformer.transform(source, result);
        }
        catch (final TransformerConfigurationException e)
        {
        }
        catch (final TransformerException e)
        {
        }
    }

resulting in

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
    <entry key="i18n.language.abbreviation">pl</entry>
    <entry key="i18n.language.name">Polish</entry>
    <entry key="application.header.home">Dom</entry>

Sep 12, 2011 Delete comment Project Member #3 mathieu.carbou
Hi,

Thanks for the report, this is definitely a feature that should be added. I'll try to do my best to spend time on it, plus other requests, by the end of the month and release a new version. I am very busy at the moment.

Status: Accepted
Owner: mathieu.carbou
Labels: Type-Enhancement Priority-High Milestone-Release3.4
Sep 12, 2011 Delete comment #4 mikenereson
Mathieu, don't rush for me. I have a solution that works. I really just wasn't sure if I was not looking in the right place, or if it was a missing feature.

I thought I could get at the underlying dom builder, or something, to set the value, but if its a missing feature, then you've confirmed that its not at this time possible to do this.

Thanks, for the tool. Been using it on several projects since 2008.

@javadev
Copy link

javadev commented Sep 15, 2020

Hi @mathieucarbou,

You may try underscore-java library. It has U.fromXmlMap(xmlstring) method that supports doctype. You may generate xml with a method U.toXml(map).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants