Skip to content

Commit

Permalink
For #6491: compile dom module with Scala 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Bruchez committed Sep 17, 2024
1 parent 4c60bbd commit 3c5a625
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 27 deletions.
7 changes: 4 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ lazy val commonScalaJvmSettings = Seq(
lazy val commonScalaJsSettings = Seq(

libraryDependencies += "org.scala-js" %%% "scala-js-macrotask-executor" % ScalaJsMacrotaskExecutor,
libraryDependencies += "org.scala-js" %%% "scalajs-fake-insecure-java-securerandom" % ScalaJsFakeSecureRandomVersion,
libraryDependencies += ("org.scala-js" %%% "scalajs-fake-insecure-java-securerandom" % ScalaJsFakeSecureRandomVersion).cross(CrossVersion.for3Use2_13),

packageJSDependencies / skip := false,
scalaJSLinkerConfig ~= (_.withSourceMap(false).withESFeatures(_.withESVersion(ESVersion.ES2018))),
Expand Down Expand Up @@ -514,10 +514,11 @@ lazy val dom = (crossProject(JVMPlatform, JSPlatform).crossType(CrossType.Pure)
.settings(commonSettings: _*)
.settings(
name := "orbeon-dom",
crossScalaVersions := supportedScalaVersions
scalaVersion := scala3,
crossScalaVersions := scala3 :: supportedScalaVersions
)
.jsSettings(
libraryDependencies += "org.scala-js" %%% "scalajs-fake-weakreferences" % ScalaJsFakeWeakReferencesVersion
libraryDependencies += ("org.scala-js" %%% "scalajs-fake-weakreferences" % ScalaJsFakeWeakReferencesVersion).cross(CrossVersion.for3Use2_13)
)

lazy val domJVM = dom.jvm.dependsOn(commonJVM)
Expand Down
9 changes: 6 additions & 3 deletions dom/src/main/scala/org/orbeon/dom/Document.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import java.{util => ju}

import org.orbeon.dom.tree.AbstractBranch

import scala.compiletime.uninitialized


object Document {

def apply(): Document =
Expand All @@ -30,11 +33,11 @@ class Document extends AbstractBranch {

override def getDocument: Document = this

private var _rootElement: Element = _
private var _rootElement: Element = uninitialized
def rootElementOpt: Option[Element] = Option(_rootElement)
def getRootElement: Element = _rootElement

private var _internalContent: ju.List[Node] = _
private var _internalContent: ju.List[Node] = uninitialized
protected def internalContent: ju.List[Node] = {
if (_internalContent eq null) {
_internalContent = new ju.ArrayList[Node](1)
Expand Down Expand Up @@ -159,7 +162,7 @@ class Document extends AbstractBranch {
if (node ne null)
node.setDocument(null)

protected def checkAddElementAllowed(element: Element): Unit = {
private def checkAddElementAllowed(element: Element): Unit = {
val root = getRootElement
if (root ne null) {
throw new IllegalAddException(
Expand Down
4 changes: 3 additions & 1 deletion dom/src/main/scala/org/orbeon/dom/QName.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package org.orbeon.dom

import java.{util => ju}

import scala.compiletime.uninitialized


object QName {

Expand Down Expand Up @@ -89,7 +91,7 @@ object QName {
//
class QName private (val localName: String, val namespace: Namespace, val qualifiedName: String) {

private var _hashCode: Int = _
private var _hashCode: Int = uninitialized

override def hashCode(): Int = {
if (_hashCode == 0) {
Expand Down
9 changes: 6 additions & 3 deletions dom/src/main/scala/org/orbeon/dom/io/SAXContentHandler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import org.xml.sax.*
import org.xml.sax.ext.LexicalHandler
import org.xml.sax.helpers.DefaultHandler

import scala.compiletime.uninitialized


/**
* `SAXContentHandler` builds a tree via SAX events.
*/
Expand All @@ -26,11 +29,11 @@ class SAXContentHandler(
def getDocument = document

// State
private var locator: Locator = _
private var locator: Locator = uninitialized
private var declaredNamespaceIndex = 0
private var currentElement: Element = _
private var currentElement: Element = uninitialized
private var textInTextBuffer = false
private var textBuffer: jl.StringBuilder = _
private var textBuffer: jl.StringBuilder = uninitialized

override def setDocumentLocator(documentLocator: Locator): Unit =
this.locator = documentLocator
Expand Down
13 changes: 8 additions & 5 deletions dom/src/main/scala/org/orbeon/dom/io/SAXWriter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import org.xml.sax.*
import org.xml.sax.ext.LexicalHandler
import org.xml.sax.helpers.{AttributesImpl, LocatorImpl}

import scala.compiletime.uninitialized


private object SAXWriter {

Expand All @@ -25,21 +27,22 @@ private object SAXWriter {
*/
class SAXWriter extends XMLReader {

var contentHandler: ContentHandler = _
var contentHandler: ContentHandler = uninitialized

private var dtdHandler: DTDHandler = uninitialized

private var dtdHandler: DTDHandler = _
var entityResolver: EntityResolver = uninitialized
var errorHandler: ErrorHandler = uninitialized
var lexicalHandler: LexicalHandler = uninitialized

var entityResolver: EntityResolver = _
override def getContentHandler(): ContentHandler = contentHandler
override def getEntityResolver(): EntityResolver = entityResolver
override def getErrorHandler(): ErrorHandler = errorHandler

var errorHandler: ErrorHandler = _
override def setContentHandler(ch: ContentHandler): Unit = contentHandler = ch
override def setEntityResolver(er: EntityResolver): Unit = entityResolver = er
override def setErrorHandler(eh: ErrorHandler): Unit = errorHandler = eh

var lexicalHandler: LexicalHandler = _
def setLexicalHandler(lh: LexicalHandler): Unit = lexicalHandler = lh

// Reusable attributes used by `createAttributes()` only
Expand Down
3 changes: 2 additions & 1 deletion dom/src/main/scala/org/orbeon/dom/tree/ConcreteElement.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import java.{lang => jl, util => ju}
import org.orbeon.dom.*
import org.xml.sax.Attributes

import scala.compiletime.uninitialized
import scala.jdk.CollectionConverters.*


Expand Down Expand Up @@ -51,7 +52,7 @@ class ConcreteElement(var qname: QName)
* child of the root document, or null if it has not been added to a
* document yet.
*/
private var _parentBranch: Branch = _
private var _parentBranch: Branch = uninitialized

override def getParent: Element =
_parentBranch match {
Expand Down
7 changes: 5 additions & 2 deletions dom/src/main/scala/org/orbeon/dom/tree/NamespaceStack.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import java.{util => ju}

import org.orbeon.dom.{Namespace, QName}

import scala.compiletime.uninitialized


/**
* NamespaceStack implements a stack of namespaces and optionally maintains a
* cache of all the fully qualified names (`QName`) which are in
Expand All @@ -25,7 +28,7 @@ class NamespaceStack {
* A cache of current namespace context cache of mapping from qualifiedName
* to QName
*/
private var currentNamespaceCache: ju.Map[String, QName] = _
private var currentNamespaceCache: ju.Map[String, QName] = uninitialized

/**
* A cache of mapping from qualifiedName to QName before any namespaces are
Expand All @@ -36,7 +39,7 @@ class NamespaceStack {
/**
* Caches the default namespace defined via xmlns=""
*/
private var _defaultNamespace: Namespace = _
private var _defaultNamespace: Namespace = uninitialized
def getDefaultNamespace: Namespace = {
if (_defaultNamespace eq null)
_defaultNamespace = findDefaultNamespace
Expand Down
5 changes: 4 additions & 1 deletion dom/src/main/scala/org/orbeon/dom/tree/WithData.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package org.orbeon.dom.tree

import scala.compiletime.uninitialized


trait WithData {
private var _data: AnyRef = _
private var _data: AnyRef = uninitialized
def setData(data: AnyRef): Unit = _data = data
def getData: AnyRef = _data
}
5 changes: 4 additions & 1 deletion dom/src/main/scala/org/orbeon/dom/tree/WithParent.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package org.orbeon.dom.tree

import org.orbeon.dom.{Element, Node}

import scala.compiletime.uninitialized


trait WithParent extends Node {
private var _parent: Element = _
private var _parent: Element = uninitialized
override def getParent: Element = _parent
override def setParent(parent: Element): Unit = _parent = parent
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ import org.orbeon.oxf.xforms.state.AnnotatedTemplate
import org.orbeon.oxf.xforms.xbl.{CommonBinding, ConcreteBinding}
import org.orbeon.oxf.xml.SAXStore
import org.orbeon.oxf.xml.dom.Extensions.*
import org.orbeon.xforms.analysis.{Perform, Propagate}
import org.orbeon.xforms.analysis.model.ValidationLevel
import org.orbeon.xforms.analysis.{Perform, Propagate}
import org.orbeon.xforms.xbl.Scope
import org.orbeon.xml.NamespaceMapping
import shapeless.syntax.typeable.typeableOps

import java.util.Base64
Expand Down Expand Up @@ -378,13 +377,13 @@ object XFormsStaticStateSerializer {
)

implicit val encodeLangRef : Encoder[LangRef] = deriveEncoder
implicit val encodeNamespace : Encoder[dom.Namespace] = deriveEncoder
// implicit val encodeNamespace : Encoder[dom.Namespace] = deriveEncoder
implicit val encodeBasicCredentials : Encoder[BasicCredentials] = deriveEncoder

implicit val encodeNamespaceMapping: Encoder[NamespaceMapping] = (a: NamespaceMapping) => Json.obj(
"hash" -> Json.fromString(a.hash),
"mapping" -> a.mapping.asJson
)
// implicit val encodeNamespaceMapping: Encoder[NamespaceMapping] = (a: NamespaceMapping) => Json.obj(
// "hash" -> Json.fromString(a.hash),
// "mapping" -> a.mapping.asJson
// )

def appendElemAndAtts(a: dom.Element, b: ListBuffer[(String, Json)]) = {
b += "name" -> Json.fromInt(collectedQNamesWithPositions(a.getQName))
Expand Down

0 comments on commit 3c5a625

Please sign in to comment.