Table Of Contents

Previous topic

IgorPro

Next topic

PHP

This Page

Java JAXB

A Java binding for the cansas1d/1.1 standard has been auto-created using the JAXB tools from Sun (see below for more on JAXB) using the cansas1d.xsd XML Schema. Resources (JAR files and documentation) for the Java binding may be found in the canSAS subversion repository: http://www.cansas.org/svn/1dwg/tags/v1.0/java

Subversion repository

http://www.cansas.org/svn/1dwg/tags/v1.0/java

canSAS subversion repository tagged release directory for the Java binding. Use resources from this directory in your development projects.

cansas1d-1.0.jar

http://www.cansas.org/svn/1dwg/tags/v1.0/java/cansas1d-1.0.jar

JAR file to add to your CLASSPATH in order to use this binding.

cansas1d-1.0-javadoc.jar

http://www.cansas.org/svn/1dwg/tags/v1.0/java/cansas1d-1.0-javadoc.jar

Use this JAR file if you want to add the javadoc documentation as tooltips to your editor, such as eclipse. (auto-generated from the project source code using maven2) Note that this file is compatible with any ZIP program and can be unzipped to provide a directory with all the documentation as a set of HTML pages. Start with the index.html page.

cansas1d-1.0-sources.jar

http://www.cansas.org/svn/1dwg/tags/v1.0/java/cansas1d-1.0-sources.jar

JAR file of the source code. (auto-generated from the project source code using maven2) Note that this is just the source code tree and not the full project development tree for the Java (JAXB) API.

cansas1d-1.0.pdf

http://www.cansas.org/svn/1dwg/tags/v1.0/java/cansas1d-1.0.pdf

PDF file of the javadoc source code documentation. (auto-generated from the project source code using pdfdoclet)

source code (for developers)

http://www.cansas.org/trac/browser/1dwg/trunk/java/maven/eclipse

canSAS Development project subversion repository for the Java binding. Only use this if you want to participate as a code developer of this binding.

Example_canSAS_Reader.java: example usage in JAVA

An example (Example_canSAS_Reader.java) has been constructed to show how to read a cansas1d/1.1 XML file using the Java API.

http://www.cansas.org/trac/browser/1dwg/trunk/java/maven/eclipse/src/main/java/org/scatteringsw/reader/Example_canSAS_Reader.java

In short, these are the important two lines:

CanSas1dType reader = new CanSas1dType();

and

SASrootType sasRoot = reader.open(xmlFile);

where String xmlFile; is the name of the XML file to be read. You will also need these imports:

1
2
3
4
5
6
7
import javax.xml.bind.JAXBException;

import net.smallangles.cansas1d.CanSas1dType;
import net.smallangles.cansas1d.SASdataType;
import net.smallangles.cansas1d.SASentryType;
import net.smallangles.cansas1d.SASrootType;
import net.smallangles.cansas1d.SASentryType.Run;

Also, since CanSas1dType.open(xmlFile) can throw a JAXBException, you should use a try{} catch {} clause. See the source code for the example.

