Skip to content
This repository has been archived by the owner on Apr 13, 2024. It is now read-only.

Commit

Permalink
Merge pull request #22 from wso2-dev/master
Browse files Browse the repository at this point in the history
adding avro datam write pattern to format the output data
  • Loading branch information
lsdeva committed Apr 9, 2014
2 parents 070c5ea + 4323d22 commit ee72277
Show file tree
Hide file tree
Showing 6 changed files with 353 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2005,2014 WSO2, Inc. http://www.wso2.org
*
* Licensed 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.wso2.datamapper.engine.core.writer;

import java.io.IOException;

import org.apache.avro.Schema;
import org.apache.avro.generic.GenericDatumWriter;
import org.apache.avro.io.Encoder;
import org.apache.avro.generic.GenericRecord;
import org.json.CDL;
import org.json.JSONArray;
import org.json.JSONException;


public class CSVDatumWriter extends GenericDatumWriter<GenericRecord> {

@Override
protected void writeArray(Schema schema, Object datum, Encoder out)
throws IOException {
try {
JSONArray jsonArray = new JSONArray(datum.toString());
out.writeString(CDL.toString(jsonArray));
} catch (JSONException e) {
throw new IOException(e);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
* Copyright 2005,2014 WSO2, Inc. http://www.wso2.org
*
* Licensed 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.wso2.datamapper.engine.core.writer;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;

import org.apache.avro.io.Encoder;
import org.apache.avro.util.Utf8;

public class DummyEncoder extends Encoder {

OutputStream outputStream ;

public DummyEncoder(OutputStream outputStream) {
this.outputStream = outputStream;
}

public void flush() throws IOException {
outputStream.flush();

}

@Override
public void setItemCount(long arg0) throws IOException {
// TODO Auto-generated method stub

}

@Override
public void startItem() throws IOException {
// TODO Auto-generated method stub

}

@Override
public void writeArrayEnd() throws IOException {
// TODO Auto-generated method stub

}

@Override
public void writeArrayStart() throws IOException {
// TODO Auto-generated method stub

}

@Override
public void writeBoolean(boolean arg0) throws IOException {
// TODO Auto-generated method stub

}

@Override
public void writeBytes(ByteBuffer arg0) throws IOException {
// TODO Auto-generated method stub

}

@Override
public void writeBytes(byte[] arg0, int arg1, int arg2) throws IOException {
// TODO Auto-generated method stub

}

@Override
public void writeDouble(double arg0) throws IOException {
// TODO Auto-generated method stub

}

@Override
public void writeEnum(int arg0) throws IOException {
// TODO Auto-generated method stub

}

@Override
public void writeFixed(byte[] arg0, int arg1, int arg2) throws IOException {
// TODO Auto-generated method stub

}

@Override
public void writeFloat(float arg0) throws IOException {
// TODO Auto-generated method stub

}

@Override
public void writeIndex(int arg0) throws IOException {
// TODO Auto-generated method stub

}

@Override
public void writeInt(int arg0) throws IOException {
// TODO Auto-generated method stub

}

@Override
public void writeLong(long arg0) throws IOException {
// TODO Auto-generated method stub

}

@Override
public void writeMapEnd() throws IOException {
// TODO Auto-generated method stub

}

@Override
public void writeMapStart() throws IOException {
// TODO Auto-generated method stub

}

@Override
public void writeNull() throws IOException {
// TODO Auto-generated method stub

}

@Override
public void writeString(Utf8 data) throws IOException {
outputStream.write(data.toString().getBytes());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2005,2014 WSO2, Inc. http://www.wso2.org
*
* Licensed 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.wso2.datamapper.engine.core.writer;

import java.util.HashMap;
import java.util.Map;

import org.apache.avro.generic.GenericRecord;
import org.apache.avro.io.DatumWriter;

/**
* A registry of writer.
*/
public class WriterRegistry {

/**
* Singleton instance.
*/
private static WriterRegistry singleton;


/**
* writer map.
*/
private Map<String, Class<? extends DatumWriter<GenericRecord>>> writerMap;

/**
*
*/
private WriterRegistry() {
writerMap = new HashMap<String, Class<? extends DatumWriter<GenericRecord>>>();

// FIXME : use java service provider interface rather than hard-coding class names/ importing classes
writerMap.put("text/csv",CSVDatumWriter.class);
writerMap.put("application/xml", XMLDatumWriter.class);
}

/**
* @return singleton instance.
*/
public static WriterRegistry getInstance() {
if (null == singleton) {
singleton = new WriterRegistry();
}
return singleton;
}

@SuppressWarnings("unchecked")
public Class<DatumWriter<GenericRecord>> get(String mediaType){
Class<DatumWriter<GenericRecord>> writer = null;
if(writerMap.containsKey(mediaType)){
writer = (Class<DatumWriter<GenericRecord>>) writerMap.get(mediaType);
} else {
throw new RuntimeException("No writer found for " + mediaType);
}
//FIXME: use proper error handling
return writer;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2005,2014 WSO2, Inc. http://www.wso2.org
*
* Licensed 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.wso2.datamapper.engine.core.writer;

import java.io.IOException;

import org.apache.avro.Schema;
import org.apache.avro.generic.GenericDatumWriter;
import org.apache.avro.io.Encoder;
import org.apache.avro.generic.GenericRecord;
import org.json.XML;
import org.json.JSONException;
import org.json.JSONObject;

public class XMLDatumWriter extends GenericDatumWriter<GenericRecord> {


@Override
protected void writeRecord(Schema schema, Object datum, Encoder out)
throws IOException {
try {
GenericRecord record = (GenericRecord) datum;
String name = record.getSchema().getName();
JSONObject rootObj = new JSONObject();
rootObj.putOnce(name, new JSONObject(record.toString()));
out.writeString(XML.toString(rootObj));
} catch (JSONException e) {
throw new IOException(e);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
package org.wso2.datamapper.engine.sample;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.apache.avro.generic.GenericRecord;
import org.apache.avro.io.Encoder;
import org.apache.avro.io.DatumWriter;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMXMLBuilderFactory;
import org.apache.axiom.om.OMXMLParserWrapper;
import org.json.JSONException;
import org.json.JSONObject;
import org.wso2.datamapper.engine.core.MappingHandler;
import org.wso2.datamapper.engine.core.MappingResourceLoader;
import org.wso2.datamapper.engine.utils.OutputJsonBuilder;
import org.wso2.datamapper.engine.core.writer.DummyEncoder;
import org.wso2.datamapper.engine.core.writer.WriterRegistry;
/**
* This is a Test class will be removed in the future
* @author jasintha
*
*/
public class MapperMain {
Expand All @@ -36,14 +38,38 @@ public static void main(String[] args) throws FileNotFoundException, JSONExcepti
OMElement documentElement = builder.getDocumentElement();

GenericRecord result = MappingHandler.doMap(documentElement,configModel);


ByteArrayOutputStream baos = new ByteArrayOutputStream();

OutputJsonBuilder outJsonBuilder = new OutputJsonBuilder();
JSONObject resultJson = outJsonBuilder.getOutPut(result, configModel.getOutputRootelement());

System.out.println(resultJson.toString());
inStream.close();
//CSV : text/csv
//XML : application/xml

DatumWriter<GenericRecord> writer = WriterRegistry.getInstance().get("text/csv").newInstance();
writer.setSchema(result.getSchema());
Encoder encoder = new DummyEncoder(baos);

writer.write(result, encoder);
encoder.flush();

//Print CSV
System.out.println("-- CSV -- \n");
System.out.println(baos.toString());


baos = new ByteArrayOutputStream();
writer = WriterRegistry.getInstance().get("application/xml").newInstance();
writer.setSchema(result.getSchema());
encoder = new DummyEncoder(baos);

writer.write(result, encoder);
encoder.flush();

//Print XML
System.out.println("\n\n-- XML -- \n");
System.out.println(baos.toString());

inStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Expand Down
Loading

0 comments on commit ee72277

Please sign in to comment.