[Httpunit-commit] CVS: httpunit/src/com/meterware/httpunit/parsing ScriptHandler.java,NONE,1.1 Docum
Brought to you by:
russgold
|
From: Russell G. <rus...@us...> - 2006-03-28 00:52:20
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/parsing In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11573/src/com/meterware/httpunit/parsing Modified Files: DocumentAdapter.java HTMLParserFactory.java JTidyHTMLParser.java NekoDOMParser.java NekoHTMLParser.java ScriptFilter.java Added Files: ScriptHandler.java Log Message: Created custom DOM ***** Error reading new file[Errno 2] No such file or directory: 'ScriptHandler.java' Index: DocumentAdapter.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/parsing/DocumentAdapter.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- DocumentAdapter.java 25 Dec 2002 15:23:11 -0000 1.2 +++ DocumentAdapter.java 28 Mar 2006 00:52:15 -0000 1.3 @@ -2,7 +2,7 @@ /******************************************************************************************************************** * $Id$ * - * Copyright (c) 2002, Russell Gold + * Copyright (c) 2002,2004, Russell Gold * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated * documentation files (the "Software"), to deal in the Software without restriction, including without limitation @@ -21,7 +21,8 @@ *******************************************************************************************************************/ import com.meterware.httpunit.scripting.ScriptableDelegate; -import org.w3c.dom.Node; +import org.w3c.dom.Document; +import org.w3c.dom.html.HTMLDocument; import java.io.IOException; @@ -36,7 +37,7 @@ /** * Records the root (Document) node. */ - public void setRootNode( Node rootNode ); + public void setDocument( HTMLDocument document ); /** Index: HTMLParserFactory.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/parsing/HTMLParserFactory.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- HTMLParserFactory.java 26 Dec 2002 04:59:35 -0000 1.3 +++ HTMLParserFactory.java 28 Mar 2006 00:52:15 -0000 1.4 @@ -111,7 +111,6 @@ */ public static void setPreserveTagCase( boolean preserveTagCase ) { _preserveTagCase = preserveTagCase; - if (preserveTagCase) _returnHTMLDocument = false; } @@ -119,7 +118,7 @@ * Returns true if the current parser will return an HTMLDocument object rather than a Document object. */ public static boolean isReturnHTMLDocument() { - return _returnHTMLDocument && getHTMLParser().supportsReturnHTMLDocument(); + return _returnHTMLDocument && !isPreserveTagCase() && getHTMLParser().supportsReturnHTMLDocument(); } Index: JTidyHTMLParser.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/parsing/JTidyHTMLParser.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- JTidyHTMLParser.java 25 Dec 2002 15:23:11 -0000 1.2 +++ JTidyHTMLParser.java 28 Mar 2006 00:52:15 -0000 1.3 @@ -2,7 +2,7 @@ /******************************************************************************************************************** * $Id$ * - * Copyright (c) 2002, Russell Gold + * Copyright (c) 2002,2004, Russell Gold * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated * documentation files (the "Software"), to deal in the Software without restriction, including without limitation @@ -20,6 +20,10 @@ * *******************************************************************************************************************/ import org.w3c.tidy.Tidy; +import org.w3c.dom.Document; +import org.w3c.dom.NodeList; +import org.w3c.dom.Node; +import org.w3c.dom.html.HTMLDocument; import org.xml.sax.SAXException; import java.net.URL; @@ -27,6 +31,8 @@ import java.io.ByteArrayInputStream; import java.io.UnsupportedEncodingException; +import com.meterware.httpunit.dom.HTMLDocumentImpl; + /** * @@ -37,7 +43,14 @@ public void parse( URL pageURL, String pageText, DocumentAdapter adapter ) throws IOException, SAXException { try { - adapter.setRootNode( getParser( pageURL ).parseDOM( new ByteArrayInputStream( pageText.getBytes( UTF_ENCODING ) ), null ) ); + Document jtidyDocument = getParser( pageURL ).parseDOM( new ByteArrayInputStream( pageText.getBytes( UTF_ENCODING ) ), null ); + HTMLDocument htmlDocument = new HTMLDocumentImpl(); + NodeList nl = jtidyDocument.getChildNodes(); + for (int i = 0; i < nl.getLength(); i++) { + Node importedNode = nl.item(i); + if (importedNode.getNodeType() != Node.DOCUMENT_TYPE_NODE) htmlDocument.appendChild( htmlDocument.importNode( importedNode, true ) ); + } + adapter.setDocument( htmlDocument ); } catch (UnsupportedEncodingException e) { throw new RuntimeException( "UTF-8 encoding failed" ); } @@ -55,7 +68,7 @@ public boolean supportsReturnHTMLDocument() { - return false; + return true; } Index: NekoDOMParser.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/parsing/NekoDOMParser.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- NekoDOMParser.java 5 Oct 2004 11:29:52 -0000 1.3 +++ NekoDOMParser.java 28 Mar 2006 00:52:15 -0000 1.4 @@ -1,6 +1,26 @@ package com.meterware.httpunit.parsing; - +/******************************************************************************************************************** + * $Id$ + * + * Copyright (c) 2002-2004, Russell Gold + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions + * of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + *******************************************************************************************************************/ import com.meterware.httpunit.scripting.ScriptableDelegate; +import com.meterware.httpunit.dom.HTMLDocumentImpl; import java.net.URL; import java.io.IOException; @@ -13,18 +33,18 @@ import org.apache.xerces.xni.XNIException; import org.xml.sax.SAXNotRecognizedException; import org.xml.sax.SAXNotSupportedException; -import org.w3c.dom.Node; -import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.html.HTMLDocument; /** * * - * @author <a href="rus...@ac...">Russell Gold</a> + * @author <a href="rus...@ht...">Russell Gold</a> * @author <a href="mailto:Art...@ly...">Artashes Aghajanyan</a> **/ -class NekoDOMParser extends org.apache.xerces.parsers.DOMParser { +class NekoDOMParser extends org.apache.xerces.parsers.DOMParser implements ScriptHandler { - private static final String HTML_DOCUMENT_CLASS_NAME = "org.apache.html.dom.HTMLDocumentImpl"; + // private static final String HTML_DOCUMENT_CLASS_NAME = "org.apache.html.dom.HTMLDocumentImpl"; /** Error reporting feature identifier. */ private static final String REPORT_ERRORS = "http://cyberneko.org/html/features/report-errors"; @@ -43,9 +63,6 @@ private DocumentAdapter _documentAdapter; - /** The node representing the document. **/ - private Node _documentNode; - static NekoDOMParser newParser( DocumentAdapter adapter, URL url ) { final HTMLConfiguration configuration = new HTMLConfiguration(); @@ -64,8 +81,8 @@ try { final NekoDOMParser domParser = new NekoDOMParser( configuration, adapter ); domParser.setFeature( DEFER_NODE_EXPANSION, false ); - if (HTMLParserFactory.isReturnHTMLDocument()) domParser.setProperty( DOCUMENT_CLASS_NAME, HTML_DOCUMENT_CLASS_NAME ); - javaScriptFilter.setParser( domParser ); + if (HTMLParserFactory.isReturnHTMLDocument()) domParser.setProperty( DOCUMENT_CLASS_NAME, HTMLDocumentImpl.class.getName() ); + javaScriptFilter.setScriptHandler( domParser ); return domParser; } catch (SAXNotRecognizedException e) { throw new RuntimeException( e.toString() ); @@ -76,21 +93,9 @@ } - ScriptableDelegate getScriptableDelegate() { - if (_documentNode == null) { - Node node = getCurrentElementNode(); - while (!(node instanceof Document)) node = node.getParentNode(); - _documentNode = node; - } - _documentAdapter.setRootNode( _documentNode ); - return _documentAdapter.getScriptableObject(); - } - - - private Node getCurrentElementNode() { + private Element getCurrentElement() { try { - final Node node = (Node) getProperty( CURRENT_ELEMENT_NODE ); - return node; + return (Element) getProperty( CURRENT_ELEMENT_NODE ); } catch (SAXNotRecognizedException e) { throw new RuntimeException( CURRENT_ELEMENT_NODE + " property not recognized" ); } catch (SAXNotSupportedException e) { @@ -100,7 +105,13 @@ } - String getIncludedScript( String srcAttribute ) { + NekoDOMParser( HTMLConfiguration configuration, DocumentAdapter adapter ) { + super( configuration ); + _documentAdapter = adapter; + } + + + public String getIncludedScript( String srcAttribute ) { try { return _documentAdapter.getIncludedScript( srcAttribute ); } catch (IOException e) { @@ -109,9 +120,20 @@ } - NekoDOMParser( HTMLConfiguration configuration, DocumentAdapter adapter ) { - super( configuration ); - _documentAdapter = adapter; + public boolean supportsScriptLanguage( String language ) { + return getScriptableDelegate().supportsScript( language ); + } + + + public String runScript( final String language, final String scriptText ) { + getScriptableDelegate().getScriptEngine().clearCaches(); + return getScriptableDelegate().runScript( language, scriptText ); + } + + + private ScriptableDelegate getScriptableDelegate() { + _documentAdapter.setDocument( (HTMLDocument) getCurrentElement().getOwnerDocument() ); + return _documentAdapter.getScriptableObject(); } Index: NekoHTMLParser.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/parsing/NekoHTMLParser.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- NekoHTMLParser.java 9 Mar 2003 20:35:47 -0000 1.4 +++ NekoHTMLParser.java 28 Mar 2006 00:52:15 -0000 1.5 @@ -2,7 +2,7 @@ /******************************************************************************************************************** * $Id$ * - * Copyright (c) 2002-2003, Russell Gold + * Copyright (c) 2002-2004, Russell Gold * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated * documentation files (the "Software"), to deal in the Software without restriction, including without limitation @@ -21,6 +21,7 @@ *******************************************************************************************************************/ import org.xml.sax.SAXException; import org.xml.sax.InputSource; +import org.w3c.dom.html.HTMLDocument; import java.net.URL; import java.io.IOException; @@ -39,7 +40,7 @@ try { NekoDOMParser parser = NekoDOMParser.newParser( adapter, pageURL ); parser.parse( new InputSource( new StringReader( pageText ) ) ); - adapter.setRootNode( parser.getDocument() ); + adapter.setDocument( (HTMLDocument) parser.getDocument() ); } catch (NekoDOMParser.ScriptException e) { throw e.getException(); } @@ -52,7 +53,7 @@ public boolean supportsPreserveTagCase() { - return true; + return false; } Index: ScriptFilter.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/parsing/ScriptFilter.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- ScriptFilter.java 9 Mar 2004 21:49:18 -0000 1.5 +++ ScriptFilter.java 28 Mar 2006 00:52:15 -0000 1.6 @@ -56,7 +56,7 @@ private int _scriptIndex; /** The parser in which this filter is running. **/ - private NekoDOMParser _domParser; + private ScriptHandler _scriptHandler; /** Constructs a script object with the specified configuration. */ @@ -65,8 +65,8 @@ } - public void setParser( NekoDOMParser domParser ) { - _domParser = domParser; + public void setScriptHandler( ScriptHandler scriptHandler ) { + _scriptHandler = scriptHandler; } @@ -89,7 +89,7 @@ _activeScriptBlock = new StringBuffer(); _scriptLanguage = getScriptLanguage( attrs ); String srcAttribute = attrs.getValue( "src" ); - if (srcAttribute != null) _activeScriptBlock.append( _domParser.getIncludedScript( srcAttribute ) ); + if (srcAttribute != null) _activeScriptBlock.append( _scriptHandler.getIncludedScript( srcAttribute ) ); } } @@ -97,7 +97,7 @@ private boolean isSupportedScript( QName element, XMLAttributes attrs ) { if (!element.rawname.equalsIgnoreCase( "script" ) || attrs == null) return false; String value = getScriptLanguage( attrs ); - return _domParser.getScriptableDelegate().supportsScript( value ); + return _scriptHandler.supportsScriptLanguage( value ); } @@ -147,8 +147,7 @@ protected String getTranslatedScript( final String language, final String scriptText ) throws IOException { - _domParser.getScriptableDelegate().getScriptEngine().clearCaches(); - return _domParser.getScriptableDelegate().runScript( language, scriptText ); + return _scriptHandler.runScript( language, scriptText ); } |