You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beehive.apache.org by ri...@apache.org on 2005/09/16 22:30:17 UTC
svn commit: r289648 [2/6] - in /beehive/trunk/netui: src/compiler-core/
src/compiler-core/org/apache/beehive/netui/compiler/
src/compiler-core/org/apache/beehive/netui/compiler/genmodel/
src/compiler-core/org/apache/beehive/netui/compiler/grammar/ src/...
Modified: beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/ForwardModel.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/ForwardModel.java?rev=289648&r1=289647&r2=289648&view=diff
==============================================================================
--- beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/ForwardModel.java (original)
+++ beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/ForwardModel.java Fri Sep 16 13:27:57 2005
@@ -20,9 +20,8 @@
import java.util.List;
import java.util.ArrayList;
-import org.apache.beehive.netui.compiler.model.schema.struts11.ForwardDocument.Forward;
-import org.apache.beehive.netui.compiler.model.schema.struts11.SetPropertyDocument.SetProperty;
import org.apache.beehive.netui.compiler.JpfLanguageConstants;
+import org.w3c.dom.Element;
/**
* Represents an action forward in a Struts application.
@@ -32,7 +31,7 @@
implements JpfLanguageConstants
{
private static final String JPF_ACTION_FWD_CLASSNAME = PAGEFLOW_PACKAGE + ".config.PageFlowActionForward";
-
+
private boolean _isNestedReturn = false;
private boolean _contextRelative = false;
private String _name; // required to be set
@@ -47,13 +46,13 @@
private List _actionOutputs = null;
private boolean _restoreQueryString = false;
private boolean _inheritedPath = false;
-
+
protected ForwardModel( StrutsApp parent )
{
super( parent );
}
-
+
public ForwardModel( String name, String path, StrutsApp parent )
{
super( parent );
@@ -61,76 +60,62 @@
_path = path;
}
- public void writeToXMLBean( Forward xb )
+ protected void writeToElement(XmlModelWriter xw, Element element)
{
assert _name != null;
+ element.setAttribute("name", _name);
+ setElementAttributeMayBeEmpty(element, "path", _path == null ? "" : _path);
+ setElementAttribute(element, "contextRelative", _contextRelative);
+ setElementAttribute(element, "redirect", _redirect);
- xb.setName( _name );
-
- if ( xb.getPath() == null ) xb.setPath( _path == null ? "" : _path );
-
- if ( xb.getContextRelative() == null && _contextRelative )
- {
- xb.setContextRelative( Forward.ContextRelative.TRUE );
- }
-
- if ( xb.getRedirect() == null && _redirect ) xb.setRedirect( Forward.Redirect.TRUE );
-
//
// TODO: comment
//
- if ( _inheritedPath ) addSetProperty( xb, "inheritedPath", "true" );
-
+ if ( _inheritedPath ) addSetProperty( xw, element, "inheritedPath", "true" );
+
//
// "externalRedirect" is set using set-property, to indicate that the redirect
// is to another app.
//
- if ( _externalRedirect ) addSetProperty( xb, "externalRedirect", "true" );
-
+ if ( _externalRedirect ) addSetProperty( xw, element, "externalRedirect", "true" );
+
//
// "returnToPage" is set using set-property, which requires us to override the
// ActionForward class.
//
- if ( _returnToPage ) addSetProperty( xb, "returnToPage", "true" );
+ if ( _returnToPage ) addSetProperty( xw, element, "returnToPage", "true" );
//
// "returnToAction" is set using set-property, which requires us to override the
// ActionForward class.
//
- if ( _returnToAction ) addSetProperty( xb, "returnToAction", "true" );
+ if ( _returnToAction ) addSetProperty( xw, element, "returnToAction", "true" );
- if ( _hasExplicitRedirectValue ) addSetProperty( xb, "hasExplicitRedirectValue", "true" );
+ if ( _hasExplicitRedirectValue ) addSetProperty( xw, element, "hasExplicitRedirectValue", "true" );
+
+ if ( _restoreQueryString ) addSetProperty( xw, element, "restoreQueryString", "true" );
- if ( _restoreQueryString ) addSetProperty( xb, "restoreQueryString", "true" );
-
if ( _actionOutputs != null && _actionOutputs.size() > 0 )
{
- if ( xb.getClassName() == null ) xb.setClassName( JPF_ACTION_FWD_CLASSNAME );
-
int n = _actionOutputs.size();
- SetProperty countProp = xb.addNewSetProperty();
- countProp.setProperty( "actionOutputCount" );
- countProp.setValue( Integer.toString( n ) );
-
+ addSetProperty(xw, element, "actionOutputCount", Integer.toString( n ) );
+
for ( int i = 0; i < n; ++i )
{
ActionOutputModel pi = ( ActionOutputModel ) _actionOutputs.get( i );
- SetProperty prop = xb.addNewSetProperty();
- prop.setProperty( "actionOutput" + i );
- prop.setValue( pi.getType() + '|' + pi.getNullable() + '|' + pi.getName() );
+ String value = pi.getType() + '|' + pi.getNullable() + '|' + pi.getName();
+ addSetProperty(xw, element, "actionOutput" + i, value);
}
}
-
+
//
// "nestedReturn" is set using set-property, which requires us to override the
// ActionForward class.
//
- if ( _isNestedReturn ) addSetProperty( xb, "nestedReturn", "true" );
- if ( _outputFormBeanType != null ) addSetProperty( xb, "returnFormType", _outputFormBeanType );
- if ( _outputFormBeanMember != null ) addSetProperty( xb, "returnFormMember", _outputFormBeanMember );
-
- addComment( xb );
- }
+ if ( _isNestedReturn ) addSetProperty( xw, element, "nestedReturn", "true" );
+ if ( _outputFormBeanType != null ) addSetProperty( xw, element, "returnFormType", _outputFormBeanType );
+ if ( _outputFormBeanMember != null ) addSetProperty( xw, element, "returnFormMember", _outputFormBeanMember );
+ }
public boolean isReturnToPage()
{
@@ -285,18 +270,15 @@
if ( _actionOutputs == null ) _actionOutputs = new ArrayList();
_actionOutputs.add( actionOutput );
}
-
+
public boolean isNestedReturn()
{
return _isNestedReturn;
}
-
- private static void addSetProperty( Forward xb, String propertyName, String propertyValue )
+
+ private void addSetProperty( XmlModelWriter xw, Element element, String propertyName, String propertyValue )
{
- SetProperty prop = xb.addNewSetProperty();
- prop.setProperty( propertyName );
- prop.setValue( propertyValue );
- if ( xb.getClassName() == null ) xb.setClassName( JPF_ACTION_FWD_CLASSNAME );
+ setCustomProperty(xw, element, propertyName, propertyValue, JPF_ACTION_FWD_CLASSNAME);
}
public boolean isInheritedPath()
Modified: beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/MessageResourcesModel.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/MessageResourcesModel.java?rev=289648&r1=289647&r2=289648&view=diff
==============================================================================
--- beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/MessageResourcesModel.java (original)
+++ beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/MessageResourcesModel.java Fri Sep 16 13:27:57 2005
@@ -17,7 +17,7 @@
*/
package org.apache.beehive.netui.compiler.model;
-import org.apache.beehive.netui.compiler.model.schema.struts11.MessageResourcesDocument;
+import org.w3c.dom.Element;
public class MessageResourcesModel extends StrutsElementSupport
{
@@ -25,7 +25,7 @@
private String _key; // the ServletContext attribute in which to store the resources
private Boolean _returnNull; // if false, will return '???keyname???' instead of null for missing resources
private String _factory; // configuration bean class -- org.apache.struts.config.MessageResourcesConfig
-
+
public MessageResourcesModel( StrutsApp parent )
{
super( parent );
@@ -81,36 +81,11 @@
_returnNull = returnNull;
}
- public void writeToXMLBean( MessageResourcesDocument.MessageResources mr )
+ protected void writeToElement(XmlModelWriter xw, Element element)
{
- if ( mr.getKey() == null && _key != null )
- {
- mr.setKey( _key );
- }
-
- if ( mr.getParameter() == null )
- {
- assert _parameter != null;
- mr.setParameter( _parameter );
- }
-
- if ( mr.getNull() == null && _returnNull != null )
- {
- if ( _returnNull.booleanValue() )
- {
- mr.setNull( MessageResourcesDocument.MessageResources.Null.TRUE );
- }
- else
- {
- mr.setNull( MessageResourcesDocument.MessageResources.Null.FALSE );
- }
- }
-
- if ( mr.getFactory() == null && _factory != null )
- {
- mr.setFactory( _factory );
- }
+ setElementAttribute(element, "key", _key);
+ setElementAttribute(element, "parameter", _parameter);
+ setElementAttribute(element, "null", _returnNull);
+ setElementAttribute(element, "factory", _factory);
}
-
-
}
Modified: beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/StrutsApp.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/StrutsApp.java?rev=289648&r1=289647&r2=289648&view=diff
==============================================================================
--- beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/StrutsApp.java (original)
+++ beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/StrutsApp.java Fri Sep 16 13:27:57 2005
@@ -17,18 +17,12 @@
*/
package org.apache.beehive.netui.compiler.model;
-import org.apache.beehive.netui.compiler.model.schema.struts11.*;
import org.apache.beehive.netui.compiler.model.validation.ValidationModel;
import org.apache.beehive.netui.compiler.JpfLanguageConstants;
import org.apache.beehive.netui.compiler.FatalCompileTimeException;
-import org.apache.xmlbeans.XmlObject;
-import org.apache.xmlbeans.XmlException;
-import org.apache.xmlbeans.XmlDocumentProperties;
-import org.apache.xmlbeans.XmlCursor;
-import org.apache.xmlbeans.XmlOptions;
+import org.w3c.dom.Element;
import java.io.File;
-import java.io.PrintStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
@@ -341,11 +335,6 @@
}
}
- protected ForwardDocument.Forward addNewForward( XmlObject xmlObject )
- {
- return ( ( GlobalForwardsDocument.GlobalForwards ) xmlObject ).addNewForward();
- }
-
protected Map getFormBeansMap()
{
return _formBeans;
@@ -420,297 +409,129 @@
}
public void writeXml( PrintWriter writer, File mergeFile )
- throws IOException, XmlException, FatalCompileTimeException
+ throws IOException, FatalCompileTimeException, XmlModelWriterException
{
- StrutsConfigDocument doc;
-
- if ( mergeFile != null && mergeFile.canRead() )
- {
- doc = StrutsConfigDocument.Factory.parse( mergeFile );
- }
- else
- {
- doc = StrutsConfigDocument.Factory.newInstance();
- }
+ XmlModelWriter xw = new XmlModelWriter( mergeFile, "struts-config",
+ "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN",
+ "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd",
+ getHeaderComment(mergeFile));
+ writeXML(xw, xw.getRootElement());
+ xw.simpleFastWrite(writer);
+ }
-
- //
- // Write the DOCTYPE and all that good stuff.
- //
- XmlDocumentProperties docProps = doc.documentProperties();
-
- if ( docProps.getDoctypeName() == null )
- {
- docProps.setDoctypeName( "struts-config" ); // NOI18N
- }
-
- if ( docProps.getDoctypePublicId() == null )
- {
- docProps.setDoctypePublicId( "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" ); // NOI18N
- }
-
- if ( docProps.getDoctypeSystemId() == null )
- {
- docProps.setDoctypeSystemId( "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd" ); // NOI18N
- }
-
-
- //
- // struts-config
- //
- StrutsConfigDocument.StrutsConfig scElement = doc.getStrutsConfig();
-
- if ( scElement == null )
- {
- scElement = doc.addNewStrutsConfig();
- }
-
- //
- // Write the "generated by" comment.
- //
- XmlCursor curs = scElement.newCursor();
- String headerComment = getHeaderComment( mergeFile );
- if ( headerComment != null ) curs.insertComment( headerComment );
-
- //
+ protected void writeToElement(XmlModelWriter xw, Element element) {
// form-beans
- //
- writeFormBeans( scElement );
-
+ writeFormBeans(xw, element);
- //
// global-exceptions
- //
- writeExceptions( scElement );
-
+ writeExceptions(xw, element);
- //
// global-forwards
- //
- GlobalForwardsDocument.GlobalForwards globalForwards = scElement.getGlobalForwards();
-
- if ( globalForwards == null )
- {
- globalForwards = scElement.addNewGlobalForwards();
- }
- writeForwards( globalForwards.getForwardArray(), globalForwards );
+ Element globalForwardsElement = findChildElement(xw, element, "global-forwards", true);
+ writeForwards(xw, globalForwardsElement);
-
- //
// action-mappings
- //
- writeActionMappings( scElement );
-
+ writeActionMappings(xw, element);
- //
// message-resources
- //
- writeMessageResources( scElement );
+ writeMessageResources(xw, element);
-
- //
- // request-processor
- //
- writeControllerElement( scElement );
+ // controller (request-processor, etc.)
+ writeControllerElement(xw, element);
-
- //
// ValidatorPlugIn
- //
- writeValidatorInit( scElement );
-
+ writeValidatorInit(xw, element);
- //
// TilesPlugin
- //
- writeTilesInit( scElement );
-
-
- //
- // Write the file.
- //
- XmlOptions options = new XmlOptions();
- options.setSavePrettyPrint();
- doc.save( writer, options );
+ writeTilesInit(xw, element);
}
- private void writeMessageResources( StrutsConfigDocument.StrutsConfig scElement )
+ private void writeMessageResources(XmlModelWriter xw, Element element)
{
- MessageResourcesDocument.MessageResources[] existingMessageResources = scElement.getMessageResourcesArray();
-
for ( Iterator i = getMessageResourcesList().iterator(); i.hasNext(); )
{
MessageResourcesModel mr = ( MessageResourcesModel ) i.next();
if ( mr != null )
{
- MessageResourcesDocument.MessageResources mrToEdit = null;
-
- for ( int j = 0; j < existingMessageResources.length; ++j )
- {
- String existingKey = existingMessageResources[j].getKey();
-
- if ( ( existingKey == null && mr.getKey() == null )
- || ( existingKey != null && mr.getKey() != null && existingKey.equals( mr.getKey() ) ) )
- {
- mrToEdit = existingMessageResources[j];
- break;
- }
-
- }
-
- if ( mrToEdit == null )
- {
- mrToEdit = scElement.addNewMessageResources();
- }
-
- mr.writeToXMLBean( mrToEdit );
+ Element mrToEdit = findChildElement(xw, element, "message-resources", "key", mr.getKey(), true);
+ mr.writeXML( xw, mrToEdit );
}
}
}
- private void writeActionMappings( StrutsConfigDocument.StrutsConfig scElement )
+ private void writeActionMappings(XmlModelWriter xw, Element element)
{
- ActionMappingsDocument.ActionMappings actionMappings = scElement.getActionMappings();
-
- if ( actionMappings == null )
- {
- actionMappings = scElement.addNewActionMappings();
- }
-
- ActionDocument.Action[] existingActions = actionMappings.getActionArray();
+ Element actionMappingsElement = findChildElement(xw, element, "action-mappings", true);
List actionMappingsList = getSortedActionMappings();
for ( int i = 0; i < actionMappingsList.size(); ++i )
{
ActionModel am = ( ActionModel ) actionMappingsList.get( i );
- ActionDocument.Action actionMappingToEdit = null;
-
- for ( int j = 0; j < existingActions.length; ++j )
- {
- if ( am.getPath().equals( existingActions[j].getPath() ) )
- {
- actionMappingToEdit = existingActions[j];
- break;
- }
- }
-
- if ( actionMappingToEdit == null )
- {
- actionMappingToEdit = actionMappings.addNewAction();
- }
-
- am.writeToXMLBean( actionMappingToEdit );
+ Element actionMappingtoEdit = findChildElement(xw, actionMappingsElement, "action", "path", am.getPath(), true);
+ am.writeXML(xw, actionMappingtoEdit);
}
}
- private void writeExceptions( StrutsConfigDocument.StrutsConfig scElement )
+ private void writeExceptions(XmlModelWriter xw, Element element)
{
- GlobalExceptionsDocument.GlobalExceptions globalExceptions = scElement.getGlobalExceptions();
-
- if ( globalExceptions == null )
- {
- globalExceptions = scElement.addNewGlobalExceptions();
- }
-
+ Element exceptionsElement = findChildElement(xw, element, "global-exceptions", true);
List exceptionCatches = getExceptionCatchesList();
+
if ( exceptionCatches != null && ! exceptionCatches.isEmpty() )
{
- ExceptionDocument.Exception[] existingExceptions = globalExceptions.getExceptionArray();
-
for ( int i = 0; i < exceptionCatches.size(); ++i )
{
ExceptionModel ec = ( ExceptionModel ) exceptionCatches.get( i );
- ExceptionDocument.Exception exceptionToEdit = null;
-
- for ( int j = 0; j < existingExceptions.length; ++j )
- {
- if ( ec.getType().equals( existingExceptions[j].getType() ) )
- {
- exceptionToEdit = existingExceptions[j];
- break;
- }
- }
-
- if ( exceptionToEdit == null )
- {
- exceptionToEdit = globalExceptions.addNewException();
- }
-
- ec.writeToXMLBean( exceptionToEdit );
+ Element exceptionToEdit = findChildElement(xw, exceptionsElement, "exception", "type", ec.getType(), true);
+ ec.writeXML( xw, exceptionToEdit );
}
}
}
- private void writeFormBeans( StrutsConfigDocument.StrutsConfig scElement )
+
+ private void writeFormBeans(XmlModelWriter xw, Element element)
{
- FormBeansDocument.FormBeans formBeans = scElement.getFormBeans();
+ Element formBeansElement = findChildElement(xw, element, "form-beans", true);
- if ( formBeans == null )
+ for (Iterator i = getFormBeansMap().values().iterator(); i.hasNext(); )
{
- formBeans = scElement.addNewFormBeans();
- }
-
- FormBeanDocument.FormBean[] existingBeans = formBeans.getFormBeanArray();
-
- for ( Iterator i = getFormBeansMap().values().iterator(); i.hasNext(); )
- {
- FormBeanModel fb = ( FormBeanModel ) i.next();
+ FormBeanModel fb = (FormBeanModel) i.next();
- if ( fb != null )
- {
- FormBeanDocument.FormBean formBeanToEdit = null;
-
- for ( int j = 0; j < existingBeans.length; ++j )
- {
- if ( existingBeans[j].getName().equals( fb.getName() ) )
- {
- formBeanToEdit = existingBeans[j];
- break;
- }
-
- }
-
- if ( formBeanToEdit == null )
- {
- formBeanToEdit = formBeans.addNewFormBean();
- }
-
- fb.writeToXMLBean( formBeanToEdit );
+ // fb may be null -- we do this to prevent a name from being used.
+ if (fb != null) {
+ Element formBean = findChildElement(xw, formBeansElement, "form-bean", "name", fb.getName(), true);
+ fb.writeXML(xw, formBean);
}
}
}
- protected void writeControllerElement( StrutsConfigDocument.StrutsConfig scElement )
+ protected void writeControllerElement(XmlModelWriter xw, Element element)
{
- ControllerDocument.Controller controller = scElement.getController();
- if ( controller == null ) controller = scElement.addNewController();
+ Element controllerElement = findChildElement(xw, element, "controller", false);
- if ( controller.getProcessorClass() == null )
- {
- controller.setProcessorClass( PAGEFLOW_REQUESTPROCESSOR_CLASSNAME );
- }
-
- if ( controller.getInputForward() == null )
- {
- controller.setInputForward( ControllerDocument.Controller.InputForward.TRUE );
+ // Insert it before the first <message-resources> element.
+ if (controllerElement == null) {
+ controllerElement = xw.getDocument().createElement("controller");
+ Element[] messageResourcesElements = getChildElements(element, "message-resources");
+ if (messageResourcesElements.length > 0) {
+ element.insertBefore(controllerElement, messageResourcesElements[0]);
+ } else {
+ element.appendChild(controllerElement);
+ }
}
- if ( _multipartHandlerClassName != null && controller.getMultipartClass() == null )
- {
- controller.setMultipartClass( _multipartHandlerClassName );
- }
+ setElementAttribute(controllerElement, "processorClass", PAGEFLOW_REQUESTPROCESSOR_CLASSNAME);
+ setElementAttribute(controllerElement, "inputForward", true);
+ setElementAttribute(controllerElement, "multipartClass", _multipartHandlerClassName);
+ setElementAttribute(controllerElement, "memFileSize", _memFileSize);
- if ( _memFileSize != null && controller.getMemFileSize() == null )
- {
- controller.setMemFileSize( _memFileSize );
- }
- if ( _isNestedPageFlow ) addSetProperty( controller, "isNestedPageFlow", "true" );
- if ( _isLongLivedPageFlow ) addSetProperty( controller, "isLongLivedPageFlow", "true" );
- if ( _isSharedFlow ) addSetProperty( controller, "isSharedFlow", "true" );
- if ( isReturnToPageDisabled() ) addSetProperty( controller, "isReturnToPageDisabled", "true" );
- if ( isReturnToActionDisabled() ) addSetProperty( controller, "isReturnToActionDisabled", "true" );
+ if ( _isNestedPageFlow ) addSetProperty( xw, controllerElement, "isNestedPageFlow", "true" );
+ if ( _isLongLivedPageFlow ) addSetProperty( xw, controllerElement, "isLongLivedPageFlow", "true" );
+ if ( _isSharedFlow ) addSetProperty( xw, controllerElement, "isSharedFlow", "true" );
+ if ( isReturnToPageDisabled() ) addSetProperty( xw, controllerElement, "isReturnToPageDisabled", "true" );
+ if ( isReturnToActionDisabled() ) addSetProperty( xw, controllerElement, "isReturnToActionDisabled", "true" );
if ( _sharedFlows != null )
{
@@ -727,72 +548,36 @@
str.append( name ).append( '=' ).append( type );
}
- addSetProperty( controller, "sharedFlows", str.toString() );
+ addSetProperty( xw, controllerElement, "sharedFlows", str.toString() );
}
- addSetProperty( controller, "controllerClass", _controllerClassName );
+ addSetProperty( xw, controllerElement, "controllerClass", _controllerClassName );
//
// If there is not a default MessageResources element in the generated XML, add a special set-property
// to communicate this to the runtime.
//
- MessageResourcesDocument.MessageResources[] mrArray = scElement.getMessageResourcesArray();
- for ( int i = 0; i < mrArray.length; i++ )
- {
- MessageResourcesDocument.MessageResources messageResources = mrArray[i];
- if ( messageResources.getKey() == null ) return;
+ if (findChildElement(xw, element, "message-resources", "key", null, false) == null) {
+ addSetProperty( xw, controllerElement, "isMissingDefaultMessages", "true" );
}
- addSetProperty( controller, "isMissingDefaultMessages", "true" );
}
- protected static void addSetProperty( ControllerDocument.Controller controller, String propName, String propValue )
+ protected void addSetProperty( XmlModelWriter xw, Element element, String propName, String propValue )
{
- if ( controller.getClassName() == null ) controller.setClassName( PAGEFLOW_CONTROLLER_CONFIG_CLASSNAME );
- SetPropertyDocument.SetProperty prop = controller.addNewSetProperty();
- prop.setProperty( propName );
- prop.setValue( propValue );
+ setCustomProperty(xw, element, propName, propValue, PAGEFLOW_CONTROLLER_CONFIG_CLASSNAME);
}
- protected void writeValidatorInit( StrutsConfigDocument.StrutsConfig scElement )
+ protected void writeValidatorInit(XmlModelWriter xw, Element element)
{
if ( ( _validationModel != null && ! _validationModel.isEmpty() ) || _additionalValidatorConfigs != null )
{
- PlugInDocument.PlugIn plugInElementToEdit = null;
- PlugInDocument.PlugIn[] existingPlugIns = scElement.getPlugInArray();
+ Element plugInElementToEdit = findChildElement(xw, element, "plug-in", "className",
+ VALIDATOR_PLUG_IN_CLASSNAME, true);
- for ( int i = 0; i < existingPlugIns.length; i++ )
- {
- PlugInDocument.PlugIn existingPlugIn = existingPlugIns[i];
-
- if ( VALIDATOR_PLUG_IN_CLASSNAME.equals( existingPlugIn.getClassName() ) )
- {
- plugInElementToEdit = existingPlugIn;
- break;
- }
- }
-
- if ( plugInElementToEdit == null )
- {
- plugInElementToEdit = scElement.addNewPlugIn();
- plugInElementToEdit.setClassName( VALIDATOR_PLUG_IN_CLASSNAME );
- }
-
- SetPropertyDocument.SetProperty[] existingSetProperties = plugInElementToEdit.getSetPropertyArray();
-
- for ( int i = 0; i < existingSetProperties.length; i++ )
- {
- if ( VALIDATOR_PATHNAMES_PROPERTY.equals( existingSetProperties[i].getProperty() ) )
- {
- //
- // This means that in the user's struts-merge file, there's already a "pathnames" set-property
- // element. We don't want to overwrite it.
- //
- return;
- }
- }
+ Element pathnamesPropertyElement =
+ findChildElement(xw, plugInElementToEdit, "set-property", "property", VALIDATOR_PATHNAMES_PROPERTY, true);
- SetPropertyDocument.SetProperty pathnamesProperty = plugInElementToEdit.addNewSetProperty();
- pathnamesProperty.setProperty( VALIDATOR_PATHNAMES_PROPERTY );
+ pathnamesPropertyElement.setAttribute("property", VALIDATOR_PATHNAMES_PROPERTY);
StringBuffer pathNames = new StringBuffer();
pathNames.append( NETUI_VALIDATOR_RULES_URI );
pathNames.append( ',' ).append( STRUTS_VALIDATOR_RULES_URI );
@@ -811,84 +596,38 @@
}
}
- pathnamesProperty.setValue( pathNames.toString() );
+ setElementAttribute(pathnamesPropertyElement, "value", pathNames.toString());
}
}
- protected void writeTilesInit( StrutsConfigDocument.StrutsConfig scElement )
+ protected void writeTilesInit(XmlModelWriter xw, Element element)
{
- if ( _tilesDefinitionsConfigs == null || _tilesDefinitionsConfigs.isEmpty() )
- {
+ if ( _tilesDefinitionsConfigs == null || _tilesDefinitionsConfigs.isEmpty() ) {
return;
}
- PlugInDocument.PlugIn plugInElementToEdit = null;
- PlugInDocument.PlugIn[] existingPlugIns = scElement.getPlugInArray();
-
- for ( int i = 0; i < existingPlugIns.length; i++ )
- {
- PlugInDocument.PlugIn existingPlugIn = existingPlugIns[i];
+ Element plugInElementToEdit =
+ findChildElement(xw, element, "plug-in", "className", TILES_PLUG_IN_CLASSNAME, true);
- if ( TILES_PLUG_IN_CLASSNAME.equals( existingPlugIn.getClassName() ) )
- {
- plugInElementToEdit = existingPlugIn;
- break;
- }
- }
-
- if ( plugInElementToEdit == null )
- {
- plugInElementToEdit = scElement.addNewPlugIn();
- plugInElementToEdit.setClassName( TILES_PLUG_IN_CLASSNAME );
- }
-
- boolean definitionsConfigIsSet = false;
- boolean moduleAwarePropertyIsSet = false;
- SetPropertyDocument.SetProperty[] existingSetProperties = plugInElementToEdit.getSetPropertyArray();
-
- for ( int i = 0; i < existingSetProperties.length; i++ )
- {
- String name = existingSetProperties[i].getProperty();
-
- if ( TILES_DEFINITIONS_CONFIG_PROPERTY.equals( name ) )
- {
- //
- // This means that in the user's struts-merge file, there's already a
- // "definitions-config" set-property element. We don't want to overwrite it.
- //
- definitionsConfigIsSet = true;
- }
-
- if ( TILES_MODULE_AWARE_PROPERTY.equals( name ) )
- {
- // Make sure "moduleAware" is true
- moduleAwarePropertyIsSet = true;
- }
- }
+
+
+ Element definitionsElement = findChildElement(xw, plugInElementToEdit, "set-property", "property",
+ TILES_DEFINITIONS_CONFIG_PROPERTY, true);
+ StringBuffer pathNames = new StringBuffer();
+ boolean firstOne = true;
- if ( !definitionsConfigIsSet )
+ for ( java.util.Iterator ii = _tilesDefinitionsConfigs.iterator(); ii.hasNext(); )
{
- SetPropertyDocument.SetProperty pathnamesProperty = plugInElementToEdit.addNewSetProperty();
- pathnamesProperty.setProperty( TILES_DEFINITIONS_CONFIG_PROPERTY );
- StringBuffer pathNames = new StringBuffer();
- boolean firstOne = true;
-
- for ( java.util.Iterator ii = _tilesDefinitionsConfigs.iterator(); ii.hasNext(); )
- {
- String definitionsConfig = ( String ) ii.next();
- if ( ! firstOne ) pathNames.append( ',' );
- firstOne = false;
- pathNames.append( definitionsConfig );
- }
- pathnamesProperty.setValue( pathNames.toString() );
+ String definitionsConfig = ( String ) ii.next();
+ if ( ! firstOne ) pathNames.append( ',' );
+ firstOne = false;
+ pathNames.append( definitionsConfig );
}
+ setElementAttribute(definitionsElement, "value", pathNames.toString());
- if ( !moduleAwarePropertyIsSet )
- {
- SetPropertyDocument.SetProperty pathnamesProperty = plugInElementToEdit.addNewSetProperty();
- pathnamesProperty.setProperty( TILES_MODULE_AWARE_PROPERTY );
- pathnamesProperty.setValue( "true" );
- }
+ Element moduleAwareElement =
+ findChildElement(xw, plugInElementToEdit, "set-property", "property", TILES_MODULE_AWARE_PROPERTY, true);
+ setElementAttribute(moduleAwareElement, "value", true);
}
protected String getHeaderComment( File mergeFile )
Modified: beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/StrutsElementSupport.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/StrutsElementSupport.java?rev=289648&r1=289647&r2=289648&view=diff
==============================================================================
--- beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/StrutsElementSupport.java (original)
+++ beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/StrutsElementSupport.java Fri Sep 16 13:27:57 2005
@@ -17,23 +17,14 @@
*/
package org.apache.beehive.netui.compiler.model;
-import org.w3c.dom.Node;
-import org.apache.xmlbeans.XmlObject;
-import org.apache.xmlbeans.XmlCursor;
-
-import java.util.HashMap;
+import org.w3c.dom.Element;
/**
* Defines general support for elements that
*/
-public abstract class StrutsElementSupport
+public abstract class StrutsElementSupport extends XmlElementSupport
{
- private String _description;
- private HashMap _properties = new HashMap();
- private String _displayName;
- private Icon _icon;
private StrutsApp _parentApp;
- private String _comment;
private String _className;
public StrutsElementSupport( StrutsApp parentApp )
@@ -46,30 +37,6 @@
return _parentApp;
}
- public void setDescription( String description )
- { _description = description; }
-
- public String getDescription()
- { return _description; }
-
- public void setProperty( String name, String value )
- { _properties.put( name, value ); }
-
- public String getProperty( String name )
- { return ( String ) _properties.get( name ); }
-
- public void setDisplayName( String displayName )
- { _displayName = displayName; }
-
- public String getDisplayName()
- { return _displayName; }
-
- public void setIcon( Icon icon )
- { _icon = icon; }
-
- public Icon getIcon()
- { return _icon; }
-
public String getClassName()
{
return _className;
@@ -80,70 +47,19 @@
_className = className;
}
- public void setComment( String comment )
- {
- _comment = comment;
- }
-
- public String getComment()
- {
- return _comment;
- }
-
- class Icon
- {
- String smallIconLocation;
- String largeIconLocation;
-
- /**
- * Creates an object representing icon locations for certain types of
- * elements in the struts-config.xml file.
- * @param smallIconLocation the location of a small icon
- * relative to the stuts config file
- */
- public Icon( String smallIconLocation )
- {
- this( smallIconLocation, null );
- }
-
- /**
- * Creates an object representing icon locations for certain types of
- * elements in the struts-config.xml file.
- * @param smallIconLocation the location of a small icon
- * relative to the stuts config file
- * @param largeIconLocation the location of a large (32x32) icon
- * relative to the stuts-config.xml file.
- */
- public Icon( String smallIconLocation, String largeIconLocation )
- {
- this.smallIconLocation = smallIconLocation;
- this.largeIconLocation = largeIconLocation;
- }
- }
-
- public static String getAttr( Node node, String name )
- {
- Node attr = node.getAttributes().getNamedItem( name );
- return ( attr != null ? attr.getNodeValue() : null );
- }
-
- public static boolean getAttrBool( Node node, String name )
+ protected void setParentApp( StrutsApp parentApp )
{
- String val = getAttr( node, name );
- return ( val != null && val.equalsIgnoreCase( "true" ) ); // NOI18N
+ _parentApp = parentApp;
}
- protected void addComment( XmlObject xb )
+ protected final void setCustomProperty(XmlModelWriter xw, Element element, String propertyName, String value,
+ String changeElementClassName)
{
- if ( _comment != null )
- {
- XmlCursor curs = xb.newCursor();
- curs.insertComment( " " + _comment + " " );
+ if (value != null) {
+ Element setPropertyElement = findChildElement(xw, element, "set-property", "property", propertyName, true);
+ setPropertyElement.setAttribute("property", propertyName);
+ setElementAttributeMayBeEmpty(setPropertyElement, "value", value);
+ setElementAttribute(element, "className", changeElementClassName);
}
- }
-
- protected void setParentApp( StrutsApp parentApp )
- {
- _parentApp = parentApp;
}
}
Added: beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/XmlElementSupport.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/XmlElementSupport.java?rev=289648&view=auto
==============================================================================
--- beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/XmlElementSupport.java (added)
+++ beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/XmlElementSupport.java Fri Sep 16 13:27:57 2005
@@ -0,0 +1,222 @@
+/*
+ * Copyright 2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * $Header:$
+ */
+package org.apache.beehive.netui.compiler.model;
+
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Node;
+import org.w3c.dom.Attr;
+import org.w3c.dom.Text;
+
+import java.util.ArrayList;
+
+public abstract class XmlElementSupport
+{
+ private String _comment;
+
+ public void setComment( String comment )
+ {
+ _comment = comment;
+ }
+
+ public final void writeXML(XmlModelWriter xw, Element element) {
+ if (_comment != null) {
+ xw.addComment( element, ' ' + _comment + ' ' );
+ }
+ writeToElement(xw, element);
+ }
+
+ protected abstract void writeToElement(XmlModelWriter xw, Element element);
+
+ protected final void setElementAttributeMayBeEmpty(Element element, String attrName, String value)
+ {
+ if (value != null) {
+ String existingAttr = getElementAttribute(element, attrName);
+ if (existingAttr == null || existingAttr.length() == 0) {
+ element.setAttribute(attrName, value);
+ }
+ }
+ }
+
+ protected final void setElementAttribute(Element element, String attrName, String value)
+ {
+ if (value != null && value.length() > 0 ) {
+ String existingAttr = getElementAttribute(element, attrName);
+ if (existingAttr == null || existingAttr.length() == 0) {
+ element.setAttribute(attrName, value);
+ }
+ }
+ }
+
+ protected final void setElementAttribute(Element element, String attrName, Boolean value)
+ {
+ if (value != null) {
+ String existingAttr = getElementAttribute(element, attrName);
+ if (existingAttr == null || existingAttr.length() == 0) {
+ element.setAttribute(attrName, value.toString());
+ }
+ }
+ }
+
+ /**
+ * Gets the attribute value, or <code>null</code> (unlike <code>Element.getAttribute</code>).
+ */
+ protected String getElementAttribute(Element element, String attrName)
+ {
+ Attr attr = element.getAttributeNode(attrName);
+ return attr != null ? attr.getValue() : null;
+ }
+
+ protected final void setElementAttribute(Element element, String attrName, boolean value)
+ {
+ if (value) {
+ String existingAttr = getElementAttribute(element, attrName);
+ if (existingAttr == null) {
+ element.setAttribute(attrName, Boolean.toString(value));
+ }
+ }
+ }
+
+ protected final Element findChildElement(XmlModelWriter xw, Element parent, String childName,
+ boolean createIfNotPresent)
+ {
+ return findChildElement(xw, parent, childName, null, null, createIfNotPresent);
+ }
+
+ protected final Element findChildElementWithChildText(XmlModelWriter xw, Element parent, String childName,
+ String childSubElementName, String textValue,
+ boolean createIfNotPresent)
+ {
+ Element[] matchingChildren = getChildElements(parent, childName);
+
+ for (int i = 0; i < matchingChildren.length; i++) {
+ Element childSubElement = findChildElement(xw, matchingChildren[i], childSubElementName, false);
+ if (childSubElement != null) {
+ String text = getTextContent(childSubElement);
+ if (textValue.equals(text)) {
+ return childSubElement;
+ }
+ }
+ }
+
+ if (createIfNotPresent) {
+ Element newChild = xw.addElement(parent, childName);
+ xw.addElementWithText(newChild, childSubElementName, textValue);
+ return newChild;
+ }
+
+ return null;
+ }
+
+ protected final Element findChildElement(XmlModelWriter xw, Element parent, String childName,
+ String keyAttributeName, String keyAttributeValue,
+ boolean createIfNotPresent)
+ {
+ NodeList childNodes = parent.getChildNodes();
+
+ for (int i = 0; i < childNodes.getLength(); ++i)
+ {
+ Node node = childNodes.item(i);
+
+ if (node instanceof Element)
+ {
+ Element childElement = (Element) node;
+
+ if (childName.equals(childElement.getTagName()))
+ {
+ // If there's no target key attribute to match, just return the element.
+ if (keyAttributeName == null) {
+ return childElement;
+ }
+
+ // Return the element if the key attribute values match (or if both are null).
+ String childElementAttributeValue = getElementAttribute(childElement, keyAttributeName);
+ if ((keyAttributeValue == null && childElementAttributeValue == null)
+ || (keyAttributeValue != null && keyAttributeValue.equals(childElementAttributeValue)))
+ {
+ return childElement;
+ }
+ }
+ }
+ }
+
+ if (createIfNotPresent)
+ {
+ Element newChild = xw.getDocument().createElement(childName);
+ parent.appendChild(newChild);
+ if (keyAttributeName != null && keyAttributeValue != null) {
+ newChild.setAttribute(keyAttributeName, keyAttributeValue);
+ }
+ return newChild;
+ }
+
+ return null;
+ }
+
+ protected Element[] getChildElements(Element element, String nameFilter)
+ {
+ NodeList children = element.getChildNodes();
+ ArrayList list = new ArrayList(children.getLength());
+ for (int i = 0; i < children.getLength(); ++i)
+ {
+ Node node = children.item(i);
+ if (node instanceof Element)
+ {
+ if (nameFilter == null || nameFilter.equals(((Element) node).getTagName()))
+ {
+ list.add(node);
+ }
+ }
+ }
+
+ return (Element[]) list.toArray(new Element[list.size()]);
+ }
+
+ public static boolean isWhiteSpace(String s)
+ {
+ for (int j = 0; j < s.length(); ++j) {
+ if (! Character.isWhitespace(s.charAt(j))) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ public static String getTextContent(Element element) // TODO: move to a utils class, so XmlModelWriter is independentf
+ {
+ NodeList children = element.getChildNodes();
+ String retVal = null;
+
+ for (int i = 0, len = children.getLength(); i < len; ++i) {
+ Node child = children.item(i);
+ if (! (child instanceof Text)) {
+ return null;
+ }
+ String text = child.getNodeValue();
+ if (! isWhiteSpace(text)) {
+ if (retVal != null) {
+ return null;
+ }
+ retVal = text;
+ }
+ }
+
+ return retVal;
+ }
+}
Propchange: beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/XmlElementSupport.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/XmlModelWriter.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/XmlModelWriter.java?rev=289648&view=auto
==============================================================================
--- beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/XmlModelWriter.java (added)
+++ beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/XmlModelWriter.java Fri Sep 16 13:27:57 2005
@@ -0,0 +1,242 @@
+/*
+ * Copyright 2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * $Header:$
+ */
+package org.apache.beehive.netui.compiler.model;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.DOMImplementation;
+import org.w3c.dom.DocumentType;
+import org.w3c.dom.Element;
+import org.w3c.dom.Comment;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Text;
+import org.xml.sax.SAXException;
+import org.apache.beehive.netui.compiler.LocalFileEntityResolver;
+
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.TransformerException;
+import java.io.File;
+import java.io.IOException;
+import java.io.Writer;
+
+public class XmlModelWriter
+{
+ private static final int INDENT_LEN = 2;
+
+ private Document _doc;
+ private String _rootName;
+ private String _systemID;
+ private String _publicID;
+
+ public XmlModelWriter(File starterFile, String rootName, String publicID, String systemID, String headerComment)
+ throws XmlModelWriterException, IOException
+ {
+ assert rootName != null;
+ _rootName = rootName;
+ _systemID = systemID;
+ _publicID = publicID;
+
+ try
+ {
+ DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
+ DocumentBuilder builder = builderFactory.newDocumentBuilder();
+ builder.setEntityResolver(LocalFileEntityResolver.getInstance());
+
+ if ( starterFile != null && starterFile.canRead() )
+ {
+ _doc = builder.parse(starterFile);
+ }
+ else
+ {
+ DOMImplementation impl = builder.getDOMImplementation();
+ DocumentType docType = impl.createDocumentType(_rootName, _publicID, _systemID);
+ _doc = impl.createDocument(null, _rootName, docType);
+ }
+
+ if (headerComment != null)
+ {
+ Element root = _doc.getDocumentElement();
+ Comment comment = _doc.createComment(headerComment);
+ root.insertBefore(comment, root.getFirstChild());
+ }
+ }
+ catch (ParserConfigurationException e)
+ {
+ throw new XmlModelWriterException(e);
+ }
+ catch (SAXException e)
+ {
+ throw new XmlModelWriterException(e);
+ }
+ }
+
+ /**
+ * Write the XML to a stream, without using standard APIs. This appears to be about ten times as fast (for our
+ * simple purposes) as using a Transformer ({@link #write}), and it avoids JDK and environment issues with obtaining
+ * a TransformerFactory.
+ */
+ public void simpleFastWrite(Writer out)
+ throws IOException
+ {
+ out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
+
+ if (_publicID != null && _systemID != null) {
+ out.write("<!DOCTYPE ");
+ out.write(_rootName);
+ out.write(" PUBLIC \"");
+ out.write(_publicID);
+ out.write("\" \"");
+ out.write(_systemID);
+ out.write("\">\n");
+ }
+
+ writeElement(out, _doc.getDocumentElement(), 0);
+ }
+
+ /**
+ * Write the XML to a stream, using standard APIs. This appears to be about ten times slower than our custom writer
+ * ({@link #simpleFastWrite}).
+ */
+ public void write(Writer out)
+ throws XmlModelWriterException, IOException
+ {
+ try
+ {
+ DOMSource domSource = new DOMSource(_doc);
+ StreamResult streamResult = new StreamResult(out);
+ TransformerFactory tf = TransformerFactory.newInstance();
+ tf.setAttribute("indent-number", new Integer(INDENT_LEN));
+ Transformer serializer = tf.newTransformer();
+ if (_publicID != null) {
+ serializer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, _publicID);
+ }
+ if (_systemID != null) {
+ serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, _systemID);
+ }
+ serializer.setOutputProperty(OutputKeys.INDENT, "yes");
+ serializer.transform(domSource, streamResult);
+ }
+ catch (TransformerException e)
+ {
+ throw new XmlModelWriterException(e);
+ }
+ }
+
+ public final Document getDocument()
+ {
+ return _doc;
+ }
+
+ public final Element getRootElement()
+ {
+ return _doc.getDocumentElement();
+ }
+
+ public final Element addElement(Element parent, String tagName) {
+ Element element = _doc.createElement(tagName);
+ parent.appendChild(element);
+ return element;
+ }
+
+ public final Element addElementWithText(Element parent, String tagName, String text) {
+ Element element = addElement(parent, tagName);
+ Text textNode = _doc.createTextNode(text);
+ element.appendChild(textNode);
+ return element;
+ }
+
+ public final void addComment(Element parent, String commentText) {
+ if (commentText != null) {
+ Comment comment = _doc.createComment(commentText);
+ parent.appendChild(comment);
+ }
+ }
+
+ private static void doIndent(Writer out, int indent)
+ throws IOException
+ {
+ for (int i = 0; i < indent; ++i) {
+ out.write(' ');
+ }
+ }
+
+ private static void writeElement(Writer out, Element element, int indent)
+ throws IOException
+ {
+ doIndent(out, indent);
+ out.write('<');
+ out.write(element.getTagName());
+ NamedNodeMap attrs = element.getAttributes();
+ for (int i = 0, len = attrs.getLength(); i < len; ++i) {
+ Node attr = attrs.item(i);
+ out.write(' ');
+ out.write(attr.getNodeName());
+ out.write("=\"");
+ out.write(attr.getNodeValue());
+ out.write('"');
+ }
+
+ String textContent = XmlElementSupport.getTextContent(element);
+ NodeList children = element.getChildNodes();
+ int childCount = children.getLength();
+ if (textContent != null) {
+ out.write('>');
+ out.write(textContent);
+ out.write("</");
+ out.write(element.getTagName());
+ out.write(">\n");
+ } else if (childCount > 0) {
+ out.write(">\n");
+ for (int i = 0; i < childCount; ++i) {
+ Node child = children.item(i);
+ if (child instanceof Comment) {
+ writeComment(out, (Comment) child, indent + INDENT_LEN);
+ } else if (child instanceof Text) {
+ assert XmlElementSupport.isWhiteSpace(child.getNodeValue()) : "expected only whitespace: " + child.getNodeValue();
+ } else {
+ assert child instanceof Element : child.getClass().getName();
+ writeElement(out, (Element) child, indent + INDENT_LEN);
+ }
+ }
+ doIndent(out, indent);
+ out.write("</");
+ out.write(element.getTagName());
+ out.write(">\n");
+ }
+ else {
+ out.write("/>\n");
+ }
+ }
+
+ private static void writeComment(Writer out, Comment comment, int indent)
+ throws IOException
+ {
+ doIndent(out, indent);
+ out.write("<!--");
+ out.write(comment.getNodeValue());
+ out.write("-->\n");
+ }
+}
Propchange: beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/XmlModelWriter.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/XmlModelWriterException.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/XmlModelWriterException.java?rev=289648&view=auto
==============================================================================
--- beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/XmlModelWriterException.java (added)
+++ beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/XmlModelWriterException.java Fri Sep 16 13:27:57 2005
@@ -0,0 +1,26 @@
+/*
+ * Copyright 2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * $Header:$
+ */
+package org.apache.beehive.netui.compiler.model;
+
+public class XmlModelWriterException extends Exception
+{
+ public XmlModelWriterException(Throwable cause)
+ {
+ super(cause);
+ }
+}
Propchange: beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/XmlModelWriterException.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/validation/LocaleSet.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/validation/LocaleSet.java?rev=289648&r1=289647&r2=289648&view=diff
==============================================================================
--- beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/validation/LocaleSet.java (original)
+++ beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/validation/LocaleSet.java Fri Sep 16 13:27:57 2005
@@ -17,8 +17,9 @@
*/
package org.apache.beehive.netui.compiler.model.validation;
-import org.apache.beehive.netui.compiler.model.schema.validator11.FormsetDocument;
-import org.apache.beehive.netui.compiler.model.schema.validator11.FormDocument;
+import org.apache.beehive.netui.compiler.model.XmlModelWriter;
+import org.apache.beehive.netui.compiler.model.XmlElementSupport;
+import org.w3c.dom.Element;
import java.util.Locale;
import java.util.HashMap;
@@ -26,6 +27,7 @@
import java.util.Iterator;
public class LocaleSet
+ extends XmlElementSupport
{
private Locale _locale;
private Map _entities = new HashMap();
@@ -56,50 +58,20 @@
_entities.put( entity.getEntityName(), entity );
}
- public void writeToXMLBean( FormsetDocument.Formset formset )
+ public void writeToElement(XmlModelWriter xw, Element element)
{
- if ( _locale != null )
- {
- formset.setLanguage( _locale.getLanguage() );
- if ( _locale.getCountry().length() > 0 )
- {
- formset.setCountry( _locale.getCountry() );
- if ( _locale.getVariant().length() > 0 )
- {
- formset.setVariant( _locale.getVariant() );
- }
- }
+ if ( _locale != null ) {
+ setElementAttribute(element, "language", _locale.getLanguage());
+ setElementAttribute(element, "country", _locale.getCountry());
+ setElementAttribute(element, "variant", _locale.getVariant());
}
- FormDocument.Form[] existingFormElements = formset.getFormArray();
for ( Iterator i = _entities.values().iterator(); i.hasNext(); )
{
ValidatableEntity entity = ( ValidatableEntity ) i.next();
String entityName = entity.getEntityName();
-
- //
- // Look for an existing element, or create one if none matches this entity name.
- //
- FormDocument.Form formElementToUse = null;
-
- for ( int j = 0; j < existingFormElements.length; j++ )
- {
- FormDocument.Form formElement = existingFormElements[j];
-
- if ( entityName.equals( formElement.getName() ) )
- {
- formElementToUse = formElement;
- break;
- }
- }
-
- if ( formElementToUse == null )
- {
- formElementToUse = formset.addNewForm();
- formElementToUse.setName( entityName );
- }
-
- entity.writeToXMLBean( formElementToUse );
+ Element formElementToUse = findChildElement(xw, element, "form", "name", entityName, true);
+ entity.writeXML(xw, formElementToUse);
}
}
}
Modified: beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/validation/ValidatableEntity.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/validation/ValidatableEntity.java?rev=289648&r1=289647&r2=289648&view=diff
==============================================================================
--- beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/validation/ValidatableEntity.java (original)
+++ beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/validation/ValidatableEntity.java Fri Sep 16 13:27:57 2005
@@ -17,14 +17,15 @@
*/
package org.apache.beehive.netui.compiler.model.validation;
-import org.apache.beehive.netui.compiler.model.schema.validator11.FormDocument;
-import org.apache.beehive.netui.compiler.model.schema.validator11.FieldDocument;
+import org.apache.beehive.netui.compiler.model.XmlElementSupport;
+import org.apache.beehive.netui.compiler.model.XmlModelWriter;
+import org.w3c.dom.Element;
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
-class ValidatableEntity
+class ValidatableEntity extends XmlElementSupport
{
private String _entityName;
private Map _fields = new HashMap();
@@ -50,38 +51,17 @@
return ( ValidatableField ) _fields.get( fieldName );
}
- public void writeToXMLBean( FormDocument.Form formElement )
+ public void writeToElement(XmlModelWriter xw, Element element)
{
- assert formElement.getName().equals( _entityName );
+ assert _entityName.equals(getElementAttribute(element, "name"))
+ : _entityName + ", " + getElementAttribute(element, "name");
- FieldDocument.Field[] existingFieldElements = formElement.getFieldArray();
for ( Iterator i = _fields.values().iterator(); i.hasNext(); )
{
ValidatableField field = ( ValidatableField ) i.next();
- FieldDocument.Field fieldElementToUse = null;
String fieldPropertyName = field.getPropertyName();
-
- //
- // Look for an existing field element to update, or create one if none matches this field's property name.
- //
- for ( int j = 0; j < existingFieldElements.length; j++ )
- {
- FieldDocument.Field existingFieldElement = existingFieldElements[j];
-
- if ( fieldPropertyName.equals( existingFieldElement.getProperty() ) )
- {
- fieldElementToUse = existingFieldElement;
- break;
- }
- }
-
- if ( fieldElementToUse == null )
- {
- fieldElementToUse = formElement.addNewField();
- fieldElementToUse.setProperty( fieldPropertyName );
- }
-
- field.writeToXMLBean( fieldElementToUse );
+ Element fieldElementToUse = findChildElement(xw, element, "field", "property", fieldPropertyName, true);
+ field.writeXML(xw, fieldElementToUse);
}
}
}
Modified: beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/validation/ValidatableField.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/validation/ValidatableField.java?rev=289648&r1=289647&r2=289648&view=diff
==============================================================================
--- beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/validation/ValidatableField.java (original)
+++ beehive/trunk/netui/src/compiler-core/org/apache/beehive/netui/compiler/model/validation/ValidatableField.java Fri Sep 16 13:27:57 2005
@@ -17,10 +17,9 @@
*/
package org.apache.beehive.netui.compiler.model.validation;
-import org.apache.beehive.netui.compiler.model.schema.validator11.ArgDocument;
-import org.apache.beehive.netui.compiler.model.schema.validator11.FieldDocument;
-import org.apache.beehive.netui.compiler.model.schema.validator11.MsgDocument;
-import org.apache.beehive.netui.compiler.model.schema.validator11.VarDocument;
+import org.apache.beehive.netui.compiler.model.XmlElementSupport;
+import org.apache.beehive.netui.compiler.model.XmlModelWriter;
+import org.w3c.dom.Element;
import java.util.ArrayList;
import java.util.Iterator;
@@ -28,19 +27,21 @@
import java.util.Map;
import java.util.Collections;
-public class ValidatableField
+public class ValidatableField extends XmlElementSupport
{
private String _propertyName;
private String _displayName;
private String _displayNameKey;
+ private boolean _isValidatorOneOne;
List _rules = new ArrayList();
- public ValidatableField( String propertyName, String displayName, String displayNameKey )
+ public ValidatableField(String propertyName, String displayName, String displayNameKey, boolean isValidatorOneOne)
{
_propertyName = propertyName;
_displayName = displayName;
_displayNameKey = displayNameKey;
+ _isValidatorOneOne = isValidatorOneOne;
}
public String getPropertyName()
@@ -78,20 +79,16 @@
/**
* Merge the rule names with the list in the field element's depends attribute.
- *
- * @param fieldElement the XMLBean field element in the validation XML to update
*/
- void mergeDependsList( FieldDocument.Field fieldElement )
+ void mergeDependsList(Element element)
{
- String depends = fieldElement.getDepends();
+ String depends = getElementAttribute(element, "depends");
StringBuffer updatedDepends = new StringBuffer();
- if ( depends != null )
- {
+ if ( depends != null ) {
updatedDepends.append( depends );
}
- else
- {
+ else {
depends = "";
}
@@ -113,15 +110,16 @@
if ( updatedDepends.length() != 0 )
{
- fieldElement.setDepends( updatedDepends.toString() );
+ element.setAttribute("depends", updatedDepends.toString());
}
}
- public void writeToXMLBean( FieldDocument.Field fieldElement )
+ public void writeToElement(XmlModelWriter xw, Element element)
{
- assert fieldElement.getProperty().equals( _propertyName );
+ assert _propertyName.equals(getElementAttribute(element, "property"))
+ : _propertyName + ", " + getElementAttribute(element, "property");
- mergeDependsList( fieldElement );
+ mergeDependsList(element);
//
// Add the display name as the default first argument (can be overridden by individual rules).
@@ -144,7 +142,7 @@
displayName = Character.toUpperCase( _propertyName.charAt( 0 ) ) + _propertyName.substring( 1 );
}
- setDefaultArg0Element( displayName, displayNameIsResource, fieldElement );
+ setDefaultArg0Element(xw, displayName, displayNameIsResource, element);
//
// Go through the rules, and add each one. Each rule can spray into...
@@ -155,15 +153,11 @@
for ( Iterator ii = _rules.iterator(); ii.hasNext(); )
{
ValidatorRule rule = ( ValidatorRule ) ii.next();
- //
+
// Add the message from the rule.
- //
- setRuleMessage( rule, fieldElement );
+ setRuleMessage(xw, rule, element);
- //
// Add vars from the rule.
- //
- VarDocument.Var[] existingVars = fieldElement.getVarArray();
Map ruleVars = rule.getVars();
if ( ruleVars != null )
@@ -172,32 +166,8 @@
{
Map.Entry entry = ( Map.Entry ) j.next();
String varName = ( String ) entry.getKey();
-
- //
- // Look for an existing var entry to update, or create one if there's none with the right name.
- //
- VarDocument.Var varElementToUse = null;
- for ( int k = 0; k < existingVars.length; k++ )
- {
- VarDocument.Var existingVar = existingVars[k];
-
- if ( varName.equals( existingVar.getVarName() ) )
- {
- varElementToUse = existingVar;
- break;
- }
- }
-
- if ( varElementToUse == null )
- {
- varElementToUse = fieldElement.addNewVar();
- varElementToUse.setVarName( varName );
- }
-
- if ( varElementToUse.getVarValue() == null )
- {
- varElementToUse.setVarValue( ( String ) entry.getValue() );
- }
+ Element varElementToUse = findChildElementWithChildText(xw, element, "var", "var-name", varName, true);
+ xw.addElementWithText(varElementToUse, "var-value", (String) entry.getValue());
}
}
@@ -206,46 +176,53 @@
// value from the rule.
//
Iterator j = ruleVars != null ? ruleVars.keySet().iterator() : null;
- setRuleArg( rule, 0, fieldElement, null );
- setRuleArg( rule, 1, fieldElement, j != null && j.hasNext() ? ( String ) j.next() : null );
- setRuleArg( rule, 2, fieldElement, j != null && j.hasNext() ? ( String ) j.next() : null );
- setRuleArg( rule, 3, fieldElement, j != null && j.hasNext() ? ( String ) j.next() : null );
+ setRuleArg(xw, rule, 0, element, null);
+ setRuleArg(xw, rule, 1, element, j != null && j.hasNext() ? ( String ) j.next() : null);
+ setRuleArg(xw, rule, 2, element, j != null && j.hasNext() ? ( String ) j.next() : null);
+ setRuleArg(xw, rule, 3, element, j != null && j.hasNext() ? ( String ) j.next() : null);
}
}
- /**
- * Find or create a default arg 0 element not associated with a
- * specific rule and set it to the display name.
- */
- void setDefaultArg0Element( String displayName, boolean displayNameIsResource, FieldDocument.Field fieldElement )
+ private Element getArgElement(XmlModelWriter xw, Element element, int argNum, String forRuleName, boolean create)
{
- ArgDocument.Arg[] argArray = fieldElement.getArgArray();
- ArgDocument.Arg defaultArg0Element = null;
-
- for ( int i = 0; i < argArray.length; i++ )
- {
- ArgDocument.Arg arg = argArray[i];
- if ( arg.getName() == null && "0".equals( arg.getPosition() ) )
+ if (_isValidatorOneOne) {
+ String strNum = new Integer(argNum).toString();
+ Element[] argArray = getChildElements(element, "arg");
+ for ( int i = 0; i < argArray.length; i++ )
{
- defaultArg0Element = arg;
- break;
+ Element arg = argArray[i];
+ String argRuleName = getElementAttribute(arg, "name");
+ if ((forRuleName == null && argRuleName == null)
+ || (forRuleName != null && forRuleName.equals(argRuleName)))
+ {
+ if(strNum.equals(getElementAttribute(arg, "position"))) {
+ return arg;
+ }
+ }
}
+
+ Element retVal = null;
+ if (create) {
+ retVal = xw.addElement(element, "arg");
+ setElementAttribute(retVal, "position", strNum);
+ }
+ return retVal;
+ } else {
+ return findChildElement(xw, element, "arg" + argNum, "name", forRuleName, create);
}
+ }
+ /**
+ * Find or create a default arg 0 element not associated with a specific rule and set it to the display name.
+ */
+ void setDefaultArg0Element(XmlModelWriter xw, String displayName, boolean displayNameIsResource, Element element)
+ {
+ Element defaultArg0Element = getArgElement(xw, element, 0, null, _rules.size() > 0);
- if ( defaultArg0Element == null && _rules.size() > 0 )
- {
- defaultArg0Element = fieldElement.addNewArg();
- defaultArg0Element.setPosition( "0" );
- }
-
- if ( defaultArg0Element != null )
+ if (defaultArg0Element != null)
{
- defaultArg0Element.setKey( displayName );
- defaultArg0Element.setResource( Boolean.toString( displayNameIsResource ) );
- if ( defaultArg0Element.getBundle() != null )
- {
- defaultArg0Element.setBundle( null );
- }
+ setElementAttribute(defaultArg0Element, "key", displayName);
+ setElementAttribute(defaultArg0Element, "resource", Boolean.toString(displayNameIsResource));
+ defaultArg0Element.removeAttribute("bundle");
}
}
@@ -253,51 +230,28 @@
* Set up the desired <msg> element and attributes for the given rule.
*
* @param rule the rule with the message to use
- * @param fieldElement an XMLBean field element in the validation XML to update
*/
- void setRuleMessage( ValidatorRule rule, FieldDocument.Field fieldElement )
+ void setRuleMessage(XmlModelWriter xw, ValidatorRule rule, Element element)
{
String messageKey = rule.getMessageKey();
String message = rule.getMessage();
if ( messageKey != null || message != null )
{
- MsgDocument.Msg[] existingMsgElements = fieldElement.getMsgArray();
- MsgDocument.Msg msgElementToUse = null;
-
- for ( int j = 0; j < existingMsgElements.length; j++ )
- {
- MsgDocument.Msg existingMsgElement = existingMsgElements[j];
- if ( rule.getRuleName().equals( existingMsgElement.getName() ) )
- {
- msgElementToUse = existingMsgElement;
- break;
- }
- }
-
- if ( msgElementToUse == null )
- {
- msgElementToUse = fieldElement.addNewMsg();
- msgElementToUse.setName( rule.getRuleName() );
- }
+ Element msgElementToUse = findChildElement(xw, element, "msg", "name", rule.getRuleName(), true);
+ setElementAttribute(msgElementToUse, "resource", true);
if ( messageKey != null )
{
- msgElementToUse.setKey( messageKey );
- msgElementToUse.setResource( Boolean.TRUE.toString() );
- String bundle = rule.getBundle();
- if ( bundle != null && bundle.length() > 0 )
- {
- msgElementToUse.setBundle( bundle );
+ setElementAttribute(msgElementToUse, "key", messageKey);
+ if (_isValidatorOneOne) {
+ setElementAttribute(msgElementToUse, "bundle", rule.getBundle());
}
}
else // message != null (it's a hardcoded message)
{
- //
// Add our special constant as the message key, append the hardcoded message to it.
- //
- msgElementToUse.setKey( ValidatorConstants.EXPRESSION_KEY_PREFIX + message );
- msgElementToUse.setResource( Boolean.TRUE.toString() );
+ setElementAttribute(msgElementToUse, "key", ValidatorConstants.EXPRESSION_KEY_PREFIX + message);
}
}
}
@@ -307,57 +261,38 @@
*
* @param rule the rule with the message and arg information to use
* @param argNum the position of the arg in the message
- * @param fieldElement an XMLBean field element in the validation XML to update
+ * @param element a <code><field></code> element in the validation XML to update
* @param altMessageVar alternative message var
*/
- void setRuleArg( ValidatorRule rule, int argNum, FieldDocument.Field fieldElement, String altMessageVar )
+ void setRuleArg(XmlModelWriter xw, ValidatorRule rule, int argNum, Element element, String altMessageVar)
{
- Integer argPosition = new Integer( argNum );
- String position = argPosition.toString();
- ValidatorRule.MessageArg arg = rule.getArg( argPosition );
+ Integer argPosition = new Integer( argNum );
+ ValidatorRule.MessageArg arg = rule.getArg( argPosition );
+ if ( arg != null || altMessageVar != null )
+ {
String ruleName = rule.getRuleName();
- ArgDocument.Arg[] argArray = fieldElement.getArgArray();
- ArgDocument.Arg argElementToUse = null;
-
- for ( int i = 0; i < argArray.length; i++ )
+ Element argElementToUse = getArgElement(xw, element, argNum, ruleName, true);
+
+ if ( arg != null )
{
- if ( ruleName.equals( argArray[i].getName() ) && position.equals( argArray[i].getPosition() ) )
- {
- argElementToUse = argArray[i];
- break;
+ String argMessage = arg.getMessage();
+ String key = arg.isKey() ? argMessage : ValidatorConstants.EXPRESSION_KEY_PREFIX + argMessage;
+ setElementAttribute(argElementToUse, "key", key);
+ setElementAttribute(argElementToUse, "resource", true);
+ if (arg.isKey() && _isValidatorOneOne) {
+ setElementAttribute(argElementToUse, "bundle", rule.getBundle());
}
}
-
- if ( arg != null || altMessageVar != null )
+ else
{
- if ( argElementToUse == null )
- {
- argElementToUse = fieldElement.addNewArg();
- }
-
- if ( arg != null )
- {
- String argMessage = arg.getMessage();
- String key = arg.isKey() ? argMessage : ValidatorConstants.EXPRESSION_KEY_PREFIX + argMessage;
- argElementToUse.setKey( key );
- argElementToUse.setResource( Boolean.TRUE.toString() );
- String bundle = rule.getBundle();
- if ( arg.isKey() && bundle != null && bundle.length() > 0 )
- {
- argElementToUse.setBundle( bundle );
- }
- }
- else
- {
- altMessageVar = "${var:" + altMessageVar + '}';
- argElementToUse.setKey( altMessageVar );
- argElementToUse.setResource( "false" );
- }
-
- argElementToUse.setPosition( position );
- argElementToUse.setName( ruleName );
+ altMessageVar = "${var:" + altMessageVar + '}';
+ setElementAttribute(argElementToUse, "key", altMessageVar);
+ setElementAttribute(argElementToUse, "resource", "false");
}
+
+ setElementAttribute(argElementToUse, "name", ruleName);
+ }
}
public String getDisplayName()