Providing 1/4 of default implementation for XML operations from java.util.** and verifying that its behaviour is similar enough with the original implementation based on DOM
1.1 --- a/build.xml Wed Jun 24 13:44:46 2009 +0200
1.2 +++ b/build.xml Wed Jun 24 14:53:01 2009 +0200
1.3 @@ -10,6 +10,9 @@
1.4 <antcall target="corba"/>
1.5 <antcall target="tools"/>
1.6 <antcall target="deprecated7"/>
1.7 +
1.8 +
1.9 + <antcall target="test"/>
1.10 </target>
1.11
1.12 <!-- basic parameters -->
1.13 @@ -355,6 +358,29 @@
1.14 </antcall>
1.15 </target>
1.16
1.17 + <!-- basic sanity tests -->
1.18 + <target name="test">
1.19 + <property name="test.dir" value="build/test"/>
1.20 + <mkdir dir="${test.dir}"/>
1.21 + <path id="boot">
1.22 + <fileset dir="${build.dir}">
1.23 + <include name="*.jar"/>
1.24 + </fileset>
1.25 + </path>
1.26 +
1.27 + <javac srcdir="test/java/util/Properties" destdir="${test.dir}" includeantruntime="false" includejavaruntime="false">
1.28 + <bootclasspath refid="boot"/>
1.29 + </javac>
1.30 +
1.31 + <java classname="XMLReadAndWriteTest" fork="true">
1.32 + <jvmarg value="-ea"/>
1.33 + <classpath>
1.34 + <path refid="boot"/>
1.35 + <pathelement location="${test.dir}"/>
1.36 + </classpath>
1.37 + </java>
1.38 + </target>
1.39 +
1.40 <!-- shared routine to compile one of the modules -->
1.41 <target name="-compile-one-module">
1.42 <mkdir dir="${build.dir}/classes/${module}"/>
2.1 --- a/src/share/classes/com/sun/xml/internal/PrefsXmlSupportImpl.java Wed Jun 24 13:44:46 2009 +0200
2.2 +++ b/src/share/classes/com/sun/xml/internal/PrefsXmlSupportImpl.java Wed Jun 24 14:53:01 2009 +0200
2.3 @@ -97,7 +97,7 @@
2.4 */
2.5 public void export(OutputStream os, final Preferences p, boolean subTree)
2.6 throws IOException, BackingStoreException {
2.7 - if (((AbstractPreferences)p).isRemoved())
2.8 + if (isRemoved(p))
2.9 throw new IllegalStateException("Node has been removed");
2.10 Document doc = createPrefsDoc("preferences");
2.11 Element preferences = doc.getDocumentElement() ;
2.12 @@ -145,10 +145,10 @@
2.13 // Node is locked to export its contents and get a
2.14 // copy of children, then lock is released,
2.15 // and, if subTree = true, recursive calls are made on children
2.16 - synchronized (((AbstractPreferences)prefs).lock) {
2.17 + synchronized (lock(prefs)) {
2.18 // Check if this node was concurrently removed. If yes
2.19 // remove it from XML Document and return.
2.20 - if (((AbstractPreferences)prefs).isRemoved()) {
2.21 + if (isRemoved(prefs)) {
2.22 elt.getParentNode().removeChild(elt);
2.23 return;
2.24 }
2.25 @@ -297,9 +297,9 @@
2.26 */
2.27 Preferences[] prefsKids;
2.28 /* Lock the node */
2.29 - synchronized (((AbstractPreferences)prefsNode).lock) {
2.30 + synchronized (lock(prefsNode)) {
2.31 //If removed, return silently
2.32 - if (((AbstractPreferences)prefsNode).isRemoved())
2.33 + if (isRemoved(prefsNode))
2.34 return;
2.35
2.36 // Import any preferences at this node
2.37 @@ -419,4 +419,21 @@
2.38 throw x;
2.39 }
2.40 }
2.41 +
2.42 + private static boolean isRemoved(Preferences p) {
2.43 + try {
2.44 + p.parent(); // throws IllegalStateException if removed;
2.45 + return false;
2.46 + } catch (IllegalStateException ex) {
2.47 + return true;
2.48 + }
2.49 + }
2.50 +
2.51 + private static Object lock(Preferences p) {
2.52 + /** JST-XXX: Needs reflection or accessor:
2.53 + * http://wiki.apidesign.org/wiki/FriendPackages
2.54 + return ((AbstractPreferences)prefs).lock;
2.55 + */
2.56 + return p;
2.57 + }
2.58 }
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/src/share/classes/sun/util/xml/DefaultPropertiesXMLUtils.java Wed Jun 24 14:53:01 2009 +0200
3.3 @@ -0,0 +1,67 @@
3.4 +/*
3.5 + * Copyright 2003-2004 Sun Microsystems, Inc. All Rights Reserved.
3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3.7 + *
3.8 + * This code is free software; you can redistribute it and/or modify it
3.9 + * under the terms of the GNU General Public License version 2 only, as
3.10 + * published by the Free Software Foundation. Sun designates this
3.11 + * particular file as subject to the "Classpath" exception as provided
3.12 + * by Sun in the LICENSE file that accompanied this code.
3.13 + *
3.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
3.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3.17 + * version 2 for more details (a copy is included in the LICENSE file that
3.18 + * accompanied this code).
3.19 + *
3.20 + * You should have received a copy of the GNU General Public License version
3.21 + * 2 along with this work; if not, write to the Free Software Foundation,
3.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3.23 + *
3.24 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
3.25 + * CA 95054 USA or visit www.sun.com if you need additional information or
3.26 + * have any questions.
3.27 + */
3.28 +
3.29 +package sun.util.xml;
3.30 +
3.31 +import java.io.*;
3.32 +import java.util.Properties;
3.33 +import java.util.InvalidPropertiesFormatException;
3.34 +import java.util.Map.Entry;
3.35 +
3.36 +/**
3.37 + * A class used to aid in Properties load and save in XML in simple way, e.g.
3.38 + * without dependency on the big machinery of XML, SAX and DOM 2.
3.39 + *
3.40 + * @author Jaroslav Tulach
3.41 + * @since 1.7
3.42 + */
3.43 +public class DefaultPropertiesXMLUtils extends PropertiesXMLUtils {
3.44 + public void load(Properties props, InputStream in)
3.45 + throws IOException, InvalidPropertiesFormatException {
3.46 + throw new IOException("Not implemented yet");
3.47 + }
3.48 + public void save(Properties props, OutputStream out, String comment, String encoding)
3.49 + throws IOException {
3.50 + PrintWriter w = new PrintWriter(new OutputStreamWriter(out, encoding));
3.51 + w.println("<?xml version=\"1.0\" encoding=\"" + encoding + "\" standalone=\"no\"?>");
3.52 + w.println("<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">");
3.53 + w.println("<properties>");
3.54 + if (comment != null) {
3.55 + w.print("<comment>");
3.56 + w.print(comment);
3.57 + w.println("</comment>");
3.58 + }
3.59 + for (Entry<Object, Object> entry : props.entrySet()) {
3.60 + w.print("<entry key=\"");
3.61 + w.print(entry.getKey());
3.62 + w.print("\">");
3.63 + w.print(entry.getValue());
3.64 + w.println("</entry>");
3.65 + }
3.66 + w.println("</properties>");
3.67 + w.flush();
3.68 + }
3.69 +
3.70 +}
4.1 --- a/src/share/classes/sun/util/xml/PropertiesXMLUtils.java Wed Jun 24 13:44:46 2009 +0200
4.2 +++ b/src/share/classes/sun/util/xml/PropertiesXMLUtils.java Wed Jun 24 14:53:01 2009 +0200
4.3 @@ -42,7 +42,7 @@
4.4 public abstract class PropertiesXMLUtils {
4.5 public static PropertiesXMLUtils getDefault() {
4.6 Iterator<PropertiesXMLUtils> it = ServiceLoader.load(PropertiesXMLUtils.class).iterator();
4.7 - return it.hasNext() ? it.next() : null /* JST-XXX: some default */;
4.8 + return it.hasNext() ? it.next() : new DefaultPropertiesXMLUtils();
4.9 }
4.10
4.11 public abstract void load(Properties props, InputStream in) throws
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/test/java/util/Properties/XMLReadAndWriteTest.java Wed Jun 24 14:53:01 2009 +0200
5.3 @@ -0,0 +1,51 @@
5.4 +/*
5.5 + * To change this template, choose Tools | Templates
5.6 + * and open the template in the editor.
5.7 + */
5.8 +
5.9 +import sun.util.xml.PropertiesXMLUtils;
5.10 +
5.11 +import java.io.ByteArrayOutputStream;
5.12 +import java.io.IOException;
5.13 +import java.util.Properties;
5.14 +
5.15 +/** Checks whether reading and writing via standard DOM and simplified API
5.16 + * results in same results.
5.17 + *
5.18 + * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5.19 + */
5.20 +public class XMLReadAndWriteTest {
5.21 + private static PropertiesXMLUtils FULL = new com.sun.xml.internal.PropertiesXMLUtilsImpl();
5.22 + private static PropertiesXMLUtils SIMPLE = new sun.util.xml.DefaultPropertiesXMLUtils();
5.23 +
5.24 +
5.25 + public static void main(String[] args) throws Exception {
5.26 + XMLReadAndWriteTest test = new XMLReadAndWriteTest();
5.27 + test.testCompareOutput();
5.28 + }
5.29 +
5.30 +
5.31 + public XMLReadAndWriteTest() {
5.32 + }
5.33 +
5.34 + public void testCompareOutput() throws IOException {
5.35 + Properties p = new Properties();
5.36 + p.setProperty("ahoj", "simple");
5.37 + p.setProperty("kuk", "buk");
5.38 + p.setProperty("multi", "one\ntwo\nthree\nfour");
5.39 +
5.40 + ByteArrayOutputStream full = new ByteArrayOutputStream();
5.41 + FULL.save(p, full, "my commment on beginging\nand on the second line\n", "UTF-8");
5.42 +
5.43 + ByteArrayOutputStream simple = new ByteArrayOutputStream();
5.44 + SIMPLE.save(p, simple, "my commment on beginging\nand on the second line\n", "UTF-8");
5.45 + if (full.toString().equals(simple.toString())) {
5.46 + // OK
5.47 + System.err.println("OK: testCompareOutput");
5.48 + } else {
5.49 + assert false :
5.50 + "Full version differs from simplified. Full:\n" + full + "\nSimple:\n" + simple;
5.51 + }
5.52 + }
5.53 +
5.54 +}
5.55 \ No newline at end of file