Mail Archive Home | fractal-commits List | July 2008 Index
| <-- Date Index --> | <-- Thread Index --> |
Add 'javadoc like' comment support in XMLParser Javadoc-like comments are attached as decoration to AST nodes In XML files a 'javadoc-like comments starts with "<!---" and new lines may starts with a "-" (which is removed)
--- trunk/fractaladl/toolchain/ast-core/src/main/java/org/objectweb/fractal/adl/comments/CommentDecoration.java (rev 0)
+++ trunk/fractaladl/toolchain/ast-core/src/main/java/org/objectweb/fractal/adl/comments/CommentDecoration.java 2008-07-25 11:47:46 UTC (rev 8320)
@@ -0,0 +1,94 @@
+/**
+ * Fractal ADL Parser
+ * Copyright (C) 2008 STMicroelectronics
+ *
+ * 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: Matthieu Leclercq
+ */
+
+package org.objectweb.fractal.adl.comments;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.objectweb.fractal.adl.Node;
+import org.objectweb.fractal.adl.merger.MergeException;
+import org.objectweb.fractal.adl.merger.MergeableDecoration;
+
+/**
+ * A decoration that can be used to attach "javadoc-like" comments on AST nodes.
+ */
+public class CommentDecoration implements MergeableDecoration {
+
+ private final String comment;
+ private final Node node;
+
+ private List<CommentDecoration> overriddenComment;
+
+ /**
+ * Create a new <code>CommentDecoration</code>.
+ *
+ * @param comment the comment contained by this decoration.
+ * @param node the node to which this comment is attached.
+ */
+ public CommentDecoration(final String comment, final Node node) {
+ this.comment = comment;
+ this.node = node;
+ }
+
+ /**
+ * Returns the comment contained by this decoration.
+ *
+ * @return the comment contained by this decoration.
+ */
+ public String getComment() {
+ return comment;
+ }
+
+ /**
+ * Returns the node to which this comment is attached.
+ *
+ * @return the node to which this comment is attached.
+ */
+ public Node getNode() {
+ return node;
+ }
+
+ /**
+ * Returns the comments that are overridden by this comment.
+ *
+ * @return the comments that are overridden by this comment.
+ */
+ public List<CommentDecoration> getOverriddenComment() {
+ return overriddenComment;
+ }
+
+ public Object mergeDecoration(final Object overridingDecoration)
+ throws MergeException {
+ if (!(overridingDecoration instanceof CommentDecoration)) {
+ return overridingDecoration;
+ }
+ final CommentDecoration overridingComment = (CommentDecoration) overridingDecoration;
+ if (overridingComment.overriddenComment == null)
+ overridingComment.overriddenComment = new ArrayList<CommentDecoration>();
+
+ overridingComment.overriddenComment.add(this);
+ return overridingComment;
+ }
+
+}
Property changes on: trunk/fractaladl/toolchain/ast-core/src/main/java/org/objectweb/fractal/adl/comments/CommentDecoration.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
--- trunk/fractaladl/toolchain/ast-core/src/main/java/org/objectweb/fractal/adl/comments/CommentDecorationHelper.java (rev 0)
+++ trunk/fractaladl/toolchain/ast-core/src/main/java/org/objectweb/fractal/adl/comments/CommentDecorationHelper.java 2008-07-25 11:47:46 UTC (rev 8320)
@@ -0,0 +1,101 @@
+/**
+ * Fractal ADL Parser
+ * Copyright (C) 2008 STMicroelectronics
+ *
+ * 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: Matthieu Leclercq
+ */
+
+package org.objectweb.fractal.adl.comments;
+
+import org.objectweb.fractal.adl.Node;
+
+/**
+ * Provides helper methods to manipulate {@link CommentDecoration}.
+ */
+public final class CommentDecorationHelper {
+
+ private CommentDecorationHelper() {
+ }
+
+ /**
+ * The decoration name used to attach {@link CommentDecoration} to nodes.
+ */
+ public static final String COMMENT_DECORATION_NAME = "comment";
+
+ /**
+ * Attach the given comment to the given node.
+ *
+ * @param node a node.
+ * @param comment the comment to attach.
+ * @throws IllegalArgumentException if the given node already has a decoration
+ * called {@link #COMMENT_DECORATION_NAME}.
+ */
+ public static void setComment(final Node node, final String comment) {
+ final Object o = node.astGetDecoration(COMMENT_DECORATION_NAME);
+ if (o != null)
+ throw new IllegalArgumentException("Given node already contains a \""
+ + COMMENT_DECORATION_NAME + "\" decoration.");
+
+ node.astSetDecoration(COMMENT_DECORATION_NAME, new CommentDecoration(
+ comment, node));
+ }
+
+ /**
+ * Return the comment that is attached to the given node.
+ *
+ * @param node a node.
+ * @return the comment that is attached to the given node, or
+ * <code>null</code> if the given node has no decoration called
+ * {@link #COMMENT_DECORATION_NAME}.
+ * @throws IllegalArgumentException if the given node has a decoration called
+ * {@link #COMMENT_DECORATION_NAME} that is not a
+ * {@link CommentDecoration}.
+ */
+ public static String getComment(final Node node) {
+ final Object deco = node.astGetDecoration(COMMENT_DECORATION_NAME);
+ if (deco == null) return null;
+ if (!(deco instanceof CommentDecoration))
+ throw new IllegalArgumentException("Given node contains a \""
+ + COMMENT_DECORATION_NAME
+ + "\" decoration that is not a CommentDecoration.");
+ return ((CommentDecoration) deco).getComment();
+ }
+
+ /**
+ * Return the {@link CommentDecoration} object that is attached to the given
+ * node.
+ *
+ * @param node a node.
+ * @return the {@link CommentDecoration} object that is attached to the given
+ * node, or <code>null</code> if the given node has no decoration
+ * called {@link #COMMENT_DECORATION_NAME}.
+ * @throws IllegalArgumentException if the given node has a decoration called
+ * {@link #COMMENT_DECORATION_NAME} that is not a
+ * {@link CommentDecoration}.
+ */
+ public static CommentDecoration getCommentDecoration(final Node node) {
+ final Object deco = node.astGetDecoration(COMMENT_DECORATION_NAME);
+ if (deco == null) return null;
+ if (!(deco instanceof CommentDecoration))
+ throw new IllegalArgumentException("Given node contains a \""
+ + COMMENT_DECORATION_NAME
+ + "\" decoration that is not a CommentDecoration.");
+ return (CommentDecoration) deco;
+ }
+}
Property changes on: trunk/fractaladl/toolchain/ast-core/src/main/java/org/objectweb/fractal/adl/comments/CommentDecorationHelper.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
--- trunk/fractaladl/toolchain/ast-core/src/main/java/org/objectweb/fractal/adl/xml/XMLParser.java 2008-07-24 10:14:40 UTC (rev 8319)
+++ trunk/fractaladl/toolchain/ast-core/src/main/java/org/objectweb/fractal/adl/xml/XMLParser.java 2008-07-25 11:47:46 UTC (rev 8320)
@@ -39,11 +39,13 @@
import org.objectweb.fractal.adl.Node;
import org.objectweb.fractal.adl.Parser;
import org.objectweb.fractal.adl.ParserException;
+import org.objectweb.fractal.adl.comments.CommentDecorationHelper;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
+import org.xml.sax.ext.LexicalHandler;
import org.xml.sax.helpers.DefaultHandler;
/**
@@ -51,7 +53,7 @@
* trees.
*/
-public class XMLParser extends DefaultHandler implements Parser {
+public class XMLParser extends DefaultHandler implements Parser, LexicalHandler {
/** The parser factory used to create new parsers. */
SAXParserFactory spf;
@@ -74,6 +76,9 @@
/** Content of characters. */
StringBuilder characters;
+ /** The comment */
+ String comment;
+
/** The locator used to keep track of line numbers. */
Locator locator;
@@ -81,9 +86,9 @@
XMLNodeFactory nodeFactory;
- // --------------------------------------------------------------------------
+ // ---------------------------------------------------------------------------
// Constructor
- // --------------------------------------------------------------------------
+ // ---------------------------------------------------------------------------
/**
* Creates a new {@link XMLParser}.
@@ -107,15 +112,16 @@
this.nodeFactory = nodeFactory;
}
- // --------------------------------------------------------------------------
+ // ---------------------------------------------------------------------------
// Implementation of the Parser interface
- // --------------------------------------------------------------------------
+ // ---------------------------------------------------------------------------
public Node parse(final InputStream is, final String name)
throws ParserException {
file = name;
try {
final SAXParser sp = spf.newSAXParser();
+ sp.setProperty("http://xml.org/sax/properties/lexical-handler", this);
sp.parse(new InputSource(is), this);
return result;
} catch (final IOException e) {
@@ -127,9 +133,9 @@
}
}
- // --------------------------------------------------------------------------
+ // ---------------------------------------------------------------------------
// Overriden DefaultHandler methods
- // --------------------------------------------------------------------------
+ // ---------------------------------------------------------------------------
@Override
public void setDocumentLocator(final Locator locator) {
@@ -163,6 +169,11 @@
final XMLNode o = nodeFactory.newXMLNode(systemId, qualifiedName);
o.astSetSource(file + ":" + locator.getLineNumber());
o.xmlSetAttributes(attributes);
+ if (comment != null) {
+ CommentDecorationHelper.setComment(o, comment);
+ comment = null;
+ }
+
if (stack.size() == 0) {
result = o;
} else {
@@ -212,4 +223,49 @@
line = locator.getLineNumber();
throw e;
}
+
+ // ---------------------------------------------------------------------------
+ // implementation of LexicalHandler interface
+ // ---------------------------------------------------------------------------
+
+ /*
+ * Assumes that every comments are received completely in a single call to
+ * this method.
+ */
+ public void comment(final char[] ch, final int start, final int length)
+ throws SAXException {
+ final int end = start + length;
+ int i = start;
+
+ // search first non-whitespace character.
+ while (i < end && Character.isWhitespace(ch[i]))
+ i++;
+ if (i == end) return;
+
+ if (ch[i] != '-') return;
+ i++;
+
+ String s = new String(ch, i, end - i).trim();
+ s = s.replaceAll("\\n(\\s)*(-)*(\\s)+", " ");
+ comment = (comment == null) ? s : comment + " " + s;
+ }
+
+ public void endCDATA() throws SAXException {
+ }
+
+ public void endDTD() throws SAXException {
+ }
+
+ public void endEntity(final String name) throws SAXException {
+ }
+
+ public void startCDATA() throws SAXException {
+ }
+
+ public void startDTD(final String name, final String publicId,
+ final String systemId) throws SAXException {
+ }
+
+ public void startEntity(final String name) throws SAXException {
+ }
}
--- trunk/fractaladl/toolchain/ast-core/src/test/java/org/objectweb/fractal/adl/xml/XMLParserTest.java 2008-07-24 10:14:40 UTC (rev 8319)
+++ trunk/fractaladl/toolchain/ast-core/src/test/java/org/objectweb/fractal/adl/xml/XMLParserTest.java 2008-07-25 11:47:46 UTC (rev 8320)
@@ -34,11 +34,13 @@
import org.objectweb.fractal.adl.Node;
import org.objectweb.fractal.adl.comments.Comment;
import org.objectweb.fractal.adl.comments.CommentContainer;
+import org.objectweb.fractal.adl.comments.CommentDecorationHelper;
import org.objectweb.fractal.adl.comments.ExtendedComment;
public class XMLParserTest {
- private static final String FIXTURE_XML = "org/objectweb/fractal/adl/xml/comment.xml";
+ private static final String FIXTURE_XML = "org/objectweb/fractal/adl/xml/comment.xml";
+ private static final String EXPECTED_COMMENT = "This is the documentation of the container top level node. This comment is continuing on this new line";
protected XMLParser parser;
protected XMLNodeFactoryImpl nodeFactory;
@@ -56,6 +58,7 @@
final Node n = parser.parse(is, FIXTURE_XML);
Assert.assertTrue(n instanceof CommentContainer);
+ assertEquals(EXPECTED_COMMENT, CommentDecorationHelper.getComment(n));
final Comment[] comments = ((CommentContainer) n).getComments();
Assert.assertEquals(3, comments.length);
--- trunk/fractaladl/toolchain/ast-core/src/test/resources/org/objectweb/fractal/adl/xml/comment.xml 2008-07-24 10:14:40 UTC (rev 8319)
+++ trunk/fractaladl/toolchain/ast-core/src/test/resources/org/objectweb/fractal/adl/xml/comment.xml 2008-07-25 11:47:46 UTC (rev 8320)
@@ -2,6 +2,10 @@
<!DOCTYPE container PUBLIC "-//objectweb.org//DTD Fractal ADL 2.0//EN"
"classpath://org/objectweb/fractal/adl/xml/comment.dtd">
+<!--- This is the documentation of the container top level node. This comment
+ - is continuing on this new line
+-->
+<!-- this is an ignored comment -->
<container>
<comment text="foo" />
<extcomment text="bar" href="" text="titi"/>fuu</extcomment>
| <-- Date Index --> | <-- Thread Index --> |
Powered by MHonArc.
Copyright © 2006-2007, OW2 Consortium | contact | webmaster.