-
Notifications
You must be signed in to change notification settings - Fork 16
Usage Filemaker
Filemaker specifies two types of XML documents:
-
A document with a
<FMPXMLRESULT>
root element contains database contents and structural informations about Filemaker database. This type of document is represented by theorg.openestate.io.filemaker.FilemakerResultDocument
class. -
A document with a
<FMPXMLLAYOUT>
root element contains structural informations about Filemaker database. This type of document is represented by theorg.openestate.io.filemaker.FilemakerLayoutDocument
class.
Both document classes are extending the general / abstract
org.openestate.io.filemaker.FilemakerDocument
class.
The class org.openestate.io.filemaker.FilemakerUtils
provides a static
createDocument()
function to read XML data in Filemaker format from a
java.io.File
, java.io.InputStream
, java.lang.String
or
org.w3c.dom.Document
into a
org.openestate.io.filemaker.FilemakerDocument
.
The createDocument()
function returns either a
org.openestate.io.filemaker.FilemakerResultDocument
or a
org.openestate.io.filemaker.FilemakerLayoutDocument
- depending on the type of
Filemaker document.
import java.io.File;
import org.apache.commons.lang3.StringUtils;
import org.openestate.io.filemaker.FilemakerResultDocument;
import org.openestate.io.filemaker.FilemakerUtils;
import org.openestate.io.filemaker.xml.FMPXMLRESULT;
public static void main( String[] args )
{
if (args.length<1)
{
System.err.println( "No file was specified!" );
System.exit( 1 );
}
// read file into a FilemakerResultDocument
FilemakerResultDocument doc = (FilemakerResultDocument)
FilemakerUtils.createDocument( new File( args[0] ) );
if (doc==null)
{
System.err.println( "Can't read XML file!" );
System.exit( 1 );
}
// convert FilemakerResultDocument into a Java object
FMPXMLRESULT result = doc.toObject();
// now we can access the XML content through ordinary Java objects
System.out.println( "> error code : " + result.getERRORCODE() );
if (result.getPRODUCT()!=null)
{
System.out.println( "> product name : " + result.getPRODUCT().getNAME() );
System.out.println( "> product version : " + result.getPRODUCT().getVERSION() );
System.out.println( "> product build : " + result.getPRODUCT().getBUILD() );
}
if (result.getDATABASE()!=null)
{
System.out.println( "> database name : " + result.getDATABASE().getNAME() );
System.out.println( "> database layout : " + result.getDATABASE().getLAYOUT() );
System.out.println( "> database date : " + result.getDATABASE().getDATEFORMAT() );
System.out.println( "> database time : " + result.getDATABASE().getTIMEFORMAT() );
System.out.println( "> database records : " + result.getDATABASE().getRECORDS() );
}
if (result.getMETADATA()!=null)
{
for (MetaDataType.FIELD field : result.getMETADATA().getFIELD())
{
System.out.println( "> database field : " + field.getNAME() );
System.out.println( ">> type : " + field.getTYPE() );
System.out.println( ">> max repeat : " + field.getMAXREPEAT() );
}
}
if (result.getRESULTSET()!=null)
{
System.out.println( "> resultset found : " + result.getRESULTSET().getFOUND() );
for (ResultSetType.ROW row : result.getRESULTSET().getROW())
{
System.out.println( "> resultset row : " + row.getRECORDID() + " / " + row.getMODID() );
for (ResultSetType.ROW.COL col : row.getCOL())
{
for (String data : col.getDATA())
{
System.out.println( ">> " + data );
}
}
}
}
}
See a full example at FilemakerReadingExample.java
.
The class org.openestate.io.filemaker.xml.FMPXMLRESULT
is equivalent to a
<FMPXMLRESULT>
root element in a FMPXMLRESULT document. For example the
following code creates a FMPXMLRESULT document programmatically:
import java.math.BigInteger;
import org.apache.commons.lang3.StringUtils;
import org.openestate.io.filemaker.FilemakerResultDocument;
import org.openestate.io.filemaker.FilemakerUtils;
import org.openestate.io.filemaker.xml.result.DatabaseType;
import org.openestate.io.filemaker.xml.result.FMPXMLRESULT;
import org.openestate.io.filemaker.xml.result.FieldType;
import org.openestate.io.filemaker.xml.result.MetaDataType;
import org.openestate.io.filemaker.xml.result.ObjectFactory;
import org.openestate.io.filemaker.xml.result.ProductType;
import org.openestate.io.filemaker.xml.result.ResultSetType;
public static void main( String[] args )
{
// create a FMPXMLRESULT object with some example data
// this object corresponds to the <FMPXMLRESULT> root element in XML
FMPXMLRESULT result = FilemakerUtils.getFactoryForResult().createFMPXMLRESULT();
result.setERRORCODE( "0" );
result.setPRODUCT( createProduct() );
result.setDATABASE( createDatabase() );
result.setMETADATA( createMetaData() );
result.setRESULTSET( createResultSet() );
// now make something useful with the object
}
protected static DatabaseType createDatabase()
{
DatabaseType database = FilemakerUtils.getFactoryForResult().createDatabaseType();
database.setNAME( "example database" );
database.setLAYOUT( "fmmedia2universal" );
database.setDATEFORMAT( "D.m.yyyy" );
database.setTIMEFORMAT( "k:mm:ss" );
database.setRECORDS( BigInteger.ZERO );
return database;
}
protected static MetaDataType createMetaData()
{
MetaDataType metadata = FilemakerUtils.getFactoryForResult().createMetaDataType();
MetaDataType.FIELD field;
field = FACTORY.createMetaDataTypeFIELD();
field.setNAME( "number of rooms" );
field.setEMPTYOK( true );
field.setMAXREPEAT( BigInteger.ONE );
field.setTYPE( FieldType.NUMBER );
metadata.getFIELD().add( field );
field = FACTORY.createMetaDataTypeFIELD();
field.setNAME( "price" );
field.setEMPTYOK( false );
field.setMAXREPEAT( BigInteger.ONE );
field.setTYPE( FieldType.NUMBER );
metadata.getFIELD().add( field );
field = FACTORY.createMetaDataTypeFIELD();
field.setNAME( "description" );
field.setEMPTYOK( true );
field.setMAXREPEAT( BigInteger.ONE );
field.setTYPE( FieldType.TEXT );
metadata.getFIELD().add( field );
return metadata;
}
protected static ProductType createProduct()
{
ProductType product = FilemakerUtils.getFactoryForResult().createProductType();
product.setNAME( "OpenEstate-IO" );
product.setVERSION( "1.2-SNAPSHOT" );
product.setBUILD( "123" );
return product;
}
protected static ResultSetType createResultSet()
{
ResultSetType result = FilemakerUtils.getFactoryForResult().createResultSetType();
result.getROW().add( createResultSetRow( 1, 3, 100, "a first example" ) );
result.getROW().add( createResultSetRow( 2, null, 200, "a second example" ) );
result.getROW().add( createResultSetRow( 3, 5, 300, null ) );
result.setFOUND( BigInteger.valueOf( result.getROW().size() ) );
return result;
}
protected static ResultSetType.ROW createResultSetRow( long id, Number numberOfRooms, Number price, String description )
{
ResultSetType.ROW.COL col;
ResultSetType.ROW row = FilemakerUtils.getFactoryForResult().createResultSetTypeROW();
row.setRECORDID( BigInteger.valueOf( id ) );
row.setMODID( BigInteger.valueOf( id ) );
col = FACTORY.createResultSetTypeROWCOL();
if (numberOfRooms!=null)
col.getDATA().add( StringUtils.EMPTY );
else
col.getDATA().add( String.valueOf( numberOfRooms ) );
row.getCOL().add( col );
col = FACTORY.createResultSetTypeROWCOL();
if (price!=null)
col.getDATA().add( "0" );
else
col.getDATA().add( String.valueOf( price ) );
row.getCOL().add( col );
col = FACTORY.createResultSetTypeROWCOL();
col.getDATA().add( StringUtils.trimToEmpty( description ) );
row.getCOL().add( col );
return row;
}
See a full example at FilemakerWritingExample.java
.
After a org.openestate.io.filemaker.xml.FMPXMLRESULT
object was created, it
can be converted into a org.openestate.io.filemaker.FilemakerResultDocument
with the static newDocument()
function.
The class org.openestate.io.filemaker.FilemakerResultDocument
provides a
toXml()
function, that finally writes the contents of the FMPXMLRESULT
object as XML into a java.io.File
, java.io.OutputStream
or java.io.Writer
.
import java.io.File;
import org.openestate.io.filemaker.FilemakerResultDocument;
import org.openestate.io.filemaker.xml.result.FMPXMLRESULT;
protected static void write( FMPXMLRESULT result, File file )
{
try
{
FilemakerResultDocument doc = FilemakerResultDocument.newDocument( result );
doc.toXml( file );
}
catch (Exception ex)
{
System.err.println( "Can't write document into a file!" );
System.err.println( "> " + ex.getLocalizedMessage(), ex );
System.exit( 1 );
}
}
See a full example at FilemakerWritingExample.java
.