Skip to content

Commit

Permalink
added an extra helper method for parsing html
Browse files Browse the repository at this point in the history
  • Loading branch information
jstrachan committed Apr 19, 2012
1 parent 3277e5a commit ddb491c
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions koolapp-html/src/main/kotlin/org/koolapp/html/Functions.kt
Expand Up @@ -12,6 +12,12 @@ import java.net.URL
import java.io.FileInputStream
import java.util.*
import java.io.InputStream
import java.io.File
import java.io.OutputStream
import org.apache.xml.serialize.HTMLSerializer
import org.apache.xml.serialize.OutputFormat
import java.io.FileWriter
import java.io.*

/**
* Returns the element with the given id or throws an exception if it can't be found
Expand Down Expand Up @@ -63,3 +69,44 @@ public fun parseHtml(uri: String, parser: DOMParser = createHtmlParser()): Docum
parser.parse(uri)
return parser.getDocument()!!
}

/**
* Parses the given *file* as a HTML document
*/
public fun parseHtml(file: File, parser: DOMParser = createHtmlParser()): Document {
return parseHtml(FileInputStream(file))
}


public fun defaultEncoding(): String = "UTF-8"

public fun defaultHtmlOutputFormat(): OutputFormat {
return OutputFormat()
}


/**
* Writes this document as HTML to the given *outputStream*
*/
public fun Document.writeHtml(outputStream: OutputStream, outputFormat: OutputFormat = OutputFormat(this)): Unit {
val serializer = HTMLSerializer(outputStream, outputFormat)
serializer.serialize(this)
}

/**
* Writes this document as HTML to the given *writer*
*/
public fun Document.writeHtml(writer: Writer, outputFormat: OutputFormat = OutputFormat(this)): Unit {
val serializer = HTMLSerializer(writer, outputFormat)
serializer.serialize(this)
}

/**
* Writes this document as HTML to the given *file*
*/
public fun Document.writeHtml(file: File, outputFormat: OutputFormat = OutputFormat(this)): Unit {
val outputStream = FileOutputStream(file)
outputStream.use {
writeHtml(outputStream)
}
}

0 comments on commit ddb491c

Please sign in to comment.