[jboss-svn-commits] JBL Code SVN: r32075 - labs/jbossesb/workspace/johan.kumps/product/rosetta/src/org/jboss/soa/esb/actions/transformation/xslt.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Sun Mar 14 17:57:40 EDT 2010


Author: johan.kumps
Date: 2010-03-14 17:57:40 -0400 (Sun, 14 Mar 2010)
New Revision: 32075

Added:
   labs/jbossesb/workspace/johan.kumps/product/rosetta/src/org/jboss/soa/esb/actions/transformation/xslt/DocumentFactory.java
Log:
Class encapsulating factory logic to create a Document instance out of the message payload

Added: labs/jbossesb/workspace/johan.kumps/product/rosetta/src/org/jboss/soa/esb/actions/transformation/xslt/DocumentFactory.java
===================================================================
--- labs/jbossesb/workspace/johan.kumps/product/rosetta/src/org/jboss/soa/esb/actions/transformation/xslt/DocumentFactory.java	                        (rev 0)
+++ labs/jbossesb/workspace/johan.kumps/product/rosetta/src/org/jboss/soa/esb/actions/transformation/xslt/DocumentFactory.java	2010-03-14 21:57:40 UTC (rev 32075)
@@ -0,0 +1,105 @@
+/*
+ * JBoss, Home of Professional Open Source Copyright 2009, Red Hat Middleware
+ * LLC, and individual contributors by the @authors tag. See the copyright.txt
+ * in the distribution for a full listing of individual contributors.
+ * 
+ * This 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 2.1 of the License, or (at your option)
+ * any later version.
+ * 
+ * This software 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 software; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
+ * site: http://www.fsf.org.
+ */
+package org.jboss.soa.esb.actions.transformation.xslt;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.Reader;
+import java.io.StringReader;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.w3c.dom.Document;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+/**
+ * Class encapsulating factory logic to create a Document instance out of the
+ * message payload.
+ * 
+ * @author Johan Kumps
+ * 
+ */
+public class DocumentFactory {
+	
+	/*
+	 * The singleton instance to use.
+	 */
+	private static final DocumentFactory INSTANCE = new DocumentFactory();
+
+	/**
+	 * Private constructor to make sure we use a singleton instance.
+	 */
+	private DocumentFactory() {
+	}
+
+	/**
+	 * Method used to get the singleton instance.
+	 * 
+	 * @return the singleton instance
+	 */
+	public static DocumentFactory getInstance() {
+		return INSTANCE;
+	}
+
+	/**
+	 * Method creating a Document instance based on the given message payload.
+	 * 
+	 * @param from
+	 *            the message payload
+	 * @return the created Document instance
+	 * @throws ParserConfigurationException
+	 * @throws SAXException
+	 * @throws IOException
+	 */
+	public Document createDocument(final Object from)
+			throws ParserConfigurationException, SAXException, IOException {
+		DocumentBuilder builder = DocumentBuilderFactory.newInstance()
+				.newDocumentBuilder();
+
+		final Document document;
+		if (from instanceof String) {
+			document = builder.parse(new InputSource(new StringReader(
+					(String) from)));
+		} else if (from instanceof byte[]) {
+			document = builder.parse(new ByteArrayInputStream((byte[]) from));
+		} else if (from instanceof Reader) {
+			document = builder.parse(new InputSource((Reader) from));
+		} else if (from instanceof InputStream) {
+			document = builder.parse((InputStream) from);
+		} else if (from instanceof File) {
+			document = builder.parse((File) from);
+		} else if (from instanceof Document) {
+			document = (Document) from;
+		} else {
+			throw new IllegalStateException(
+					"Object '"
+							+ from
+							+ "' is not of a supported type (String, byte[], Reader, InputStream, File, or Document).");
+		}
+		return document;
+	}
+
+}



More information about the jboss-svn-commits mailing list