Mail Archive Home | fractal-commits List | June 2008 Index
| <-- Date Index --> | <-- Thread Index --> |
Cecilia ADL: as a prerequisite to implementing non-trivial initializers for attributes:
* change Cecilia DTD to:
* allow parsed character data within <attribute> elements;
* make the "value" attribute of the <attribute> element optional;
* create and insert into the toolchain a new Loader, AttributeModifierLoader, that transforms
<attribute name="...name..."><![CDATA[...definition...]]></attribute>
into
<attribute name="...name..." value="...definition..." />
so that the rest of the toolchain doesn't need any modifications.
* modify ConstAttributeMC test to test the new behaviour.
--- branches/cecilia-attr-nontrivialinit/cecilia-adl/src/main/java/org/objectweb/fractal/cecilia/adl/attributes/AttributeModifierLoader.java (rev 0)
+++ branches/cecilia-attr-nontrivialinit/cecilia-adl/src/main/java/org/objectweb/fractal/cecilia/adl/attributes/AttributeModifierLoader.java 2008-06-30 11:48:24 UTC (rev 7991)
@@ -0,0 +1,223 @@
+/***
+ * Cecilia ADL Compiler
+ * Copyright (C) 2008 INRIA
+ *
+ * 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 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
+ *
+ * Contact: fractal@xxxxxxxxxxxxx
+ *
+ * Author: Lionel Debroux
+ *
+ * Contributors: Ali Erdem Ozcan, Matthieu Leclercq
+ * (for the code in AttributeLoader, of which this file is inspired)
+ */
+
+package org.objectweb.fractal.cecilia.adl.attributes;
+
+import static org.objectweb.fractal.adl.NodeUtil.castNodeError;
+
+import java.util.Map;
+
+import org.objectweb.fractal.adl.AbstractLoader;
+import org.objectweb.fractal.adl.ADLException;
+import org.objectweb.fractal.adl.CompilerError;
+import org.objectweb.fractal.adl.Definition;
+import org.objectweb.fractal.adl.Loader;
+import org.objectweb.fractal.adl.Node;
+import org.objectweb.fractal.adl.attributes.Attribute;
+import org.objectweb.fractal.adl.attributes.AttributeErrors;
+import org.objectweb.fractal.adl.attributes.Attributes;
+import org.objectweb.fractal.adl.attributes.AttributesContainer;
+import org.objectweb.fractal.adl.error.GenericErrors;
+import org.objectweb.fractal.adl.xml.XMLNode;
+import org.objectweb.fractal.adl.xml.XMLNodeFactory;
+import org.objectweb.fractal.api.NoSuchInterfaceException;
+import org.objectweb.fractal.api.control.IllegalBindingException;
+import org.objectweb.fractal.cecilia.adl.CeciliaADLConstants;
+import org.xml.sax.SAXException;
+
+/**
+ * A {@link org.objectweb.fractal.adl.Loader} to handle {@link Attributes} nodes
+ * that contain {@link Attribute} nodes of the special form
+ * <attribute name="...name..."><![CDATA[...definition...]]></attribute>
+ * This form is the key to non-trivial initializers for attributes.
+ *
+ * This loader transforms this special form of {@link Attribute} to the
+ * usual form
+ * <attribute name="...name..." value="...definition..." />
+ */
+public class AttributeModifierLoader extends AbstractLoader {
+
+ // ---------------------------------------------------------------------------
+ // Client interfaces
+ // ---------------------------------------------------------------------------
+
+ /** The {@link XMLNodeFactory} client interface used by this component. */
+ protected XMLNodeFactory nodeFactoryItf;
+
+ // ---------------------------------------------------------------------------
+ // Overridden methods
+ // ---------------------------------------------------------------------------
+
+ @Override
+ public Definition load(String name, Map<Object, Object> context)
+ throws ADLException {
+ Definition d = clientLoader.load(name, context);
+ //System.out.println("Name of definition: " + d.getName());
+ //System.out.println("AST source of definition: " + d.astGetSource());
+
+ handleNodePotentiallyContainingAttribute(d);
+
+ return d;
+ }
+
+ private void handleNodePotentiallyContainingAttribute(Node n0) {
+ // Recursively handle <attributes> nodes in sub-components.
+ Node[] components = n0.astGetNodes("component");
+ for (Node n : components) {
+ if (n != null) {
+ handleNodePotentiallyContainingAttribute(n);
+ }
+ }
+
+ // Handle <attribute(s)> nodes which are direct sub-nodes
+ // of this node.
+ Node[] attributes = n0.astGetNodes("attributes");
+ for (Node n : attributes) {
+ if (n != null) {
+ Node[] attribute = n.astGetNodes("attribute");
+ int attrCount = 0;
+
+ // Count <attribute> nodes.
+ for (Node n2 : attribute) {
+ if (n2 != null) {
+ attrCount++;
+ }
+ }
+
+ // Now handle them. Create a new array of attributes,
+ // which will replace that in n.
+ Node[] newAttribute = new Node[attrCount];
+
+ attrCount = 0;
+ for (Node n2 : attribute) {
+ if (n2 != null) {
+ final String attrName = ((Attribute)n2).getName();
+ final String attrValue = ((Attribute)n2).getValue();
+ final String attrContent = ((XMLNode)n2).xmlGetContent();
+ // When attrValue != null, don't bother with the contents of the
+ // XML node.
+ if ((attrValue == null) && (attrContent != null)) {
+ // There's no "value" attribute to this <attribute> XML element,
+ // but there's parsed character data (PCDATA) in it.
+ // Try creating value from the PCDATA contents of the XML node.
+ Attribute attrNode;
+ try {
+ attrNode = castNodeError(nodeFactoryItf.newXMLNode(
+ CeciliaADLConstants.CECILIA_ADL_DTD,
+ CeciliaADLConstants.ATTRIBUTE_AST_NODE_NAME),
+ Attribute.class);
+ } catch (final SAXException e) {
+ throw new CompilerError(GenericErrors.INTERNAL_ERROR, e, e
+ .getMessage());
+ }
+
+ // Creation was successful,
+ attrNode.setName(attrName);
+ attrNode.setValue(attrContent);
+
+ newAttribute[attrCount] = attrNode;
+ }
+ else {
+ newAttribute[attrCount] = attribute[attrCount];
+ }
+ //System.out.println(attrName);
+ //System.out.println(attrValue);
+ //System.out.println("\"" + attrContent + "\"");
+
+ attrCount++;
+ }
+ }
+
+ // Perform replacement of attributes
+ for (final Node attr : attribute) {
+ n.astRemoveNode(attr);
+ }
+
+ for (final Node attr : newAttribute) {
+ n.astAddNode(attr);
+ }
+
+ }
+ }
+ }
+
+ // ---------------------------------------------------------------------------
+ // Implementation of the BindingController interface
+ // ---------------------------------------------------------------------------
+
+ @Override
+ public void bindFc(final String s, final Object o)
+ throws NoSuchInterfaceException, IllegalBindingException {
+
+ if (s == null) {
+ throw new IllegalArgumentException("Interface name can't be null");
+ }
+
+ if (s.equals(XMLNodeFactory.ITF_NAME)) {
+ nodeFactoryItf = (XMLNodeFactory) o;
+ } else {
+ super.bindFc(s, o);
+ }
+ }
+
+ @Override
+ public String[] listFc() {
+ final String[] superList = super.listFc();
+ final String[] list = new String[superList.length + 1];
+ list[0] = XMLNodeFactory.ITF_NAME;
+ System.arraycopy(superList, 0, list, 1, superList.length);
+ return list;
+ }
+
+ @Override
+ public Object lookupFc(final String s) throws NoSuchInterfaceException {
+
+ if (s == null) {
+ throw new IllegalArgumentException("Interface name can't be null");
+ }
+
+ if (s.equals(XMLNodeFactory.ITF_NAME)) {
+ return nodeFactoryItf;
+ } else {
+ return super.lookupFc(s);
+ }
+ }
+
+ @Override
+ public void unbindFc(final String s) throws IllegalBindingException,
+ NoSuchInterfaceException {
+
+ if (s == null) {
+ throw new IllegalArgumentException("Interface name can't be null");
+ }
+
+ if (s.equals(XMLNodeFactory.ITF_NAME)) {
+ nodeFactoryItf = null;
+ } else {
+ super.unbindFc(s);
+ }
+ }
+}
--- branches/cecilia-attr-nontrivialinit/cecilia-adl/src/main/resources/org/objectweb/fractal/cecilia/adl/CeciliaLoader.fractal 2008-06-30 11:47:35 UTC (rev 7990)
+++ branches/cecilia-attr-nontrivialinit/cecilia-adl/src/main/resources/org/objectweb/fractal/cecilia/adl/CeciliaLoader.fractal 2008-06-30 11:48:24 UTC (rev 7991)
@@ -38,6 +38,13 @@
<content class="org.objectweb.fractal.adl.arguments.ArgumentLoader" />
</component>
+ <component name="attribute-modifier-loader"
+ definition="org.objectweb.fractal.adl.DelegateLoaderType">
+ <interface name="node-factory" role="client"
+ signature="org.objectweb.fractal.adl.xml.XMLNodeFactory" />
+ <content class="org.objectweb.fractal.cecilia.adl.attributes.AttributeModifierLoader" />
+ </component>
+
<component name="type-loader"
definition="org.objectweb.fractal.adl.DelegateLoaderType">
<interface name="interface-loader" role="client"
@@ -144,7 +151,8 @@
<binding client="language-loader.client-loader"
server="type-loader.loader" />
<binding client="type-loader.client-loader" server="component-loader.loader" />
- <binding client="component-loader.client-loader" server="argument-loader.loader" />
+ <binding client="component-loader.client-loader" server="attribute-modifier-loader.loader" />
+ <binding client="attribute-modifier-loader.client-loader" server="argument-loader.loader" />
<binding client="argument-loader.client-loader"
server="parser-selector-loader.loader" />
@@ -160,6 +168,8 @@
server="node-factory.node-factory" />
<binding client="language-loader.node-factory"
server="node-factory.node-factory" />
+ <binding client="attribute-modifier-loader.node-factory"
+ server="node-factory.node-factory" />
<binding client="attribute-loader.node-factory"
server="node-factory.node-factory" />
<binding client="IDL-loader.node-factory" server="node-factory.node-factory" />
--- branches/cecilia-attr-nontrivialinit/cecilia-adl/src/main/resources/org/objectweb/fractal/cecilia/adl/parser/xml/cecilia.dtd 2008-06-30 11:47:35 UTC (rev 7990)
+++ branches/cecilia-attr-nontrivialinit/cecilia-adl/src/main/resources/org/objectweb/fractal/cecilia/adl/parser/xml/cecilia.dtd 2008-06-30 11:48:24 UTC (rev 7991)
@@ -161,10 +161,10 @@
signature CDATA #IMPLIED
>
-<!ELEMENT attribute (comment*) >
+<!ELEMENT attribute (#PCDATA|comment)* >
<!ATTLIST attribute
name CDATA #REQUIRED
- value CDATA #REQUIRED
+ value CDATA #IMPLIED
>
<!ELEMENT controller (comment*) >
--- branches/cecilia-attr-nontrivialinit/cecilia-adl/src/test/resources/test/attributes/ConstAttributeMC.fractal 2008-06-30 11:47:35 UTC (rev 7990)
+++ branches/cecilia-attr-nontrivialinit/cecilia-adl/src/test/resources/test/attributes/ConstAttributeMC.fractal 2008-06-30 11:48:24 UTC (rev 7991)
@@ -6,8 +6,8 @@
<component name="test">
<content class="test.attributes.constAttribute" language="thinkMC"/>
<attributes signature="test.attributes.AttributesWithConstField" >
- <attribute name="x" value="1" />
- <attribute name="y" value="2" />
+ <attribute name="x"><![CDATA[1]]></attribute>
+ <attribute name="y"><![CDATA[2]]></attribute>
<attribute name="s" value="foo" />
</attributes>
</component>
| <-- Date Index --> | <-- Thread Index --> |
Powered by MHonArc.
Copyright © 2006-2007, OW2 Consortium | contact | webmaster.