Skip to content
Permalink
Browse files
CXF-4069 Add a new module for JAX DataTypeAdapter and the ToString cl…
…asses so generated code does not have to depend on cxf

git-svn-id: https://svn.apache.org/repos/asf/cxf/xjc-utils/trunk@1238358 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
cschneider committed Jan 31, 2012
1 parent 19626b1 commit 29c71b4bcc2f71cbd02e2472fa330e4e61a5d56b
Showing 9 changed files with 468 additions and 4 deletions.
@@ -59,6 +59,7 @@
<module>wsdlextension-test</module>
<module>bug671</module>
<module>property-listener</module>
<module>runtime</module>
</modules>
<repositories>
<repository>
@@ -0,0 +1,51 @@
<!--
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.cxf.xjc-utils</groupId>
<artifactId>cxf-xjc-runtime</artifactId>
<packaging>jar</packaging>
<name>Apache CXF XJC Runtime</name>
<url>http://cxf.apache.org</url>

<parent>
<groupId>org.apache.cxf.xjc-utils</groupId>
<artifactId>xjc-utils</artifactId>
<version>2.4.1-SNAPSHOT</version>
</parent>


<properties>
<maven.test.skip>true</maven.test.skip>
</properties>

<dependencies>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
@@ -0,0 +1,75 @@
/**
* 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.
*/
package org.apache.cxf.xjc.runtime;

import java.util.Calendar;
import java.util.Date;

import javax.xml.bind.DatatypeConverter;

public final class DataTypeAdapter {

private DataTypeAdapter() {
}

public static Date parseDate(String s) {
if (s == null) {
return null;
}
return DatatypeConverter.parseDate(s).getTime();
}
public static String printDate(Date dt) {
if (dt == null) {
return null;
}
Calendar c = Calendar.getInstance();
c.setTime(dt);
return DatatypeConverter.printDate(c);
}

public static Date parseTime(String s) {
if (s == null) {
return null;
}
return DatatypeConverter.parseTime(s).getTime();
}
public static String printTime(Date dt) {
if (dt == null) {
return null;
}
Calendar c = Calendar.getInstance();
c.setTime(dt);
return DatatypeConverter.printTime(c);
}

public static Date parseDateTime(String s) {
if (s == null) {
return null;
}
return DatatypeConverter.parseDateTime(s).getTime();
}
public static String printDateTime(Date dt) {
if (dt == null) {
return null;
}
Calendar c = Calendar.getInstance();
c.setTime(dt);
return DatatypeConverter.printDateTime(c);
}
}
@@ -0,0 +1,44 @@
/**
* 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.
*/

package org.apache.cxf.xjc.runtime;

import java.util.Collection;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

public final class JAXBToStringBuilder {
private JAXBToStringBuilder() {
}

public static String valueOf(Object object) {
return valueOf(object, JAXBToStringStyle.DEFAULT_STYLE);
}

public static String valueOf(Object object, ToStringStyle style) {
if (object instanceof String) {
return (String)object;
}
if (object instanceof Collection) {
object = ((Collection<?>) object).toArray();
}
return ToStringBuilder.reflectionToString(object, style);
}
}
@@ -0,0 +1,85 @@
/**
* 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.
*/

package org.apache.cxf.xjc.runtime;

import javax.xml.bind.JAXBElement;

import org.apache.commons.lang.SystemUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/*
* Override default styles to recognise JAXBElement as needing introspection
*/
public final class JAXBToStringStyle {

public static final ToStringStyle MULTI_LINE_STYLE =
new JAXBToStringStyleImpl(true);

public static final ToStringStyle SIMPLE_STYLE =
new JAXBToStringStyleImpl(false);

public static final ToStringStyle DEFAULT_STYLE =
new JAXBToStringStyleImpl();

private JAXBToStringStyle() {
//utility class
}
}

class JAXBToStringStyleImpl extends ToStringStyle {
private static final long serialVersionUID = -584903238590491314L;

JAXBToStringStyleImpl() {
super();
}

JAXBToStringStyleImpl(boolean multiLine) {
super();
if (multiLine) {
this.setContentStart("[");
this.setFieldSeparator(SystemUtils.LINE_SEPARATOR + " ");
this.setFieldSeparatorAtStart(true);
this.setContentEnd(SystemUtils.LINE_SEPARATOR + "]");
} else {
// simple
this.setUseClassName(false);
this.setUseIdentityHashCode(false);
this.setUseFieldNames(false);
this.setContentStart("");
this.setContentEnd("");
}
}

/*
* Introspect into JAXBElement as a special case as it does not have a
* toString() and we loose the content
*
* @see org.apache.commons.lang.builder.ToStringStyle
*/
@Override
protected void appendDetail(StringBuffer buffer, String fieldName, Object value) {
if (value instanceof JAXBElement) {
buffer.append(ToStringBuilder.reflectionToString(value, this));
} else {
buffer.append(value);
}
}
}
@@ -0,0 +1,96 @@
/**
* 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.
*/

package org.apache.cxf.xjc.runtime;


import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;

import org.apache.commons.lang.builder.ToStringBuilder;

import org.junit.Assert;
import org.junit.Test;

public class JAXBElementToStringStyleTest {

class Holder {
String name = "HolderName";
Object obj;
Holder(Object o) {
this.obj = o;
}
}

JAXBElement<String> nel =
new JAXBElement<String>(
new QName("ab", "bv"),
String.class, "SomeText");

Holder h = new Holder(nel);

@Test
public void testToStringDefault() throws Exception {

String ts = ToStringBuilder.reflectionToString(h);

validateHolderString(ts);

// JAXBElement contents not present
Assert.assertTrue("has no value", ts.indexOf("value") == -1);
Assert.assertTrue("has no bv", ts.indexOf("bv") == -1);

}

@Test
public void testToStringMultiLineStyle() throws Exception {
String ts =
ToStringBuilder.reflectionToString(h, JAXBToStringStyle.MULTI_LINE_STYLE);

validateHolderString(ts);
validateElementString(ts);
}

@Test
public void testToStringSimpleStyle() throws Exception {
String ts =
ToStringBuilder.reflectionToString(h, JAXBToStringStyle.SIMPLE_STYLE);

// field names are missing
Assert.assertTrue("has no obj field", ts.indexOf("obj") == -1);
Assert.assertTrue("has HolderName", ts.indexOf("HolderName") != -1);
Assert.assertTrue("has SomeText", ts.indexOf("SomeText") != -1);
}

private void validateHolderString(String ts) {
Assert.assertTrue("has HolderName", ts.indexOf("HolderName") != -1);
Assert.assertTrue("has JAXBElement", ts.indexOf("JAXBElement") != -1);
Assert.assertTrue("has obj", ts.indexOf("obj") != -1);
}

private void validateElementString(String ts) {
Assert.assertTrue("has value", ts.indexOf("value") != -1);
Assert.assertTrue("has scope", ts.indexOf("scope") != -1);
Assert.assertTrue("has bv", ts.indexOf("bv") != -1);


}


}

0 comments on commit 29c71b4

Please sign in to comment.