Code Monkey home page Code Monkey logo

xmappr's Introduction

Xmappr is a really simple way to handle XML in Java.

It's a lightweight library for mapping arbitrary XML to your Java classes, mapped via annotations or external config, understands XML namespaces, can cache unmapped elements, preserves order of XML elements, it's extensible with custom converters, it's thread-safe and is lightweight at under 80kb with no external dependencies. Oh, and it has a permissive license: BSD.

List of features:

  • Simple configuration. Object-to-XML mappings are configured via Java annotations or external XML configuration.
  • Partial mapping. Map only a part of XML document that you are interested in. Unmapped XML will be preserved on output.
  • XML namespaces are fully supported.
  • Preserves order of XML elements from input to output.
  • XML element and text mixing supported. Can map XHTML.
  • Extensible. Custom converters can be written to support custom type conversions.
  • Lightweight. Only 80kb in size with no dependencies.
  • Thread-safe. Designed for multi-thread use.
  • Permissive license: xmappr is released under BSD license.

Quick example

Let’s map some xml:

some text 123 to this class:
@RootElement
public class Root {

    @Attribute
    public float a;

    @Element
    public Integer node;

    @Text
    public String text;
}

All you have to do is add @Element, @Attribute and @Text annotations to your classes and configuration is done.

Mapping is than done in just two lines of code:

Xmappr xm = new Xmappr(Root.class);
Root root = (Root) xm.fromXML(reader);

If you don't like annotations you can configure mappings via XML configuration. For the given example it's:

<root-element name="root" class="package.name.Root">
  <attribute field="a"/>
  <element field="node"/>
  <text field="text"/>
</root-element>

Now head on to documentation.

xmappr's People

Contributors

peterknego avatar tobiaskarow avatar

Stargazers

 avatar  avatar Camilo Sacanamboy avatar Iulian avatar

Watchers

 avatar James Cloos avatar Iulian avatar

xmappr's Issues

Simplify ElementMapper

ElementMapper handles both Collection and simple fields as targets. Code
contains a lot of branching (if() clauses) to facilitate this. To unify the
code, remove targetType and elementConverter fields and all code that
depends on them. Replace them with itemTypes and converterCache collections
which already exist for Collections handling. Also check
AnnotationProcessor.processElements() method that sets this two fields and
replace accordingly.

Original issue reported on code.google.com by [email protected] on 28 Nov 2009 at 7:36

Maven repository deploy

Update pom.xml with automatic deploy/release scenarios.

Read:
http://www.thewebsemantic.com/2009/04/11/your-very-own-google-code-maven-repo/
http://www.jroller.com/mrdon/entry/find_of_the_day_wagon


Original issue reported on code.google.com by [email protected] on 27 Nov 2009 at 2:47

Modularize Xmappr

Partition Xmappr into several modules: Core, Examples, Benchmarks, Spring O/X.

SVN: reate module dirs under /trunk. 

Maven: create parent project that links together all child modules' pom.xml's.

Original issue reported on code.google.com by [email protected] on 28 Nov 2009 at 7:47

Remove all @XYZ mapping annotation references from MappingBuilder

MappingBuilder should be agnostic to configuration method: XML or annotations.

So there should be no references to concrete configuration method in this
class: basically this means removing all @XYZ annotation references.

All validation logic must be moved to ConfigurationProcessor validateXYZ()
methods.


Original issue reported on code.google.com by [email protected] on 24 Jan 2010 at 8:28

SubElement name="*" does not work

What is the expected output? What do you see instead?
class SubElement{
@Element(name="*", targetType=String.class) 
List numbers;
}
does not work, it regards numbers the type DomElement any way, while

class SubElement{
@Element(name="Date", targetType=String.class) 
List numbers;
} 
works.

What version of the product are you using? On what operating system?
0.93

Original issue reported on code.google.com by [email protected] on 15 Feb 2010 at 7:31

Allow 'converter' and/or 'targetType' attributes on wildcard mappings

