<commit_before>package com.idyria.osi.ooxoo.core.buffers.structural

import com.idyria.osi.ooxoo.core.buffers.datatypes.XSDStringBuffer
import scala.reflect.runtime.universe._
import scala.reflect.ClassTag

trait AnyBufferTrait extends Buffer {

    var name : String = null
    
    var ns : String = null
}

@any
class AnyElementBuffer extends ElementBuffer with AnyBufferTrait {

    @any 
    var content = AnyXList()

}

@any
class AnyAttributeBuffer extends XSDStringBuffer with AnyBufferTrait {


}

/**
    Trait to add any content variable to a class
*/
trait AnyContent {

    @any
    var content = AnyXList()

}

/*class AnyXList( cl: => AnyElementBuffer) extends XList[AnyElementBuffer](AnyXList) {

}*/

object AnyXList {

    // 

    /**
        Map Containing possible Modeled XML elements that could be instanciated by the XList closure 
        instead of creating generic Any classes

        Map Format: 

            ( ns -> ElementName) -> Class

        Arguments:
            - ns: The namespace of element, or an empty string if no namespace
            - the name of the XML element
            - The class implementing ElementBuffer for that element

        @warning The model class MUST have an empty constructor, otherwise a runtime error will be thrown
    */
    var modelsMap = Map[ Tuple2[String,String], ( DataUnit => Buffer )]()

    /**
        Register a new model in the models map

        @throws IllegalArgumentException if cannot determine xelement parameters from class
    */
    def apply[T <: Buffer]( cl : Class[T]) = {

        //println("Registering model class in XList")

        // Get name and ns from annotation
        //-------------
        var xelement = xelement_base(cl)
        if (xelement==null) {
            throw new IllegalArgumentException(s"Cannot register XML model class $cl that seems to be missing @xelement annotation")
        }

        // Register
        //------------------
        this.modelsMap = this.modelsMap +((xelement.ns -> xelement.name) -> {du => cl.newInstance()}) 
    }
    
    def register[T  <: Buffer](implicit tag: ClassTag[T]) = {
      AnyXList(tag.runtimeClass.asInstanceOf[Class[T]])
    }

    def register[T  <: Buffer](cl: => T)(implicit tag: ClassTag[T]) = {
      
      // Get name and ns from annotation
    //-------------
    var xelement = xelement_base(tag.runtimeClass.asInstanceOf[Class[T]])
    if (xelement==null) {
        throw new IllegalArgumentException(s"Cannot register XML model class $cl that seems to be missing @xelement annotation")
    }

    // Register
    //------------------
    this.modelsMap = this.modelsMap + ((xelement.ns -> xelement.name) -> { du ⇒ cl })
<commit_msg>AnyBuffer: Registering namespace enabled data structures as namespaced and non namespaced one, to allow interaction with lighter protocols like JSON from Javascript. Be aware that this might lead to conflicts between libraries in case the application is using non namespaced communication<commit_after>package com.idyria.osi.ooxoo.core.buffers.structural

import com.idyria.osi.ooxoo.core.buffers.datatypes.XSDStringBuffer
import scala.reflect.runtime.universe._
import scala.reflect.ClassTag

trait AnyBufferTrait extends Buffer {

    var name : String = null
    
    var ns : String = null
}

@any
class AnyElementBuffer extends ElementBuffer with AnyBufferTrait {

    @any 
    var content = AnyXList()

}

@any
class AnyAttributeBuffer extends XSDStringBuffer with AnyBufferTrait {


}

/**
    Trait to add any content variable to a class
*/
trait AnyContent {

    @any
    var content = AnyXList()

}

/*class AnyXList( cl: => AnyElementBuffer) extends XList[AnyElementBuffer](AnyXList) {

}*/

object AnyXList {

    // 

    /**
        Map Containing possible Modeled XML elements that could be instanciated by the XList closure 
        instead of creating generic Any classes

        Map Format: 

            ( ns -> ElementName) -> Class

        Arguments:
            - ns: The namespace of element, or an empty string if no namespace
            - the name of the XML element
            - The class implementing ElementBuffer for that element

        @warning The model class MUST have an empty constructor, otherwise a runtime error will be thrown
    */
    var modelsMap = Map[ Tuple2[String,String], ( DataUnit => Buffer )]()

    /**
        Register a new model in the models map
        
        Models are registered as namespaced and non-namespaced.
        Use namespaces in case it is unclear if any collisions happen

        @throws IllegalArgumentException if cannot determine xelement parameters from class
    */
    def apply[T <: Buffer]( cl : Class[T]) = {

        //println("Registering model class in XList")

        // Get name and ns from annotation
        //-------------
        var xelement = xelement_base(cl)
        if (xelement==null) {
            throw new IllegalArgumentException(s"Cannot register XML model class $cl that seems to be missing @xelement annotation")
        }

        // Register
        //------------------
        this.modelsMap = this.modelsMap +((xelement.ns -> xelement.name) -> {du => cl.newInstance()}) 
        this.modelsMap = this.modelsMap +(("" -> xelement.name) -> {du => cl.newInstance()}) 
    }
    
    def register[T  <: Buffer](implicit tag: ClassTag[T]) = {
      AnyXList(tag.runtimeClass.asInstanceOf[Class[T]])
    }

    def register[T  <: Buffer](cl: => T)(implicit tag: ClassTag[T]) = {
      
      // Get name and ns from annotation
    //-------------
    var xelement = xelement_base(tag.runtimeClass.asInstanceOf[Class[T]])
    if (xelement==null) {
        throw new IllegalArgumentException(s"Cannot register XML model class $cl that seems to be missing @xelement annotation")
    }

    // Register
    //------------------
    this.modelsMap = this.modelsMap + ((xelement.ns -> xelement.name) -> { du ⇒ cl })
    this.modelsMap = this.modelsMap + (("" -> xelement.name) -> { du ⇒ cl })

{'fraction_non_alphanumeric': 0.11133300049925113, 'fraction_numerical': 0.0003328340822100183, 'mean_word_length': 2.454022988505747, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7063610', 'n_tokens_mistral': 1680, 'n_tokens_neox': 1622, 'n_words': 551}