Browse Source

Adding XMLWriter



git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/branches/archiva-jpox-database-refactor@529613 13f79535-47bb-0310-9956-ffa450edef68
tags/archiva-1.0-alpha-1
Joakim Erdfelt 17 years ago
parent
commit
8e01424163

+ 16
- 0
archiva-base/archiva-xml-tools/pom.xml View File

@@ -37,11 +37,27 @@
<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>

+ 50
- 0
archiva-base/archiva-xml-tools/src/main/java/org/apache/maven/archiva/xml/XMLWriter.java View File

@@ -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 );
}
}
}

+ 3
- 2
archiva-base/archiva-xml-tools/src/test/examples/prolog-with-utf8.xml View 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>
</basic>

+ 55
- 0
archiva-base/archiva-xml-tools/src/test/java/org/apache/maven/archiva/xml/XMLWriterTest.java View File

@@ -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() );
}
}

Loading…
Cancel
Save