-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from nenadjakic/feature/merge_final_documents
Added merge functionality of final documents.
- Loading branch information
Showing
7 changed files
with
160 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,3 +29,4 @@ replay_pid* | |
# Ignore Gradle build output directory | ||
build | ||
/.idea/ | ||
/data/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/main/kotlin/com/github/nenadjakic/ocr/studio/handler/sax/HocrSaxHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.github.nenadjakic.ocr.studio.handler.sax | ||
|
||
import org.xml.sax.Attributes | ||
import org.xml.sax.helpers.DefaultHandler | ||
|
||
class HocrSaxHandler: DefaultHandler() { | ||
enum class InsideElement { HEAD, BODY, OTHER} | ||
private var insideElement = InsideElement.OTHER | ||
|
||
private lateinit var bodyBuilder: StringBuilder | ||
private lateinit var headBuilder: StringBuilder | ||
|
||
val body: String | ||
get() = bodyBuilder.toString() | ||
|
||
val head: String | ||
get() = headBuilder.toString() | ||
|
||
|
||
override fun startDocument() { | ||
bodyBuilder = StringBuilder() | ||
headBuilder = StringBuilder() | ||
} | ||
|
||
override fun startElement(uri: String?, localName: String?, qName: String?, attributes: Attributes?) { | ||
if (qName.equals("head", true)) { | ||
insideElement = InsideElement.HEAD | ||
} else if (qName.equals("body", true)) { | ||
insideElement = InsideElement.BODY | ||
} else if (insideElement == InsideElement.BODY) { | ||
appendStartElement(bodyBuilder, qName, attributes) | ||
} else if (insideElement == InsideElement.HEAD) { | ||
appendStartElement(headBuilder, qName, attributes) | ||
} | ||
} | ||
|
||
override fun endElement(uri: String?, localName: String?, qName: String?) { | ||
if (qName.equals("body", true) || qName.equals("head", true)) { | ||
insideElement = InsideElement.OTHER | ||
} else if (insideElement == InsideElement.BODY) { | ||
bodyBuilder.append("</$qName>") | ||
} else if (insideElement == InsideElement.HEAD) { | ||
headBuilder.append("</$qName>") | ||
} | ||
} | ||
|
||
override fun characters(ch: CharArray?, start: Int, length: Int) { | ||
if (insideElement == InsideElement.BODY) { | ||
bodyBuilder.append(ch, start, length) | ||
} else if (insideElement == InsideElement.HEAD) { | ||
headBuilder.append(ch, start, length) | ||
} | ||
} | ||
|
||
private fun appendStartElement(stringBuilder: StringBuilder, qName: String?, attributes: Attributes?) { | ||
stringBuilder.append("<$qName") | ||
|
||
if (attributes != null && attributes.length > 0) { | ||
for (i in 0 until attributes.length) { | ||
val attributeName = attributes.getQName(i) | ||
val attributeValue = attributes.getValue(i) | ||
if (attributeValue.contains("'")) { | ||
stringBuilder.append(" $attributeName=\"$attributeValue\"") | ||
} else { | ||
stringBuilder.append(" $attributeName='$attributeValue'") | ||
} | ||
} | ||
} | ||
stringBuilder.append(">") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters