-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
231 additions
and
8 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 |
---|---|---|
|
@@ -9,6 +9,7 @@ jobs: | |
matrix: | ||
buildType: | ||
- "common" | ||
- "lite" | ||
- "kotlinx" | ||
- "korlibs" | ||
- "ktor2" | ||
|
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
68 changes: 68 additions & 0 deletions
68
ksoup-engine-common/src/com/fleeksoft/ksoup/io/SourceReaderByteArray.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,68 @@ | ||
package com.fleeksoft.ksoup.io | ||
|
||
internal class SourceReaderByteArray(bytes: ByteArray) : SourceReader { | ||
private var source: ByteArray = bytes | ||
private var currentPosition: Int = 0 | ||
private var markedPosition: Int? = null | ||
private var isClosed: Boolean = false | ||
|
||
override fun mark(readLimit: Long) { | ||
markedPosition = currentPosition | ||
} | ||
|
||
override fun reset() { | ||
isClosed = false | ||
markedPosition?.let { | ||
currentPosition = it | ||
markedPosition = null | ||
} | ||
} | ||
|
||
|
||
override fun readBytes(count: Int): ByteArray { | ||
val byteArray = ByteArray(count) | ||
var i = 0 | ||
while (exhausted().not() && i < count) { | ||
byteArray[i] = source[currentPosition++] | ||
i++ | ||
} | ||
return if (i == 0) { | ||
byteArrayOf() | ||
} else if (i != count) { | ||
byteArray.sliceArray(0 until i) | ||
} else { | ||
byteArray | ||
} | ||
} | ||
|
||
override fun read(bytes: ByteArray, offset: Int, length: Int): Int { | ||
var i = offset | ||
while (exhausted().not() && i < length) { | ||
bytes[i] = source[currentPosition++] | ||
i++ | ||
} | ||
return i | ||
} | ||
|
||
override fun readAllBytes(): ByteArray { | ||
return readBytes(source.size - currentPosition) | ||
} | ||
|
||
override fun exhausted(): Boolean { | ||
return currentPosition >= source.size | ||
} | ||
|
||
override fun close() { | ||
// on reset we need bytes again | ||
// source = ByteArray(0) | ||
// currentPosition = 0 | ||
// markedPosition = null | ||
isClosed = true | ||
} | ||
|
||
override fun readAtMostTo(sink: KByteBuffer, byteCount: Int): Int { | ||
val bytes = readBytes(byteCount) | ||
sink.writeBytes(bytes, bytes.size) | ||
return bytes.size | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
ksoup-engine-common/src/com/fleeksoft/ksoup/io/SourceReaderExt.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,3 @@ | ||
package com.fleeksoft.ksoup.io | ||
|
||
fun SourceReader.Companion.from(byteArray: ByteArray): SourceReader = SourceReaderByteArray(byteArray) |
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,37 @@ | ||
plugins { | ||
alias(libs.plugins.mavenPublish) | ||
} | ||
|
||
group = "com.fleeksoft.ksoup" | ||
version = libs.versions.libraryVersion.get() | ||
|
||
val artifactId = "ksoup-engine-lite" | ||
mavenPublishing { | ||
coordinates("com.fleeksoft.ksoup", artifactId, libs.versions.libraryVersion.get()) | ||
pom { | ||
name.set(artifactId) | ||
description.set("Ksoup is a Kotlin Multiplatform library for working with HTML and XML, and offers an easy-to-use API for URL fetching, data parsing, extraction, and manipulation using DOM and CSS selectors.") | ||
licenses { | ||
license { | ||
name.set("Apache-2.0") | ||
url.set("https://opensource.org/licenses/Apache-2.0") | ||
} | ||
} | ||
url.set("https://github.com/fleeksoft/ksoup") | ||
issueManagement { | ||
system.set("Github") | ||
url.set("https://github.com/fleeksoft/ksoup/issues") | ||
} | ||
scm { | ||
connection.set("https://github.com/fleeksoft/ksoup.git") | ||
url.set("https://github.com/fleeksoft/ksoup") | ||
} | ||
developers { | ||
developer { | ||
name.set("Sabeeh Ul Hussnain Anjum") | ||
email.set("[email protected]") | ||
organization.set("Fleek Soft") | ||
} | ||
} | ||
} | ||
} |
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,11 @@ | ||
product: | ||
type: lib | ||
platforms: [ jvm, js, wasm, android, linuxX64, linuxArm64, tvosArm64, tvosX64, tvosSimulatorArm64, macosX64, macosArm64, iosArm64, iosSimulatorArm64, iosX64, mingwX64 ] | ||
|
||
apply: [ ../common.module-template.yaml ] | ||
|
||
aliases: | ||
- jvmAndAndroid: [ jvm, android ] | ||
|
||
dependencies: | ||
- ../ksoup-engine-common |
18 changes: 18 additions & 0 deletions
18
ksoup-engine-lite/src/com/fleeksoft/ksoup/engine/KsoupEngineImpl.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,18 @@ | ||
package com.fleeksoft.ksoup.engine | ||
|
||
import com.fleeksoft.ksoup.io.* | ||
|
||
object KsoupEngineImpl : KsoupEngine { | ||
|
||
override fun getUtf8Charset(): Charset { | ||
return CharsetImpl("UTF-8") | ||
} | ||
|
||
override fun charsetForName(name: String): Charset { | ||
return CharsetImpl(name) | ||
} | ||
|
||
override fun pathToFileSource(path: String): FileSource { | ||
TODO("File Source not supported in lite") | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
ksoup-engine-lite/src/com/fleeksoft/ksoup/io/CharsetImpl.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,57 @@ | ||
package com.fleeksoft.ksoup.io | ||
|
||
import kotlin.math.max | ||
|
||
|
||
class CharsetImpl(override val name: String) : Charset { | ||
init { | ||
require(name.lowercase() == "utf8" || name.lowercase() == "utf-8" || name.lowercase() == "iso-8859-1" || name.lowercase() == "ascii" || name.lowercase() == "us-ascii") { | ||
"Charset $name not supported" | ||
} | ||
} | ||
|
||
override fun onlyUtf8(): Boolean = true | ||
|
||
override fun decode(stringBuilder: StringBuilder, byteArray: ByteArray, start: Int, end: Int): Int { | ||
if (end <= 0) return 0 | ||
var incompleteByteIndex = -1 | ||
|
||
val isUtf8 = name.lowercase() == "utf-8" || name.lowercase() == "utf8" | ||
if (isUtf8) { | ||
// TODO:// may be we can use this for other charsets | ||
val startIndex = if (end > 4) end - 4 else 0 | ||
var i = startIndex | ||
while (i < end) { | ||
val byteLength = guessByteSequenceLength(byteArray[i]) | ||
if (byteLength > 1 && (i + byteLength) > end) { | ||
incompleteByteIndex = i | ||
break | ||
} else { | ||
i += max(byteLength, 1) | ||
} | ||
} | ||
} | ||
val toDecodeSize = if (incompleteByteIndex > 0) { | ||
incompleteByteIndex | ||
} else { | ||
end | ||
} | ||
|
||
stringBuilder.append(byteArray.sliceArray(start until toDecodeSize).decodeToString()) | ||
return toDecodeSize - start | ||
} | ||
|
||
private fun guessByteSequenceLength(byte: Byte): Int { | ||
return when ((byte.toInt() and 0xFF) shr 4) { | ||
in 0b0000..0b0111 -> 1 | ||
in 0b1100..0b1101 -> 2 | ||
0b1110 -> 3 | ||
0b1111 -> 4 | ||
else -> 0 | ||
} | ||
} | ||
|
||
override fun toByteArray(value: String): ByteArray { | ||
return value.encodeToByteArray() | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
ksoup-engine-lite/src@jvmAndAndroid/com/fleeksoft/ksoup/io/SourceExtJvm.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,9 @@ | ||
package com.fleeksoft.ksoup.io | ||
|
||
import java.io.File | ||
import java.io.InputStream | ||
|
||
// todo for jvm we can use streaming api in lite module | ||
fun SourceReader.Companion.from(inputStream: InputStream): SourceReader = SourceReader.from(inputStream.readAllBytes()) | ||
fun FileSource.Companion.from(file: File): FileSource = TODO("File Source not supported in lite") | ||
fun FileSource.Companion.from(file: String): FileSource = TODO("File Source not supported in lite") |
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
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