How to use TagSoup with Scala XML?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

How to use TagSoup with Scala XML?

Julian Morrison
I want to read bad HTML into Scala as XML. I know TagSoup
(http://home.ccil.org/~cowan/XML/tagsoup/) is up to the job of parsing
the HTML. I can't see any way of parameterizing Scala's XML with a
replacement SAX parser.

Is there any way to do this, or workaround achieving the same result?
Reply | Threaded
Open this post in threaded view
|

Re: How to use TagSoup with Scala XML?

Florian Hars-3
Julian Morrison schrieb:
> I can't see any way of parameterizing Scala's XML with a
> replacement SAX parser.

You need a TagSoupFactoryAdapter (what a class name...). The attached one
works for me. (Is the scopeStack.push/pop necessary in a non namespace-aware
setting?)

Usage is something like

  val r = new java.io.StringReader(s)
  val p = new TagSoupFactoryAdapter
  val html = p load r

Yours, Florian
--
Dr. Florian Hars                 |
BIK ASCHPURWIS + BEHRENS GmbH    | Geschäftsführer: Kurt Behrens
Feldbrunnenstr. 7, 20148 Hamburg | Handelsregister: HAMBURG 66 HR B 40 473
(040) 41 47 87 -21, Fax: -15     | Ust.-ID: DE 118607581


/*
Copyright (c) 2008 Florian Hars, BIK Aschpurwis+Behrens GmbH, Hamburg
Copyright (c) 2002-2008 EPFL, Lausanne, unless otherwise specified.
All rights reserved.

This software was developed by the Programming Methods Laboratory of the
Swiss Federal Institute of Technology (EPFL), Lausanne, Switzerland.

Permission to use, copy, modify, and distribute this software in source
or binary form for any purpose with or without fee is hereby granted,
provided that the following conditions are met:

   1. Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.

   2. Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.

   3. Neither the name of the EPFL nor the names of its contributors
      may be used to endorse or promote products derived from this
      software without specific prior written permission.


THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/

package tagsoup

import org.xml.sax.InputSource
import javax.xml.parsers.SAXParser
import org.ccil.cowan.tagsoup.jaxp.SAXFactoryImpl
import scala.xml.parsing.FactoryAdapter
import scala.xml._

class TagSoupFactoryAdapter extends FactoryAdapter {

  val parserFactory = new SAXFactoryImpl
  parserFactory.setNamespaceAware(false)

  val emptyElements = Set("area", "base", "br", "col", "hr", "img",
                          "input", "link", "meta", "param")

  /** Tests if an XML element contains text.
   * @return true if element named <code>localName</code> contains text.
   */
  def nodeContainsText(localName: String) = !(emptyElements contains localName)
 
  /** creates a node.
  */
  def createNode(pre:String, label: String, attrs: MetaData,
                 scpe: NamespaceBinding, children: List[Node] ): Elem = {
    Elem( pre, label, attrs, scpe, children:_* );
  }

  /** creates a text node
  */
  def createText( text:String ) =
    Text( text );
 
  /** Ignore Processing Instructions
  */
  def createProcInstr(target: String, data: String) = Nil

  /** load XML document
   * @param source
   * @return a new XML document object
   */
  override def loadXML(source: InputSource) = {
    val parser: SAXParser = parserFactory.newSAXParser()
     
    scopeStack.push(TopScope)
    parser.parse(source, this)
    scopeStack.pop
    rootElem
  }

}
Reply | Threaded
Open this post in threaded view
|

Re: How to use TagSoup with Scala XML?

Channing Walton
In reply to this post by Julian Morrison
Julian Morrison wrote
I want to read bad HTML into Scala as XML. I know TagSoup
(http://home.ccil.org/~cowan/XML/tagsoup/) is up to the job of parsing
the HTML. I can't see any way of parameterizing Scala's XML with a
replacement SAX parser.

Is there any way to do this, or workaround achieving the same result?
I recently used Tagsoup in scala but I just wrote a method using Tagsoup's Parser to load the HTML. Is this a reasonable way to do it:

def load(url:String): Node = {
    import org.ccil.cowan.tagsoup.Parser
    import org.ccil.cowan.tagsoup.XMLWriter
    import java.io.StringWriter
    import java.net.URL
    import org.xml.sax.InputSource
   
    val parser = new Parser()
    val writer = new StringWriter()
   
    parser.setContentHandler(new XMLWriter(writer))
    parser.parse(new InputSource(new URL(url).openConnection().getInputStream()))

    XML.loadString(writer.toString())
  }
Reply | Threaded
Open this post in threaded view
|

Re: How to use TagSoup with Scala XML?

Burak Emir-2


On Mon, Jun 2, 2008 at 9:05 PM, Channing Walton <[hidden email]> wrote:


Julian Morrison wrote:
>
> I want to read bad HTML into Scala as XML. I know TagSoup
> (http://home.ccil.org/~cowan/XML/tagsoup/) is up to the job of parsing
> the HTML. I can't see any way of parameterizing Scala's XML with a
> replacement SAX parser.
>
> Is there any way to do this, or workaround achieving the same result?
>
>
I recently used Tagsoup in scala but I just wrote a method using Tagsoup's
Parser to load the HTML. Is this a reasonable way to do it:

def load(url:String): Node = {
   import org.ccil.cowan.tagsoup.Parser
   import org.ccil.cowan.tagsoup.XMLWriter
   import java.io.StringWriter
   import java.net.URL
   import org.xml.sax.InputSource

   val parser = new Parser()
   val writer = new StringWriter()

   parser.setContentHandler(new XMLWriter(writer))
   parser.parse(new InputSource(new
URL(url).openConnection().getInputStream()))

   XML.loadString(writer.toString())
 }

--
View this message in context: http://www.nabble.com/How-to-use-TagSoup-with-Scala-XML--tp17575225p17608457.html
Sent from the Scala - User mailing list archive at Nabble.com.


If you use Florian's TagSoupFactoryAdapter, parsing happens once which is efficient.
In your solution the xml is parsed, then converted to a string, then parsed again. Not very elegant, but works -- I would still prefer the efficient solution.

Burak
--
Burak Emir
--
http://burak.emir.googlepages.com
Reply | Threaded
Open this post in threaded view
|

Re: How to use TagSoup with Scala XML?

Channing Walton

Burak Emir-2 wrote
If you use Florian's TagSoupFactoryAdapter, parsing happens once which is
efficient.
In your solution the xml is parsed, then converted to a string, then parsed
again. Not very elegant, but works -- I would still prefer the efficient
solution.
yes good point. I will modify my solution to use Florian's.