Here is a Java class that shows how to use the JAXB binding. Use this with any of the test data supplied with the cansas-1d-standard directory (above). By default, it shows the two samples in the 1998spheres.xml example file. (http://www.cansas.org/trac/browser/1dwg/trunk/1998spheres.xml)

Note

The reader will have to get the directory paths right until this documentation improves.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package org.scatteringsw.reader;

//########### SVN repository information ###################
//# $Date: 2012-11-01 17:03:20 -0500 (Thu, 01 Nov 2012) $
//# $Author: prjemian $
//# $Revision: 263 $
//# $URL: http://www.cansas.org/svn/1dwg/trunk/doc/source/examples/Example_canSAS_Reader.java $
//# $Id: Example_canSAS_Reader.java 263 2012-11-01 22:03:20Z prjemian $
//########### SVN repository information ###################

import java.util.List;

import javax.xml.bind.JAXBException;

import net.smallangles.cansas1d.CanSas1dType;
import net.smallangles.cansas1d.SASdataType;
import net.smallangles.cansas1d.SASentryType;
import net.smallangles.cansas1d.SASrootType;
import net.smallangles.cansas1d.SASentryType.Run;

/**
 * Demonstrate how to use the Java binding for the 
 * cansas1d/1.0 standard XML format for reduced 
 * small-angle scattering data.
 * 
 */
public class Example_canSAS_Reader {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		CanSas1dType reader = new CanSas1dType();

		try {
			// open the XML file and load contents into a Java data structure
			SASrootType sasRoot = reader.open("java-test.xml");

			// canSAS XML file is now loaded in memory
			int numEntries = sasRoot.getSASentry().size();
			System.out.println("SASentry elements: " + numEntries);

			for (int i = 0; i < numEntries; i++ ) {
				System.out.println("SASentry");

				SASentryType sasEntry = sasRoot.getSASentry().get(i);
				System.out.printf("Title:\t%s\n", sasEntry.getTitle());

				List<SASentryType.Run> runs = sasEntry.getRun();
				System.out.printf("#Runs:\t%d\n", runs.size());

				for( int j = 0; j < runs.size(); j++ ) {
					Run run = (Run) runs.get(j);
					System.out.printf("Run@name:\t%s\n", run.getName());
					System.out.printf("Run:\t%s\n", run.getValue());
				}

				List<SASdataType> datasets = sasEntry.getSASdata();
				System.out.printf("#SASdata:\t%d\n", sasEntry.getSASdata().size());

				for( int j = 0; j < runs.size(); j++ ) {
					SASdataType sasData = (SASdataType) datasets.get(j);
					System.out.printf("SASdata@name:\t%s\n", sasData.getName());
					System.out.printf("#points:\t%d\n", sasData.getIdata().size());
				}
				System.out.println();
			}

			System.out.println("the end.");

		} catch (JAXBException e) {
			e.printStackTrace();
			System.out.printf("Could not open (unmarshall) XML file: %s\n", xmlFile);
		}
	}

}

example: how to retrieve \(I(Q)\)

This is a slightly longer example. Look near line 75 for this code:

Qsas[i] = sdt.getIdata().get(i).getQ().getValue();

to see the operations that unwind the data into usable double[] vectors. Pretty straightforward although there is lots of interesting, yet unnecessary, diagnostic output. Here is a table that describes the items in the line just shown:

java item description
sdt SASdataType object
getIdata() amongst the /SASdata/Idata tuples ...
get(i) ... pick the Idata tuple from row i.
getQ() Just the /SASdata/Idata/Q
getValue() and specifically the value, not the unit

GetSASdata.java

Since the source code is rather lengthy, download it from: http://www.cansas.org/svn/1dwg/trunk/doc/src/GetSASdata.java

java-test.xml

java-test.xml is an example cansas1d/1.1 XML data file (derived from the standard test file for the lake desmearing code).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="example.xsl" ?>
<!-- 
    ########### SVN repository information ###################
    # $Date: 2012-11-17 18:03:04 -0600 (Sat, 17 Nov 2012) $
    # $Author: prjemian $
    # $Revision: 273 $
    # $HeadURL: http://www.cansas.org/svn/1dwg/trunk/doc/source/examples/java-test.xml $
    # $Id: java-test.xml 273 2012-11-18 00:03:04Z prjemian $
    ########### SVN repository information ###################
