org.metawidget.pipeline.w3c.W3CPipeline Maven / Gradle / Ivy
// Metawidget
//
// 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.ConfigReader;
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 {
//
// Public methods
//
/**
* 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
*/
@SuppressWarnings( "unchecked" )
public T getWidgetProcessor( Class widgetProcessorClass ) {
if ( getWidgetProcessors() == null ) {
return null;
}
for ( WidgetProcessor widgetProcessor : getWidgetProcessors() ) {
if ( widgetProcessorClass.isAssignableFrom( widgetProcessor.getClass() ) ) {
return (T) widgetProcessor;
}
}
return null;
}
/**
* Uses the given ConfigReader
to configure a default Inspector (
* setInspector
), WidgetBuilder (setWidgetBuilder
), list of
* WidgetProcessors (setWidgetProcessors
) and a Layout (setLayout
).
*
* @param metawidgetClass
* the base class of the Metawidget. This is different from
* getPipelineOwner().getClass(), as that will return the instance (and therefore
* potentially a subclass)
*/
public void configureDefaults( ConfigReader configReader, String configuration, Class metawidgetClass ) {
if ( getInspector() == null ) {
configReader.configure( configuration, getPipelineOwner(), "inspector" );
}
if ( getInspectionResultProcessors() == null ) {
configReader.configure( configuration, getPipelineOwner(), "inspectionResultProcessors" );
}
if ( getWidgetBuilder() == null ) {
configReader.configure( configuration, getPipelineOwner(), "widgetBuilder" );
}
if ( getWidgetProcessors() == null ) {
configReader.configure( configuration, getPipelineOwner(), "widgetProcessors" );
}
if ( getLayout() == null ) {
configReader.configure( configuration, getPipelineOwner(), "layout" );
}
}
//
// Protected methods
//
@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 int getChildCount( Element element ) {
return element.getChildNodes().getLength();
}
@Override
protected Element getChildAt( Element element, int index ) {
return XmlUtils.getElementAt( element, index );
}
@Override
protected String getElementName( Element element ) {
return element.getNodeName();
}
@Override
protected Map getAttributesAsMap( Element element ) {
return XmlUtils.getAttributesAsMap( element );
}
}