Skip to content

Commit

Permalink
Support primitive classes
Browse files Browse the repository at this point in the history
  • Loading branch information
niallg committed Feb 18, 2013
1 parent cc03e1a commit bb6b5ec
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,51 @@
* @author Niall Gallagher
*/
class ClassTransform implements Transform<Class> {

/**
* This is the string that represents the class for an integer.
*/
private static final String INTEGER = "int";

/**
* This is the string that represents the class for a double.
*/
private static final String DOUBLE = "double";

/**
* This is the string that represents the class for a float.
*/
private static final String FLOAT = "float";

/**
* This is the string that represents the class for a boolean.
*/
private static final String BOOLEAN = "boolean";

/**
* This is the string that represents the class for a short.
*/
private static final String SHORT = "short";

/**
* This is the string that represents the class for a character.
*/
private static final String CHARACTER = "char";

/**
* This is the string that represents the class for a long.
*/
private static final String LONG = "long";

/**
* This is the string that represents the class for a byte.
*/
private static final String BYTE = "byte";

/**
* This is the string that represents the class for a void.
*/
private static final String VOID = "void";

/**
* This method is used to convert the string value given to an
Expand Down Expand Up @@ -77,31 +122,31 @@ public Class read(String target) throws Exception {
* @return this returns an appropriate instanced to be used
*/
private Class readPrimitive(String target) throws Exception {
if(target.equals("byte")) {
if(target.equals(BYTE)) {
return byte.class;
}
if(target.equals("short")) {
if(target.equals(SHORT)) {
return short.class;
}
if(target.equals("int")) {
if(target.equals(INTEGER)) {
return int.class;
}
if(target.equals("long")) {
if(target.equals(LONG)) {
return long.class;
}
if(target.equals("char")) {
if(target.equals(CHARACTER)) {
return char.class;
}
if(target.equals("float")) {
if(target.equals(FLOAT)) {
return float.class;
}
if(target.equals("double")) {
if(target.equals(DOUBLE)) {
return double.class;
}
if(target.equals("boolean")) {
if(target.equals(BOOLEAN)) {
return boolean.class;
}
if(target.equals("void")) {
if(target.equals(VOID)) {
return void.class;
}
return null;
Expand Down
50 changes: 50 additions & 0 deletions src/test/java/org/simpleframework/xml/core/PrimitiveClassTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.simpleframework.xml.core;

import java.io.StringWriter;

import org.simpleframework.xml.ValidationTestCase;

public class PrimitiveClassTest extends ValidationTestCase {

private static class Example {
private Class[] classes;
public Example() {
this.classes = new Class[]{
int.class,
double.class,
float.class,
long.class,
void.class,
byte.class,
short.class,
boolean.class,
char.class
};
}
}


public void testPrimitiveClass() throws Exception {
Example example = new Example();
Persister persister = new Persister();
StringWriter writer = new StringWriter();

persister.write(example, writer);

String text = writer.toString();
System.err.println(text);

assertElementExists(text, "/example");
assertElementExists(text, "/example/classes");
assertElementHasAttribute(text, "/example/classes", "length", "9");
assertElementHasValue(text, "/example/classes/class[1]", "int");
assertElementHasValue(text, "/example/classes/class[2]", "double");
assertElementHasValue(text, "/example/classes/class[3]", "float");
assertElementHasValue(text, "/example/classes/class[4]", "long");
assertElementHasValue(text, "/example/classes/class[5]", "void");
assertElementHasValue(text, "/example/classes/class[6]", "byte");
assertElementHasValue(text, "/example/classes/class[7]", "short");
assertElementHasValue(text, "/example/classes/class[8]", "boolean");
assertElementHasValue(text, "/example/classes/class[9]", "char");
}
}

0 comments on commit bb6b5ec

Please sign in to comment.