From a18cf2f77a59d957dbfb9f01ccb52e4eeb68e2c4 Mon Sep 17 00:00:00 2001 From: Imran M Yousuf Date: Sat, 19 Sep 2009 16:34:55 +0700 Subject: [PATCH] Add XML export implementation with the association type implementation The XML export implementation will be complete if and when all the element export is complete. This change adds the element exporter API and please note that while implementing this API might change. Signed-off-by: Imran M Yousuf --- .../exim/impl/xml/ElementExporter.java | 49 +++++++++ .../exim/impl/xml/XMLExporterImpl.java | 116 ++++++++++++++++++++- 2 files changed, 162 insertions(+), 3 deletions(-) create mode 100644 smart-exim/smart-exim-xml-impl/src/main/java/com/smartitengineering/exim/impl/xml/ElementExporter.java diff --git a/smart-exim/smart-exim-xml-impl/src/main/java/com/smartitengineering/exim/impl/xml/ElementExporter.java b/smart-exim/smart-exim-xml-impl/src/main/java/com/smartitengineering/exim/impl/xml/ElementExporter.java new file mode 100644 index 0000000..e618fb6 --- /dev/null +++ b/smart-exim/smart-exim-xml-impl/src/main/java/com/smartitengineering/exim/impl/xml/ElementExporter.java @@ -0,0 +1,49 @@ +/* + * This is a common dao with basic CRUD operations and is not limited to any + * persistent layer implementation + * + * Copyright (C) 2009 Imran M Yousuf (imyousuf@smartitengineering.com) + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +package com.smartitengineering.exim.impl.xml; + +import com.smartitengineering.exim.AssociationConfig.AssociationType; +import java.io.IOException; +import java.util.Collection; +import javax.xml.transform.sax.TransformerHandler; + +/** + * An interface for different types of association exporter implementation. All + * its implementation should be designed in singleton thread safe way. + * @author imyousuf + * @since 0.4 + */ +public interface ElementExporter { + + /** + * This operation is resposnsible for exporting XML elements of type it + * supports. It will use other type exporters for exporting and will only be + * responsible for the types it supports. + * @param type Type to export + * @param object Object to export + * @param handler Handler to use to create elements and attributes + * @throws IOException If any error while exporting + */ + public void exportElement(final AssociationType type, + final Object object, + final TransformerHandler handler) + throws IOException; + + public Collection getSupportedTypes(); +} diff --git a/smart-exim/smart-exim-xml-impl/src/main/java/com/smartitengineering/exim/impl/xml/XMLExporterImpl.java b/smart-exim/smart-exim-xml-impl/src/main/java/com/smartitengineering/exim/impl/xml/XMLExporterImpl.java index bbcdf40..220182c 100644 --- a/smart-exim/smart-exim-xml-impl/src/main/java/com/smartitengineering/exim/impl/xml/XMLExporterImpl.java +++ b/smart-exim/smart-exim-xml-impl/src/main/java/com/smartitengineering/exim/impl/xml/XMLExporterImpl.java @@ -2,7 +2,7 @@ * This is a common dao with basic CRUD operations and is not limited to any * persistent layer implementation * - * Copyright (C) 2008 Imran M Yousuf (imyousuf@smartitengineering.com) + * Copyright (C) 2009 Imran M Yousuf (imyousuf@smartitengineering.com) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -22,20 +22,71 @@ import com.smartitengineering.exim.AssociationConfig.AssociationType; import com.smartitengineering.exim.ConfigRegistrar; import com.smartitengineering.exim.EximResourceConfig; import com.smartitengineering.exim.Exporter; +import com.smartitengineering.util.simple.IOFactory; +import com.smartitengineering.util.simple.reflection.ClassInstanceVisitorImpl; +import com.smartitengineering.util.simple.reflection.ClassScanner; +import com.smartitengineering.util.simple.reflection.Config; +import com.smartitengineering.util.simple.reflection.VisitCallback; import java.io.IOException; import java.io.OutputStream; import java.lang.annotation.Annotation; import java.lang.reflect.Type; import java.util.List; import java.util.Map; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.sax.SAXTransformerFactory; +import javax.xml.transform.sax.TransformerHandler; +import javax.xml.transform.stream.StreamResult; +import org.xml.sax.SAXException; /** - * Hello world! * + * @author imyousuf + * @since 0.4 */ public class XMLExporterImpl implements Exporter { + private String encoding; + private String indent; + private String method; + private static Map handlers; + + static { + String packageName = ConfigRegistrar.class.getPackage().getName(); + ClassScanner scanner = IOFactory.getDefaultClassScanner(); + scanner.scan(new String[] {packageName}, + new ClassInstanceVisitorImpl(IOFactory.getClassNameForVisitor( + ElementExporter.class), new VisitCallback() { + + public void handle(Config config) { + try { + IOFactory.getClassFromVisitorName(config.getClassName()); + } + catch (Exception ex) { + ex.printStackTrace(); + } + } + })); + } + + static void addHandler(AssociationType type, + ElementExporter exporter) { + handlers.put(type, exporter); + } + + static ElementExporter getElementExporter(AssociationType type) { + return handlers.get(type); + } + + public XMLExporterImpl() { + encoding = "UTF-8"; + indent = "yes"; + method = "xml"; + } + public boolean isExportSupported(Class clazz, Type genericType, Annotation[] annotations) { @@ -69,13 +120,72 @@ public class XMLExporterImpl OutputStream outputStream, Map> headers) throws IOException { - throw new UnsupportedOperationException("Not supported yet."); + if (outputStream == null || clazz == null || object == null) { + throw new IOException( + "OutputStream or Object or Class can't be null!"); + } + try { + SAXTransformerFactory factory = + (SAXTransformerFactory) SAXTransformerFactory. + newInstance(); + TransformerHandler handler = factory.newTransformerHandler(); + StreamResult result = new StreamResult(outputStream); + Transformer transformer = handler.getTransformer(); + transformer.setOutputProperty(OutputKeys.ENCODING, encoding); + transformer.setOutputProperty(OutputKeys.INDENT, indent); + transformer.setOutputProperty(OutputKeys.METHOD, method); + handler.setResult(result); + handler.startDocument(); + try { + AssociationType type = AssociationType.getAssociationType(clazz); + ElementExporter exporter = getElementExporter(type); + if (exporter == null) { + throw new UnsupportedOperationException("Type not supported yet! - " + + type); + } + exporter.exportElement(type, object, handler); + + } + finally { + handler.endDocument(); + } + } + catch (SAXException ex) { + throw new IOException(ex); + } + catch (TransformerConfigurationException ex) { + throw new IOException(ex); + } } public String getMediaType() { return "application/xml"; } + public String getEncoding() { + return encoding; + } + + public void setEncoding(String encoding) { + this.encoding = encoding; + } + + public String getIndent() { + return indent; + } + + public void setIndent(String indent) { + this.indent = indent; + } + + public String getMethod() { + return method; + } + + public void setMethod(String method) { + this.method = method; + } + private Class getComponentClass(Class clazz) { if (clazz == null) { throw new IllegalArgumentException(); -- 2.11.4.GIT