This should be possible (currently it's not allowed):

@RootElement
class Root{

  @Element(name="*", converter=MyConverter.class, targetType=SomeType.class)
  List number;
}

Default behavior: if wildcard mapping @Element("*") does not define
'converter' AND 'targetType', then DomElementConverter is used as converter.

Rationale: if all subelements are of the same type then it's possible to
convert them with and assgned converter.

Original issue reported on code.google.com by [email protected] on 9 Dec 2009 at 11:41

CDATA mapping

Write converter that handles CDATA. Output should be byte[] or char[].

Original issue reported on code.google.com by [email protected] on 13 Dec 2009 at 10:02

UTF-8 Encoded files cause fromXML(stream) parser to fail.

What steps will reproduce the problem?
1. Build/Use a project that uses the XMappr.fromXML function
2. Identify the XML file in use. 
3. open the file in notepad++ and change the encoding to UTF-8. 
4. Do a file diff to see the special characters introduced by that encoding.  
5. run the testing project again and get the error: org.xmappr.XmapprException: 
Error reading XML stream: ParseError at [row,col]:[1,1]
6. change encoding back to ANSI and repeat test without failure.


Original issue reported on code.google.com by [email protected] on 19 Nov 2010 at 7:17

Non-uniform mapping

Currently mapping is done one-to-one between XML and java: one Java class
maps to one XML element. Meaning that Java classes and XML have same structure.

Create a way for Java and XML to be non-uniformly mapped. For example:

<customer>
  <contact>
    <firstname>Joe</firstname>
    <lastname>Somebody</lastname>
  </contact>
  <address>
    <street>Streetname</street>
    <city>Big City</city>
  </address>
</customer>

would be mapped to:

class Customer{ 
  String firstName;
  String lastName;
  String street;
  String city;
}

---------------------------
The other situation is reversed:

<customer>
    <firstname>Joe</firstname>
    <lastname>Somebody</lastname>
    <street>Streetname</street>
    <city>Big City</city>
</customer>

mapped to:

class Customer{ 
  Contact contact;
  Address address;
}

class Contact{
  String firstName;
  String lastName;
}

Class Address{
  String street;
  String city;
}


Original issue reported on code.google.com by [email protected] on 29 Nov 2009 at 11:58

Xmappr require 'getxxx' method for boolean field which should be 'isxxx'

What steps will reproduce the problem?
1. Define a boolean field in a Java bean
2. Annotate the boolean field with @Attribute
3. Run app with Xmappr

What version of the product are you using? On what operating system?
0.9.2(while 0.9.3 don't work at all)

Please provide any additional information below.
statck trace
org.xmappr.XmapprException: Could not find getter method for private field:
: org.hyk.sip.test.script.message.SendAction.getReliable()
    at org.xmappr.FieldAccessor.findAccessorMethod(FieldAccessor.java:84)
    at org.xmappr.FieldAccessor.<init>(FieldAccessor.java:26)
    at org.xmappr.mappers.AttributeMapper.<init>(AttributeMapper.java:29)
    at org.xmappr.MappingBuilder.processAttributes(MappingBuilder.java:475)
    at org.xmappr.MappingBuilder.createClassConverter(MappingBuilder.java:85)
    at org.xmappr.MappingContext.lookupElementConverter(MappingContext.java:162)
    at
org.xmappr.MappingBuilder.assignCollectionConverter(MappingBuilder.java:256)
    at org.xmappr.MappingBuilder.processElements(MappingBuilder.java:174)
    at org.xmappr.MappingBuilder.createClassConverter(MappingBuilder.java:89)
    at org.xmappr.MappingContext.lookupElementConverter(MappingContext.java:168)
    at org.xmappr.MappingBuilder.processConfiguration(MappingBuilder.java:37)
    at org.xmappr.MappingContext.addRootMapper(MappingContext.java:95)
    at org.xmappr.Xmappr.initialize(Xmappr.java:292)
    at org.xmappr.Xmappr.fromXML(Xmappr.java:129)
    at org.hyk.sip.test.HykSipUnitTestCase.proceed(HykSipUnitTestCase.java:75)
    at
org.hyk.sip.test.HykSipUnitTestCase.testSipMessage(HykSipUnitTestCase.java:100)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at junit.framework.TestCase.runTest(TestCase.java:164)
    at junit.framework.TestCase.runBare(TestCase.java:130)
    at junit.framework.TestResult$1.protect(TestResult.java:106)
    at junit.framework.TestResult.runProtected(TestResult.java:124)
    at junit.framework.TestResult.run(TestResult.java:109)
    at junit.framework.TestCase.run(TestCase.java:120)
    at junit.framework.TestSuite.runTest(TestSuite.java:230)
    at junit.framework.TestSuite.run(TestSuite.java:225)
    at
org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestR
eference.java:130)
    at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner
.java:467)
    at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner
