org.metawidget.pipeline.w3c.W3CPipeline Maven / Gradle / Ivy
// Metawidget (licensed under LGPL)
//
// 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 2.1 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package org.metawidget.pipeline.w3c;
import java.util.Map;
import org.metawidget.config.iface.ConfigReader;
import org.metawidget.config.impl.BaseConfigReader;
import org.metawidget.pipeline.base.BasePipeline;
import org.metawidget.util.XmlUtils;
import org.metawidget.widgetprocessor.iface.WidgetProcessor;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
/**
* Pipeline for platforms that support org.w3c.dom
.
*
* @author Richard Kennard
*/
public abstract class W3CPipeline
extends BasePipeline {
//
// Private statics
//
private static ConfigReader DEFAULT_CONFIG_READER;
//
// Private methods
//
private ConfigReader mConfigReader;
private Object mConfig;
//
// Public methods
//
/**
* Gets the current ConfigReader
, or creates a default one if one hasn't been set.
*
* Subclasses wishing to set a different default should call setConfigReader
. Care
* should be taken to reuse the same ConfigReader
instance as much as
* possible, to maximize caching.
*/
public final ConfigReader getConfigReader() {
if ( mConfigReader == null ) {
if ( DEFAULT_CONFIG_READER == null ) {
DEFAULT_CONFIG_READER = new BaseConfigReader();
}
mConfigReader = DEFAULT_CONFIG_READER;
}
return mConfigReader;
}
public void setConfigReader( ConfigReader configReader ) {
mConfigReader = configReader;
}
/**
* Reference to the configuration file. Typically this is a Resource path (e.g.
* com/myapp/metawidget.xml
), but can also be an id (e.g. for Android).
*/
public Object getConfig() {
return mConfig;
}
public void setConfig( Object config ) {
mConfig = config;
setNeedsConfiguring();
}
/**
* Returns the first WidgetProcessor in this pipeline's list of WidgetProcessors (ie. as added
* by addWidgetProcessor
) that the given class isAssignableFrom
.
*
* This method is here, rather than in BasePipeline
, because even though
* GwtPipeline
overrides it the GWT compiler still chokes on the
* isAssignableFrom
.
*
* @param widgetProcessorClass
* the class, or interface or superclass, to find. Returns null
if no
* such WidgetProcessor
* @param
* the type of the WidgetProcessor. Note this needn't be a subclass of
* WidgetProcessor
. It may be some orthagonal interface (like
* org.metawidget.faces.component.widgetprocessor.ConverterProcessor
)
*/
@SuppressWarnings( "unchecked" )
public T getWidgetProcessor( Class widgetProcessorClass ) {
configureOnce();
if ( getWidgetProcessors() == null ) {
return null;
}
for ( WidgetProcessor widgetProcessor : getWidgetProcessors() ) {
if ( widgetProcessorClass.isAssignableFrom( widgetProcessor.getClass() ) ) {
return (T) widgetProcessor;
}
}
return null;
}
/**
* Overridden to support custom ConfigReaders.
*/
@Override
public void initNestedPipeline(BasePipeline nestedPipeline, Map attributes ) {
((W3CPipeline) nestedPipeline).setConfigReader( getConfigReader() );
super.initNestedPipeline( nestedPipeline, attributes );
}
//
// Protected methods
//
@Override
protected void configure() {
if ( mConfig != null ) {
getConfigReader().configure( (String) mConfig, getPipelineOwner() );
}
configureDefaults();
}
/**
* @return the resource path to the default configuration file, or null if there is no default
* configuration.
*/
protected abstract String getDefaultConfiguration();
/**
* Configure a default Inspector (setInspector
),
* list of InspectionResultProcessors (setInspectionResultProcessors
),
* WidgetBuilder (setWidgetBuilder
), list of
* WidgetProcessors (setWidgetProcessors
) and a Layout (setLayout
).
*/
protected void configureDefaults() {
String defaultConfiguration = getDefaultConfiguration();
if ( defaultConfiguration != null ) {
ConfigReader configReader = getConfigReader();
if ( getInspector() == null ) {
configReader.configure( defaultConfiguration, getPipelineOwner(), "inspector" );
}
if ( getInspectionResultProcessors() == null ) {
configReader.configure( defaultConfiguration, getPipelineOwner(), "inspectionResultProcessors" );
}
if ( getWidgetBuilder() == null ) {
configReader.configure( defaultConfiguration, getPipelineOwner(), "widgetBuilder" );
}
if ( getWidgetProcessors() == null ) {
configReader.configure( defaultConfiguration, getPipelineOwner(), "widgetProcessors" );
}
if ( getLayout() == null ) {
configReader.configure( defaultConfiguration, getPipelineOwner(), "layout" );
}
}
}
@Override
protected Element stringToElement( String xml ) {
Document document = XmlUtils.documentFromString( xml );
return document.getDocumentElement();
}
@Override
protected String elementToString( Element element ) {
return XmlUtils.nodeToString( element, false );
}
@Override
protected Element getFirstChildElement( Element parent ) {
return XmlUtils.getFirstChildElement( parent );
}
@Override
protected Element getNextSiblingElement( Element element ) {
return XmlUtils.getNextSiblingElement( element );
}
@Override
protected String getElementName( Element element ) {
return element.getNodeName();
}
@Override
protected Map getAttributesAsMap( Element element ) {
return XmlUtils.getAttributesAsMap( element );
}
}