-->
<SASroot version="1.1"
	xmlns="urn:cansas1d:1.1"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="urn:cansas1d:1.1 
	    http://www.cansas.org/formats/1.1/cansas1d.xsd"
    >
    <SASentry>
        <Title>(severely abbreviated version of the) standard test dataset for Lake desmearing routine</Title>
        <Run>Run</Run>
        <SASdata name="slit-smeared">
            <Idata><Q unit="1/A">0.000371484</Q><I unit="1/cm">211554</I><Idev unit="1/cm">1874.86</Idev></Idata>
            <Idata><Q unit="1/A">0.000510348</Q><I unit="1/cm">172441</I><Idev unit="1/cm">505.368</Idev></Idata>
            <Idata><Q unit="1/A">0.0006516</Q><I unit="1/cm">139041</I><Idev unit="1/cm">373.826</Idev></Idata>
            <Idata><Q unit="1/A">0.000798956</Q><I unit="1/cm">110665</I><Idev unit="1/cm">299.932</Idev></Idata>
            <Idata><Q unit="1/A">0.000931629</Q><I unit="1/cm">91165.3</I><Idev unit="1/cm">255.102</Idev></Idata>
            <Idata><Q unit="1/A">0.00107907</Q><I unit="1/cm">73855.7</I><Idev unit="1/cm">218.547</Idev></Idata>
            <Idata><Q unit="1/A">0.00121794</Q><I unit="1/cm">60799.2</I><Idev unit="1/cm">191.38</Idev></Idata>
            <Idata><Q unit="1/A">0.00168963</Q><I unit="1/cm">33573.8</I><Idev unit="1/cm">130.321</Idev></Idata>
            <Idata><Q unit="1/A">0.00227932</Q><I unit="1/cm">17602</I><Idev unit="1/cm">82.5591</Idev></Idata>
            <Idata><Q unit="1/A">0.00325199</Q><I unit="1/cm">7394.04</I><Idev unit="1/cm">50.0284</Idev></Idata>
            <Idata><Q unit="1/A">0.00608234</Q><I unit="1/cm">1305.37</I><Idev unit="1/cm">3.52884</Idev></Idata>
            <Idata><Q unit="1/A">0.00891261</Q><I unit="1/cm">464.949</I><Idev unit="1/cm">2.1601</Idev></Idata>
            <Idata><Q unit="1/A">0.0113893</Q><I unit="1/cm">241.462</I><Idev unit="1/cm">1.7191</Idev></Idata>
            <Idata><Q unit="1/A">0.0151631</Q><I unit="1/cm">126.664</I><Idev unit="1/cm">1.46166</Idev></Idata>
            <Idata><Q unit="1/A">0.0264845</Q><I unit="1/cm">53.7715</I><Idev unit="1/cm">1.27858</Idev></Idata>
            <Idata><Q unit="1/A">0.0378057</Q><I unit="1/cm">43.2662</I><Idev unit="1/cm">1.25507</Idev></Idata>
            <Idata><Q unit="1/A">0.0472402</Q><I unit="1/cm">39.9715</I><Idev unit="1/cm">1.25581</Idev></Idata>
            <Idata><Q unit="1/A">0.0566745</Q><I unit="1/cm">38.9685</I><Idev unit="1/cm">1.25924</Idev></Idata>
            <Idata><Q unit="1/A">0.0996003</Q><I unit="1/cm">38.8772</I><Idev unit="1/cm">1.27929</Idev></Idata>
            <Idata><Q unit="1/A">0.144883</Q><I unit="1/cm">37.5137</I><Idev unit="1/cm">1.30765</Idev></Idata>
            <Idata><Q unit="1/A">0.190162</Q><I unit="1/cm">39.1407</I><Idev unit="1/cm">1.33442</Idev></Idata>
            <Idata><Q unit="1/A">0.224119</Q><I unit="1/cm">38.589</I><Idev unit="1/cm">1.35581</Idev></Idata>
        </SASdata>
        <SASsample>
            <ID>ID</ID>
        </SASsample>
        <SASinstrument>
            <name>calculated</name>
            <SASsource>
                <radiation>model</radiation>
            </SASsource>
            <SAScollimation/>
            <SASdetector>
                <name>calculated</name>
                <slit_length unit="1/A">0.08</slit_length>
            </SASdetector>
        </SASinstrument>
        <SASnote/>
    </SASentry>
</SASroot>

JAXB: Questions and Answers

Q:

What is JAXB?

A:

Java Architecture for XML Binding (http://java.sun.com/developer/technicalArticles/WebServices/jaxb)

Q:

Wow! Is it available for other languages?

A:

Ask Google. JAXB is for Java. (http://java.sun.com/developer/technicalArticles/WebServices/jaxb)

For example: http://www.devx.com/ibm/Article/20261

Q:How do I pull out the \(I(Q)\) data?
A:See Java code fragment above that gets data for desmearing. (java-test.xml)
Q:Has JAXB been useful?
A:Very useful. Since an XML Schema was defined, JAXB was very useful to create a Java binding automatically. Then, javadoc was able to auto-generate the basic documentation as HTML and pdfdoclet was able to auto-generate the documentation in a PDF file.