]> source.dussan.org Git - archiva.git/commitdiff
Adding XMLWriter
authorJoakim Erdfelt <joakime@apache.org>
Tue, 17 Apr 2007 14:10:16 +0000 (14:10 +0000)
committerJoakim Erdfelt <joakime@apache.org>
Tue, 17 Apr 2007 14:10:16 +0000 (14:10 +0000)
git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/branches/archiva-jpox-database-refactor@529613 13f79535-47bb-0310-9956-ffa450edef68

archiva-base/archiva-xml-tools/pom.xml
archiva-base/archiva-xml-tools/src/main/java/org/apache/maven/archiva/xml/XMLWriter.java [new file with mode: 0644]
archiva-base/archiva-xml-tools/src/test/examples/prolog-with-utf8.xml
archiva-base/archiva-xml-tools/src/test/java/org/apache/maven/archiva/xml/XMLWriterTest.java [new file with mode: 0644]

index efa42d3f0f17af4b6f8972791c04001ecd386f04..352f6891bb3bc249263ff081c7fbceb0087a46d6 100644 (file)
       <groupId>dom4j</groupId>
       <artifactId>dom4j</artifactId>
       <version>1.6.1</version>
+      <exclusions>
+        <exclusion>
+          <groupId>xom</groupId>
+          <artifactId>xom</artifactId>
+        </exclusion>
+      </exclusions>
     </dependency>
     <dependency>
       <groupId>jaxen</groupId>
       <artifactId>jaxen</artifactId>
       <version>1.1</version>
+      <exclusions>
+        <exclusion>
+          <groupId>jdom</groupId>
+          <artifactId>jdom</artifactId>
+        </exclusion>
+        <exclusion>
+          <groupId>xom</groupId>
+          <artifactId>xom</artifactId>
+        </exclusion>
+      </exclusions>
     </dependency>
     <dependency>
       <groupId>org.codehaus.plexus</groupId>
diff --git a/archiva-base/archiva-xml-tools/src/main/java/org/apache/maven/archiva/xml/XMLWriter.java b/archiva-base/archiva-xml-tools/src/main/java/org/apache/maven/archiva/xml/XMLWriter.java
new file mode 100644 (file)
index 0000000..575d8e7
--- /dev/null
@@ -0,0 +1,50 @@
+package org.apache.maven.archiva.xml;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.dom4j.Document;
+import org.dom4j.io.OutputFormat;
+
+import java.io.IOException;
+import java.io.Writer;
+
+/**
+ * XMLWriter 
+ *
+ * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
+ * @version $Id$
+ */
+public class XMLWriter
+{
+    public static void write( Document doc, Writer writer )
+        throws XMLException
+    {
+        try
+        {
+            OutputFormat outputFormat = OutputFormat.createPrettyPrint();
+            org.dom4j.io.XMLWriter xmlwriter = new org.dom4j.io.XMLWriter( writer, outputFormat );
+            xmlwriter.write( doc );
+        }
+        catch ( IOException e )
+        {
+            throw new XMLException( "Unable to write xml contents to writer: " + e.getMessage(), e );
+        }
+    }
+}
index c1aa8f454654dc9e13438151057bff7673be7273..3bc319199a95519069f493c18825b1d1afdc5024 100644 (file)
@@ -1,7 +1,8 @@
-<?xml version="1.0" encoding="UTF-8" ?>
+<?xml version="1.0" encoding="UTF-8"?>
+
 <basic>
   <names>
     <name>Trygve Laugstøl</name>
     <name>The ∞ Archiva</name>
   </names>
-</basic>
\ No newline at end of file
+</basic>
diff --git a/archiva-base/archiva-xml-tools/src/test/java/org/apache/maven/archiva/xml/XMLWriterTest.java b/archiva-base/archiva-xml-tools/src/test/java/org/apache/maven/archiva/xml/XMLWriterTest.java
new file mode 100644 (file)
index 0000000..f8dcde2
--- /dev/null
@@ -0,0 +1,55 @@
+package org.apache.maven.archiva.xml;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.codehaus.plexus.util.FileUtils;
+import org.dom4j.Document;
+import org.dom4j.DocumentHelper;
+import org.dom4j.Element;
+
+import java.io.StringWriter;
+
+/**
+ * XMLWriterTest 
+ *
+ * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
+ * @version $Id$
+ */
+public class XMLWriterTest
+    extends AbstractArchivaXmlTestCase
+{
+    public void testWrite()
+        throws Exception
+    {
+        String expectedResults = FileUtils.fileRead( getExampleXml( "prolog-with-utf8.xml" ) );
+
+        Element basic = DocumentHelper.createElement( "basic" );
+        Document doc = DocumentHelper.createDocument( basic );
+
+        Element names = basic.addElement( "names" );
+        names.addElement( "name" ).setText( "Trygve Laugst\u00f8l" );
+        names.addElement( "name" ).setText( "The \u221e Archiva" );
+
+        StringWriter actual = new StringWriter();
+        XMLWriter.write( doc, actual );
+
+        assertEquals( "Comparision of contents:", expectedResults, actual.toString() );
+    }
+}