.java:683)
    at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java
:390)
    at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.jav
a:197)
Caused by: java.lang.NoSuchMethodException:
org.hyk.sip.test.script.message.SendAction.getReliable()
    at java.lang.Class.getMethod(Unknown Source)
    at org.xmappr.FieldAccessor.findAccessorMethod(FieldAccessor.java:82)
    ... 33 more



Original issue reported on code.google.com by [email protected] on 4 Mar 2010 at 1:08

String holding tags changes to &lt; and &gt;

What steps will reproduce the problem?
1.set the value for a string attribute to be like <id><123></id> in an 
object test
2.add xmappr annotation
3.use xmapper's toXML method to convert the object "test" holding this 
attribute to string.

What is the expected output? What do you see instead?
excepted output: <id><1024></id>
what i see: &lt;id&gt;1024&lt;/id&gt;

What version of the product are you using? On what operating system?
0.9.2

Please provide any additional information below.


Original issue reported on code.google.com by [email protected] on 17 Feb 2010 at 9:39

Custom accessor methods

Xmappr should support custom getters/setters via annotations set on methods:

@RootElement("primitives")
    public static class Primitives {

        private int in;

        @Attribute
        public int getIn() {
            return in;
        }

        @Attribute
        public void setIn(int in) {
            this.in = in;
        }
}

This is also needed for Interface mappings.

Original issue reported on code.google.com by [email protected] on 25 Jan 2010 at 12:48

Mapping to parent class

Xmappr should support mappings where a field has a type of a parent class,
but concrete object assigned to this field are a children of fields's type:

@RootElement
public static class Root {

    @Elements({
            @Element(name = "first", targetType = FirstChild.class),
            @Element(name = "second", targetType = SecondChild.class)
     })
    public Parent parent;
}

Where FirstChild and SecondChild both extend Parent.

Child classes map to different XML elements (that can have different
structure).

TO-DO:
1. Enhance MappingBuilder to support this scenario.
2. Write test.

Original issue reported on code.google.com by [email protected] on 22 Jan 2010 at 9:36

JavaDoc for Tests

Write JavaDoc for every Test class: what's its purpose and how it does it.


Original issue reported on code.google.com by [email protected] on 28 Nov 2009 at 7:30

Spring O/X integration

Integrate Xmappr with Spring O/X mappers
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html
/ch14.html

Original issue reported on code.google.com by [email protected] on 28 Nov 2009 at 7:42

Checking type of given target object

When using xmappr.fromXml(reader, targetObject) it must be checked that
targetObject is of the right type:

Check in RootMapper that the configured ElementConverter produces type
assignable to targetObject type.

Original issue reported on code.google.com by [email protected] on 1 Dec 2009 at 11:21

Write javadoc for external API and important internal classes

JavaDoc for:

AttributeMapper, AnnotatedClassConverter, CollectionConverter,
DOMelementConverter, ElementMapper, FieldAccessor, RootMapper,
ValueConverterWrapper, AnnotationProcessor, MappingContext, NsContext,
ObjectStore, XMLSimpleReader, XMLSimpleWriter


Original issue reported on code.google.com by [email protected] on 28 Nov 2009 at 7:34

Wrong attribute defaults

Error: Attribute defaults are used when attributes are empty. 
Instead they should be used when attributes are missing. 

Original issue reported on code.google.com by [email protected] on 3 Dec 2009 at 9:32

Comment the code!

Code is not commented enough as per Ohloh.

Comment more the complex sections.


Original issue reported on code.google.com by [email protected] on 28 Nov 2009 at 7:37

Write architectural documentation

Write documentation that explains internal working of Xmappr: 

  # Mapping configuration parsing
  # Building of mapping tree
  # Mappers: attribute, element, text
  # Conveters: ValueConverter vs ElementConverter
  # Storing unmapped elements
  # XmlSimpleReader in XmlSimpleWriter etc..

Original issue reported on code.google.com by [email protected] on 28 Nov 2009 at 7:32

Benchmark

Add a Xmappr to http://code.google.com/p/thrift-protobuf-compare/

Original issue reported on code.google.com by [email protected] on 28 Nov 2009 at 7:39

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.