Author: ajaquith
Date: Sun Oct 5 08:58:13 2008
New Revision: 701813
URL: http://svn.apache.org/viewvc?rev=701813&view=rev
Log:
Near-final refactoring of JspParser. Now successfully detects <link> and <meta>
tags, and DOCTYPE declaration. Successfully detects tag imbalances, too!
Invaluable for finding 5 existing JSPs that are not XHTML compliant.
Modified:
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/JSPWikiJspTransformerTest.java
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/JspParser.java
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/JspParserTest.java
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/NodeType.java
Modified:
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/JSPWikiJspTransformerTest.java
URL:
http://svn.apache.org/viewvc/incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/JSPWikiJspTransformerTest.java?rev=701813&r1=701812&r2=701813&view=diff
==============================================================================
---
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/JSPWikiJspTransformerTest.java
(original)
+++
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/JSPWikiJspTransformerTest.java
Sun Oct 5 08:58:13 2008
@@ -1,6 +1,5 @@
package com.ecyrd.jspwiki.ui.stripes;
-import java.io.File;
import java.util.HashMap;
import java.util.Map;
@@ -73,15 +72,6 @@
assertEquals( "form", node.getName() );
}
- public void testParseWeirdDoc() throws Exception
- {
- File src = new File( "src/webdocs/Captcha.jsp" );
- String s = JspMigrator.readSource( src );
-
- JspDocument doc = new JspParser().parse( s );
- m_transformer.transform( m_sharedState, doc );
- }
-
public static Test suite()
{
return new TestSuite( JSPWikiJspTransformerTest.class );
Modified:
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/JspParser.java
URL:
http://svn.apache.org/viewvc/incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/JspParser.java?rev=701813&r1=701812&r2=701813&view=diff
==============================================================================
---
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/JspParser.java
(original)
+++
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/JspParser.java
Sun Oct 5 08:58:13 2008
@@ -180,7 +180,7 @@
// Figure out what this tag is
String lookahead = ctx.lookahead( 4 );
- String lookahead9 = ctx.lookahead( 9 );
+ String lookahead10= ctx.lookahead( 10 );
JspDocument doc = ctx.getDocument();
// <%- means hidden JSP comment
@@ -223,8 +223,14 @@
}
}
+ // <!DOCTYPE means DOCTYPE
+ else if ( lookahead10.startsWith( NodeType.DOCTYPE.getTagStart() )
)
+ {
+ initNode( new Text( doc, NodeType.DOCTYPE ),
Stage.CODE_OR_COMMENT );
+ }
+
// <![CDATA[ means CDATA
- else if( lookahead9.startsWith( NodeType.CDATA.getTagStart() ) )
+ else if( lookahead10.startsWith( NodeType.CDATA.getTagStart() ) )
{
initNode( new Text( doc, NodeType.CDATA ),
Stage.CODE_OR_COMMENT );
}
@@ -328,7 +334,7 @@
case CODE_OR_COMMENT: {
switch( ch )
{
- // Terminating %> or --> means the end of the comment
+ // Terminating %>, > or --> means the end of the
comment (or DOCTYPE)
case ('>'): {
String tagEnd = node.getType().getTagEnd();
String lookbehind = ctx.lookbehind(
tagEnd.length() );
@@ -384,6 +390,16 @@
{
ParseContext ctx = ParseContext.currentContext();
Node node = ctx.getNode();
+
+ // Special case if we have a META or LINK tag
+ if ( "LINK".equals( node.getName() ) )
+ {
+ node.setType( NodeType.HTML_LINK );
+ }
+ else if ( "META".equals( node.getName() ) )
+ {
+ node.setType( NodeType.HTML_META );
+ }
// Resolve tag type if not set
if( node.getType() == NodeType.UNRESOLVED_HTML_TAG )
@@ -421,7 +437,23 @@
// Make end tag the peer of the start tag
Node startTag = node.getParent();
node.setParent( startTag.getParent() );
-
+
+ // Does the end tag match the start tag?
+ if ( !node.getName().equals( startTag.getName() ) )
+ {
+ if ( startTag.equals( ctx.getDocument().getRoot() ) )
+ {
+ throw new IllegalStateException( "Encountered end
tag </"
+ + node.getName()
+ "> (" + node.getLine() + "," + node.getColumn() + " char " + node.getStart()
+ + ") that did
have a matching start tag." );
+ }
+ throw new IllegalStateException( "Encountered end tag
</"
+ + node.getName() + ">
(" + node.getLine() + "," + node.getColumn() + " char " + node.getStart()
+ + ") that did not
match start tag <"
+ + startTag.getName()
+ "> (" + startTag.getLine() + "," + startTag.getColumn() + " char " +
startTag.getEnd() + ")"
+ );
+ }
+
// Get rid of the current node in the parent context
ctx = ParseContext.pop();
ctx.setNode( null );
Modified:
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/JspParserTest.java
URL:
http://svn.apache.org/viewvc/incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/JspParserTest.java?rev=701813&r1=701812&r2=701813&view=diff
==============================================================================
---
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/JspParserTest.java
(original)
+++
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/JspParserTest.java
Sun Oct 5 08:58:13 2008
@@ -14,6 +14,69 @@
super( s );
}
+ public void testParseDoctype() throws Exception
+ {
+ String s = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0
Transitional//EN\" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
+
+ // Parse the contents
+ JspParser parser = new JspParser();
+ JspDocument doc = parser.parse( s );
+
+ // Verify three nodes total
+ List<Node> nodes = doc.getNodes();
+ assertEquals( 1, nodes.size() );
+ Node node = nodes.get( 0 );
+ assertEquals( "(TEXT)", node.getName() );
+ assertEquals( NodeType.DOCTYPE, node.getType() );
+ assertEquals( "html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"", node.getValue() );
+ }
+
+ public void testMeta() throws Exception
+ {
+ String s = "<META name=\"Author\" content=\"Dave Raggett\">";
+
+ // Parse the contents
+ JspParser parser = new JspParser();
+ JspDocument doc = parser.parse( s );
+
+ // Verify three nodes total
+ List<Node> nodes = doc.getNodes();
+ assertEquals( 1, nodes.size() );
+ Tag tag = (Tag)nodes.get( 0 );
+ assertEquals( "META", tag.getName() );
+ assertEquals( NodeType.HTML_META, tag.getType() );
+ assertEquals( 2, tag.getAttributes().size() );
+ assertEquals( "name", tag.getAttribute( "name" ).getName() );
+ assertEquals( "Author", tag.getAttribute( "name" ).getValue() );
+ assertEquals( "content", tag.getAttribute( "content" ).getName() );
+ assertEquals( "Dave Raggett", tag.getAttribute( "content" ).getValue()
);
+ }
+
+ public void testLink() throws Exception
+ {
+ String s = "<LINK rel=\"Start\" title=\"First\" type=\"text/html\"
href=\"http://start.html\">";
+
+ // Parse the contents
+ JspParser parser = new JspParser();
+ JspDocument doc = parser.parse( s );
+
+ // Verify three nodes total
+ List<Node> nodes = doc.getNodes();
+ assertEquals( 1, nodes.size() );
+ Tag tag = (Tag)nodes.get( 0 );
+ assertEquals( "LINK", tag.getName() );
+ assertEquals( NodeType.HTML_LINK, tag.getType() );
+ assertEquals( 4, tag.getAttributes().size() );
+ assertEquals( "rel", tag.getAttribute( "rel" ).getName() );
+ assertEquals( "Start", tag.getAttribute( "rel" ).getValue() );
+ assertEquals( "title", tag.getAttribute( "title" ).getName() );
+ assertEquals( "First", tag.getAttribute( "title" ).getValue() );
+ assertEquals( "type", tag.getAttribute( "type" ).getName() );
+ assertEquals( "text/html", tag.getAttribute( "type" ).getValue() );
+ assertEquals( "href", tag.getAttribute( "href" ).getName() );
+ assertEquals( "http://start.html", tag.getAttribute( "href"
).getValue() );
+ }
+
public void testNestedAttributes() throws Exception
{
String s = "<a <b test=\"c\">selected=\"d\"</b> >Foo</a>";
Modified:
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/NodeType.java
URL:
http://svn.apache.org/viewvc/incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/NodeType.java?rev=701813&r1=701812&r2=701813&view=diff
==============================================================================
---
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/NodeType.java
(original)
+++
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/ui/stripes/NodeType.java
Sun Oct 5 08:58:13 2008
@@ -15,10 +15,16 @@
TEXT("", ""),
/** CDATA node. */
CDATA("<![CDATA[","]]>"),
+ /** DOCTYPE declaration */
+ DOCTYPE("<!DOCTYPE ", ">"),
/** HTML comment tag */
HTML_COMMENT("<!--", "-->"),
/** HTML start tag */
HTML_START_TAG("<", ">"),
+ /** HTML LINK tag. */
+ HTML_LINK("<",">"),
+ /** HTML META tag. */
+ HTML_META("<",">"),
/** HTML end tag */
HTML_END_TAG("</", ">"),
/** HTML end tag */