All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.milyn.payload.JavaSource Maven / Gradle / Ivy

There is a newer version: 1.7.1
Show newest version
/*
	Milyn - Copyright (C) 2006 - 2010

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License (version 2.1) as published by the Free Software
	Foundation.

	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:
	http://www.gnu.org/licenses/lgpl.txt
*/
package org.milyn.payload;

import org.milyn.assertion.AssertArgument;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

/**
 * Java Filtration/Transformation {@link javax.xml.transform.Source}.
 * 
 * 

Controlling Event Stream Generation

* The Java Objects used to create this source can be used to generate a stream of SAX * events, which can then be analysed by Smooks in the normal manner. Generation of the * SAX event stream can also be turned off via the {@link #setEventStreamRequired(boolean)} * method, or by configuring the 'http://www.smooks.org/sax/features/generate-java-event-stream' * feature on the reader in the Smoooks configuration as follows: * *
 * <reader>
 *     <features>
 *         <setOff feature="http://www.smooks.org/sax/features/generate-java-event-stream" />
 *     </features>
 * </reader>
 * 
* * Turning off event stream generation can make sense in many situations. In some use cases event stream * generation may add no value and may just reduces performance e.g. where you simply wish to apply a * template to the supplied Java Object(s). *

* Smooks must generate at least 1 event to which configured resources can be targeted (e.g. a templating * resource). When event stream generation is turned off, Smooks simply generates what we call a "Null Source" * document event. In XML, it could be represented as "<nullsource-document/>". So when event stream * generation is turned off, simply target resources at "nullsource-document" or "#document". * * @author [email protected] */ public class JavaSource extends FilterSource { /** * */ public static final String FEATURE_GENERATE_EVENT_STREAM = "http://www.smooks.org/sax/features/generate-java-event-stream"; private List sourceObjects; private Map beans; private boolean eventStreamRequired = true; /** * Default Contructor. * @deprecated Use any of the other constructors. */ public JavaSource() { eventStreamRequired = false; } /** * Construct a JavaSource from the supplied source object. * * @param sourceObject The source object. */ public JavaSource(Object sourceObject) { AssertArgument.isNotNull(sourceObject, "sourceObject"); sourceObjects = new ArrayList(); sourceObjects.add(sourceObject); if(sourceObject instanceof Map) { beans = (Map) sourceObject; } else { beans = new HashMap(); beans.put(toPropertyName(sourceObject.getClass().getSimpleName()), sourceObject); } } /** * Construct a JavaSource from the supplied source object. * * @param objectName The object name the sourceObject is known under * @param sourceObject The source object. */ public JavaSource(String objectName, Object sourceObject) { AssertArgument.isNotNull(sourceObject, "sourceObject"); sourceObjects = new ArrayList(); sourceObjects.add(sourceObject); beans = new HashMap(); beans.put(objectName, sourceObject); } /** * Construct a JavaSource from the supplied source object. * * @param sourceObjects The source object list. */ public JavaSource(List sourceObjects) { AssertArgument.isNotNull(sourceObjects, "sourceObjects"); this.sourceObjects = sourceObjects; beans = new HashMap(); beans.put("objects", sourceObjects); } /** * Is SAX event stream generation required for the processing of this JavaSource. *

* Default is true. * * @return True if SAX event stream generation is required, otherwise false. */ public boolean isEventStreamRequired() { return eventStreamRequired; } /** * Turn on/off SAX event stream generation for the processing of this JavaSource. * * @param eventStreamRequired True if SAX event stream generation is required, otherwise false. */ public void setEventStreamRequired(boolean eventStreamRequired) { this.eventStreamRequired = eventStreamRequired; } /** * Get the source object list. * @return The source object list. */ public List getSourceObjects() { return sourceObjects; } /** * Get the input bean map for the transform. * * @return The bean map. */ public Map getBeans() { return beans; } /** * Set the input bean map for the transform. * * @param beans The bean map. */ public void setBeans(Map beans) { this.beans = beans; } private String toPropertyName(String simpleName) { StringBuilder stringBuilder = new StringBuilder(simpleName); int stringLength = stringBuilder.length(); for(int i = 0; i < stringLength; i++) { char charAt = stringBuilder.charAt(i); if(Character.isLowerCase(charAt)) { break; } stringBuilder.setCharAt(i, Character.toLowerCase(charAt)); } return stringBuilder.toString(); } }