Skip to content

Instantly share code, notes, and snippets.

@mauricio
Created April 10, 2012 10:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mauricio/2350438 to your computer and use it in GitHub Desktop.
Save mauricio/2350438 to your computer and use it in GitHub Desktop.
class PDFTronXMLFilterWriter( writer : Writer ) extends FilterWriter( writer ) {
val buffer = MutableList[Char]()
val selectedContent = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>".toCharArray
writer.write( selectedContent )
writer.write( "\n<document>\n" )
override def close() {
this.out.write( buffer.mkString("") )
this.out.write( "\n</document>" )
this.out.flush()
this.out.close()
}
override def write(c: Int) {
this.buffer += c.toChar
if ( this.selectedContent.indexOfSlice( this.buffer ) == -1 ) {
this.out.write( buffer.mkString("") )
this.buffer.clear()
} else {
if ( this.buffer.size == this.selectedContent.size ) {
this.buffer.clear()
}
}
}
override def write(buffer: Array[Char], off: Int, len: Int) {
( off until ( off + len) ).foreach( (index) => { this.write( buffer(index) ) } )
}
override def write(str: String, off: Int, len: Int) {
this.write( str.toCharArray, off, len )
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment