Skip to content

Commit

Permalink
Merge pull request #3 from Ashampoo/throws_for_swift
Browse files Browse the repository at this point in the history
Small improvements
  • Loading branch information
StefanOltmann authored Jul 10, 2023
2 parents 236063f + 566b1eb commit 083763c
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 109 deletions.
33 changes: 17 additions & 16 deletions src/commonMain/kotlin/com/ashampoo/xmp/XMPError.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,22 @@
// =================================================================================================
package com.ashampoo.xmp

interface XMPError {
object XMPError {

companion object {
const val UNKNOWN: Int = 0
const val BADPARAM: Int = 4
const val BADVALUE: Int = 5
const val INTERNALFAILURE: Int = 9
const val BADSCHEMA: Int = 101
const val BADXPATH: Int = 102
const val BADOPTIONS: Int = 103
const val BADINDEX: Int = 104
const val BADSERIALIZE: Int = 107
const val BADXML: Int = 201
const val BADRDF: Int = 202
const val BADXMP: Int = 203
const val BADSTREAM: Int = 204
}
const val EMPTY_SCHEMA_TEXT: String = "Empty schema namespace URI"
const val EMPTY_CONVERT_STRING_TEXT: String = "Empty convert-string"

const val UNKNOWN: Int = 0
const val BADPARAM: Int = 4
const val BADVALUE: Int = 5
const val INTERNALFAILURE: Int = 9
const val BADSCHEMA: Int = 101
const val BADXPATH: Int = 102
const val BADOPTIONS: Int = 103
const val BADINDEX: Int = 104
const val BADSERIALIZE: Int = 107
const val BADXML: Int = 201
const val BADRDF: Int = 202
const val BADXMP: Int = 203
const val BADSTREAM: Int = 204
}
12 changes: 8 additions & 4 deletions src/commonMain/kotlin/com/ashampoo/xmp/XMPMetaFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,27 @@ object XMPMetaFactory {

fun create(): XMPMeta = XMPMetaImpl()

@Throws(XMPException::class)
fun parseFromString(
packet: String,
options: ParseOptions = ParseOptions()
options: ParseOptions? = null
): XMPMeta =
XMPMetaParser.parse(packet, options)

@Throws(XMPException::class)
fun serializeToString(
xmp: XMPMeta,
options: SerializeOptions = SerializeOptions()
options: SerializeOptions? = null
): String {

require(xmp is XMPMetaImpl) { "Serialization only works with XMPMetaImpl" }

val actualOptions = options ?: SerializeOptions()

/* sort the internal data model on demand */
if (options.getSort())
if (actualOptions.getSort())
xmp.sort()

return XMPRDFWriter(xmp, options).serialize()
return XMPRDFWriter(xmp, actualOptions).serialize()
}
}
8 changes: 4 additions & 4 deletions src/commonMain/kotlin/com/ashampoo/xmp/XMPUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ object XMPUtils {
fun convertToBoolean(value: String?): Boolean {

if (value.isNullOrEmpty())
throw XMPException("Empty convert-string", XMPError.BADVALUE)
throw XMPException(XMPError.EMPTY_CONVERT_STRING_TEXT, XMPError.BADVALUE)

val valueLowercase = value.lowercase()

Expand All @@ -55,7 +55,7 @@ object XMPUtils {
try {

if (rawValue.isNullOrEmpty())
throw XMPException("Empty convert-string", XMPError.BADVALUE)
throw XMPException(XMPError.EMPTY_CONVERT_STRING_TEXT, XMPError.BADVALUE)

return if (rawValue.startsWith("0x"))
rawValue.substring(2).toInt(16)
Expand All @@ -73,7 +73,7 @@ object XMPUtils {
try {

if (rawValue.isNullOrEmpty())
throw XMPException("Empty convert-string", XMPError.BADVALUE)
throw XMPException(XMPError.EMPTY_CONVERT_STRING_TEXT, XMPError.BADVALUE)

return if (rawValue.startsWith("0x"))
rawValue.substring(2).toLong(16)
Expand All @@ -91,7 +91,7 @@ object XMPUtils {
try {

if (rawValue.isNullOrEmpty())
throw XMPException("Empty convert-string", XMPError.BADVALUE)
throw XMPException(XMPError.EMPTY_CONVERT_STRING_TEXT, XMPError.BADVALUE)

return rawValue.toDouble()

Expand Down
27 changes: 25 additions & 2 deletions src/commonMain/kotlin/com/ashampoo/xmp/impl/DomParser.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
package com.ashampoo.xmp.impl

import com.ashampoo.xmp.XMPError
import com.ashampoo.xmp.XMPException
import nl.adaptivity.xmlutil.DomWriter
import nl.adaptivity.xmlutil.EventType
import nl.adaptivity.xmlutil.XmlStreaming
import nl.adaptivity.xmlutil.dom.Document
import nl.adaptivity.xmlutil.writeCurrent

fun interface DomParser {
object DomParser {

fun parseDocumentFromString(input: String): Document
fun parseDocumentFromString(input: String): Document {

try {

val writer = DomWriter()

val reader = XmlStreaming.newReader(input)

do {
val event = reader.next()
reader.writeCurrent(writer)
} while (event != EventType.END_DOCUMENT)

return writer.target

} catch (ex: Exception) {
throw XMPException("Error reading the XML-file", XMPError.BADSTREAM, ex)
}
}
}
44 changes: 22 additions & 22 deletions src/commonMain/kotlin/com/ashampoo/xmp/impl/XMPMetaImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class XMPMetaImpl : XMPMeta {
) {

if (schemaNS.isEmpty())
throw XMPException("Empty schema namespace URI", XMPError.BADPARAM)
throw XMPException(XMPError.EMPTY_SCHEMA_TEXT, XMPError.BADPARAM)

if (arrayName.isEmpty())
throw XMPException("Empty array name", XMPError.BADPARAM)
Expand Down Expand Up @@ -120,7 +120,7 @@ class XMPMetaImpl : XMPMeta {
override fun countArrayItems(schemaNS: String, arrayName: String): Int {

if (schemaNS.isEmpty())
throw XMPException("Empty schema namespace URI", XMPError.BADPARAM)
throw XMPException(XMPError.EMPTY_SCHEMA_TEXT, XMPError.BADPARAM)

if (arrayName.isEmpty())
throw XMPException("Empty array name", XMPError.BADPARAM)
Expand All @@ -137,7 +137,7 @@ class XMPMetaImpl : XMPMeta {
override fun deleteArrayItem(schemaNS: String, arrayName: String, itemIndex: Int) {

if (schemaNS.isEmpty())
throw XMPException("Empty schema namespace URI", XMPError.BADPARAM)
throw XMPException(XMPError.EMPTY_SCHEMA_TEXT, XMPError.BADPARAM)

if (arrayName.isEmpty())
throw XMPException("Empty array name", XMPError.BADPARAM)
Expand All @@ -150,7 +150,7 @@ class XMPMetaImpl : XMPMeta {
override fun deleteProperty(schemaNS: String, propName: String) {

if (schemaNS.isEmpty())
throw XMPException("Empty schema namespace URI", XMPError.BADPARAM)
throw XMPException(XMPError.EMPTY_SCHEMA_TEXT, XMPError.BADPARAM)

if (propName.isEmpty())
throw XMPException("Empty property name", XMPError.BADPARAM)
Expand All @@ -169,7 +169,7 @@ class XMPMetaImpl : XMPMeta {

// Note: qualNS and qualName are checked inside composeQualfierPath
if (schemaNS.isEmpty())
throw XMPException("Empty schema namespace URI", XMPError.BADPARAM)
throw XMPException(XMPError.EMPTY_SCHEMA_TEXT, XMPError.BADPARAM)

if (propName.isEmpty())
throw XMPException("Empty property name", XMPError.BADPARAM)
Expand All @@ -189,7 +189,7 @@ class XMPMetaImpl : XMPMeta {
// fieldNS and fieldName are checked inside composeStructFieldPath

if (schemaNS.isEmpty())
throw XMPException("Empty schema namespace URI", XMPError.BADPARAM)
throw XMPException(XMPError.EMPTY_SCHEMA_TEXT, XMPError.BADPARAM)

if (structName.isEmpty())
throw XMPException("Empty array name", XMPError.BADPARAM)
Expand All @@ -202,7 +202,7 @@ class XMPMetaImpl : XMPMeta {
override fun doesPropertyExist(schemaNS: String, propName: String): Boolean {

if (schemaNS.isEmpty())
throw XMPException("Empty schema namespace URI", XMPError.BADPARAM)
throw XMPException(XMPError.EMPTY_SCHEMA_TEXT, XMPError.BADPARAM)

if (propName.isEmpty())
throw XMPException("Empty property name", XMPError.BADPARAM)
Expand All @@ -220,7 +220,7 @@ class XMPMetaImpl : XMPMeta {
override fun doesArrayItemExist(schemaNS: String, arrayName: String, itemIndex: Int): Boolean {

if (schemaNS.isEmpty())
throw XMPException("Empty schema namespace URI", XMPError.BADPARAM)
throw XMPException(XMPError.EMPTY_SCHEMA_TEXT, XMPError.BADPARAM)

if (arrayName.isEmpty())
throw XMPException("Empty array name", XMPError.BADPARAM)
Expand All @@ -240,7 +240,7 @@ class XMPMetaImpl : XMPMeta {
// fieldNS and fieldName are checked inside composeStructFieldPath()

if (schemaNS.isEmpty())
throw XMPException("Empty schema namespace URI", XMPError.BADPARAM)
throw XMPException(XMPError.EMPTY_SCHEMA_TEXT, XMPError.BADPARAM)

if (structName.isEmpty())
throw XMPException("Empty array name", XMPError.BADPARAM)
Expand All @@ -260,7 +260,7 @@ class XMPMetaImpl : XMPMeta {
// qualNS and qualName are checked inside composeQualifierPath()

if (schemaNS.isEmpty())
throw XMPException("Empty schema namespace URI", XMPError.BADPARAM)
throw XMPException(XMPError.EMPTY_SCHEMA_TEXT, XMPError.BADPARAM)

if (propName.isEmpty())
throw XMPException("Empty property name", XMPError.BADPARAM)
Expand All @@ -273,7 +273,7 @@ class XMPMetaImpl : XMPMeta {
override fun getArrayItem(schemaNS: String, arrayName: String, itemIndex: Int): XMPProperty? {

if (schemaNS.isEmpty())
throw XMPException("Empty schema namespace URI", XMPError.BADPARAM)
throw XMPException(XMPError.EMPTY_SCHEMA_TEXT, XMPError.BADPARAM)

if (arrayName.isEmpty())
throw XMPException("Empty array name", XMPError.BADPARAM)
Expand All @@ -291,7 +291,7 @@ class XMPMetaImpl : XMPMeta {
): XMPProperty? {

if (schemaNS.isEmpty())
throw XMPException("Empty schema namespace URI", XMPError.BADPARAM)
throw XMPException(XMPError.EMPTY_SCHEMA_TEXT, XMPError.BADPARAM)

if (altTextName.isEmpty())
throw XMPException("Empty array name", XMPError.BADPARAM)
Expand Down Expand Up @@ -345,7 +345,7 @@ class XMPMetaImpl : XMPMeta {
) {

if (schemaNS.isEmpty())
throw XMPException("Empty schema namespace URI", XMPError.BADPARAM)
throw XMPException(XMPError.EMPTY_SCHEMA_TEXT, XMPError.BADPARAM)

if (altTextName.isEmpty())
throw XMPException("Empty array name", XMPError.BADPARAM)
Expand Down Expand Up @@ -503,7 +503,7 @@ class XMPMetaImpl : XMPMeta {
private fun getProperty(schemaNS: String, propName: String, valueType: Int): XMPProperty? {

if (schemaNS.isEmpty())
throw XMPException("Empty schema namespace URI", XMPError.BADPARAM)
throw XMPException(XMPError.EMPTY_SCHEMA_TEXT, XMPError.BADPARAM)

if (propName.isEmpty())
throw XMPException("Empty property name", XMPError.BADPARAM)
Expand Down Expand Up @@ -546,7 +546,7 @@ class XMPMetaImpl : XMPMeta {
private fun getPropertyObject(schemaNS: String, propName: String, valueType: Int): Any? {

if (schemaNS.isEmpty())
throw XMPException("Empty schema namespace URI", XMPError.BADPARAM)
throw XMPException(XMPError.EMPTY_SCHEMA_TEXT, XMPError.BADPARAM)

if (propName.isEmpty())
throw XMPException("Empty property name", XMPError.BADPARAM)
Expand Down Expand Up @@ -641,7 +641,7 @@ class XMPMetaImpl : XMPMeta {

// qualNS and qualName are checked inside composeQualfierPath
if (schemaNS.isEmpty())
throw XMPException("Empty schema namespace URI", XMPError.BADPARAM)
throw XMPException(XMPError.EMPTY_SCHEMA_TEXT, XMPError.BADPARAM)

if (propName.isEmpty())
throw XMPException("Empty property name", XMPError.BADPARAM)
Expand All @@ -661,7 +661,7 @@ class XMPMetaImpl : XMPMeta {
// fieldNS and fieldName are checked inside composeStructFieldPath

if (schemaNS.isEmpty())
throw XMPException("Empty schema namespace URI", XMPError.BADPARAM)
throw XMPException(XMPError.EMPTY_SCHEMA_TEXT, XMPError.BADPARAM)

if (structName.isEmpty())
throw XMPException("Empty array name", XMPError.BADPARAM)
Expand Down Expand Up @@ -693,7 +693,7 @@ class XMPMetaImpl : XMPMeta {
) {

if (schemaNS.isEmpty())
throw XMPException("Empty schema namespace URI", XMPError.BADPARAM)
throw XMPException(XMPError.EMPTY_SCHEMA_TEXT, XMPError.BADPARAM)

if (arrayName.isEmpty())
throw XMPException("Empty array name", XMPError.BADPARAM)
Expand All @@ -717,7 +717,7 @@ class XMPMetaImpl : XMPMeta {
) {

if (schemaNS.isEmpty())
throw XMPException("Empty schema namespace URI", XMPError.BADPARAM)
throw XMPException(XMPError.EMPTY_SCHEMA_TEXT, XMPError.BADPARAM)

if (arrayName.isEmpty())
throw XMPException("Empty array name", XMPError.BADPARAM)
Expand All @@ -740,7 +740,7 @@ class XMPMetaImpl : XMPMeta {
) {

if (schemaNS.isEmpty())
throw XMPException("Empty schema namespace URI", XMPError.BADPARAM)
throw XMPException(XMPError.EMPTY_SCHEMA_TEXT, XMPError.BADPARAM)

if (propName.isEmpty())
throw XMPException("Empty property name", XMPError.BADPARAM)
Expand All @@ -767,7 +767,7 @@ class XMPMetaImpl : XMPMeta {
) {

if (schemaNS.isEmpty())
throw XMPException("Empty schema namespace URI", XMPError.BADPARAM)
throw XMPException(XMPError.EMPTY_SCHEMA_TEXT, XMPError.BADPARAM)

if (propName.isEmpty())
throw XMPException("Empty property name", XMPError.BADPARAM)
Expand All @@ -790,7 +790,7 @@ class XMPMetaImpl : XMPMeta {
) {

if (schemaNS.isEmpty())
throw XMPException("Empty schema namespace URI", XMPError.BADPARAM)
throw XMPException(XMPError.EMPTY_SCHEMA_TEXT, XMPError.BADPARAM)

if (structName.isEmpty())
throw XMPException("Empty array name", XMPError.BADPARAM)
Expand Down
16 changes: 9 additions & 7 deletions src/commonMain/kotlin/com/ashampoo/xmp/impl/XMPMetaParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import nl.adaptivity.xmlutil.dom.namespaceURI
* XML-parsing and fixes the prefix. After the parsing several normalisations
* are applied to the XMPTree.
*/
object XMPMetaParser {
internal object XMPMetaParser {

private val XMP_RDF = Any() // "new Object()" in Java

Expand All @@ -42,26 +42,28 @@ object XMPMetaParser {
*/
fun parse(
input: String,
options: ParseOptions = ParseOptions()
options: ParseOptions?
): XMPMeta {

val document = XmlUtilDomParser.parseDocumentFromString(input)
val actualOptions = options ?: ParseOptions()

val xmpmetaRequired = options.getRequireXMPMeta()
val document = DomParser.parseDocumentFromString(input)

val xmpmetaRequired = actualOptions.getRequireXMPMeta()

var result: Array<Any?>? = arrayOfNulls(3)

result = findRootNode(document, xmpmetaRequired, result)

return if (result != null && result[1] === XMP_RDF) {

val xmp = XMPRDFParser.parse(result[0] as Node, options)
val xmp = XMPRDFParser.parse(result[0] as Node, actualOptions)

xmp.setPacketHeader(result[2] as? String)

// Check if the XMP object shall be normalized
if (!options.getOmitNormalization())
normalize(xmp, options)
if (!actualOptions.getOmitNormalization())
normalize(xmp, actualOptions)
else
xmp

Expand Down
Loading

0 comments on commit 083763c

Please sign in to comment.