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

org.metawidget.inspectionresultprocessor.impl.BaseInspectionResultProcessor Maven / Gradle / Ivy

The newest version!
// Metawidget
//
// This file is dual licensed under both the LGPL
// (http://www.gnu.org/licenses/lgpl-2.1.html) and the EPL
// (http://www.eclipse.org/org/documents/epl-v10.php). As a
// recipient of Metawidget, you may choose to receive it under either
// the LGPL or the EPL.
//
// Commercial licenses are also available. See http://metawidget.org
// for details.

package org.metawidget.inspectionresultprocessor.impl;

import static org.metawidget.inspector.InspectionResultConstants.*;

import java.util.Map;

import org.metawidget.inspectionresultprocessor.iface.DomInspectionResultProcessor;
import org.metawidget.inspectionresultprocessor.iface.InspectionResultProcessorException;
import org.metawidget.util.ArrayUtils;
import org.metawidget.util.XmlUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * Convenience implementation for InspectionResultProcessors.
 *
 * @author Richard Kennard
 */

public abstract class BaseInspectionResultProcessor
	implements DomInspectionResultProcessor {

	//
	// Public methods
	//

	/**
	 * Process the given inspection result in context of the given Metawidget.
	 * 

* This method is marked final because most Metawidget implementations will call * processInspectionResultAsDom directly instead. So subclasses need to override * processInspectionResultAsDom, not processInspectionResult. */ public final String processInspectionResult( String inspectionResult, M metawidget, Object toInspect, String type, String... names ) { Document document = XmlUtils.documentFromString( inspectionResult ); Element inspectionResultRoot = document.getDocumentElement(); Element newInspectionResultRoot = processInspectionResultAsDom( inspectionResultRoot, metawidget, toInspect, type, names ); return XmlUtils.documentToString( newInspectionResultRoot.getOwnerDocument(), false ); } public Element processInspectionResultAsDom( Element inspectionResult, M metawidget, Object toInspect, String type, String... names ) { Element entity = XmlUtils.getFirstChildElement( inspectionResult ); // Sanity check String elementName = entity.getNodeName(); if ( !ENTITY.equals( elementName ) ) { throw InspectionResultProcessorException.newException( "Top-level element name should be " + ENTITY + ", not " + elementName ); } Map attributes = XmlUtils.getAttributesAsMap( entity ); processEntity( attributes, metawidget, toInspect, type, names ); XmlUtils.setMapAsAttributes( entity, attributes ); processTraits( entity, metawidget, toInspect, type, names ); return inspectionResult; } // // Protected methods // /** * Process the traits of the given entity. Subclasses may find it useful to override this method * to perform before/after setup around trait processing (for example, establishing an EL * context). * * @param entity * the DOM Element representing the entity that contains the traits * @param metawidget * the parent Metawidget. Never null. May be useful to help processing * @param toInspect * the Object being inspected. May be useful to help processing * @param type * the type being inspected. May be useful to help processing * @param names * the names being inspected. May be useful to help processing */ protected void processTraits( Element entity, M metawidget, Object toInspect, String type, String... names ) { // For each trait... Element trait = XmlUtils.getFirstChildElement( entity ); while ( trait != null ) { // ...modify its attributes as appropriate Map attributes = XmlUtils.getAttributesAsMap( trait ); processTrait( attributes, metawidget ); XmlUtils.setMapAsAttributes( trait, attributes ); // If the trait has children, modify them too. This is not strictly in keeping with how // inspection-result-1.0.xsd is defined, but is very useful for // TypeMappingInspectionResultProcessor so that it can process embedded schemas before // returning them over a remote XML/JSON interface if ( trait.getChildNodes().getLength() > 0 && attributes.containsKey( NAME ) ) { processTraits( trait, metawidget, toInspect, type, ArrayUtils.add( names, attributes.get( NAME ) ) ); } trait = XmlUtils.getNextSiblingElement( trait ); } } /** * Defers to processAttributes by default. * * @param attributes * attributes of the entity being processed. Subclasses can modify this Map to * modify the attributes * @param metawidget * the parent Metawidget. Never null. May be useful to help processing * @param toInspect * the Object being inspected. May be useful to help processing * @param type * the type being inspected. May be useful to help processing * @param names * the names being inspected. May be useful to help processing */ protected void processEntity( Map attributes, M metawidget, Object toInspect, String type, String... names ) { processAttributes( attributes, metawidget ); } /** * Defers to processAttributes by default. * * @param attributes * attributes of the trait being processed. Subclasses can modify this Map to * modify the attributes * @param metawidget * the parent Metawidget. Never null. May be useful to help processing * @param toInspect * the Object being inspected. May be useful to help processing * @param type * the type being inspected. May be useful to help processing * @param names * the names being inspected. May be useful to help processing */ protected void processTrait( Map attributes, M metawidget ) { processAttributes( attributes, metawidget ); } /** * Process the given attributes (which may belong to either entity, property or action). *

* Does nothing by default. * * @param attributes * attributes of the trait being processed. Subclasses can modify this Map to * modify the attributes * @param metawidget * the parent Metawidget. Never null. May be useful to help processing */ protected void processAttributes( Map attributes, M metawidget ) { // Do nothing by default } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy