2014-04-08 8 views
0

저는 스칼라에서 Scales library을 사용하여 XML을 처리하고 있습니다. 구문 분석 중에 xi:include elements을 확장하고 싶습니다.X 축척 및 비율 조정 XML

import java.io.FileReader 
import scales.utils._, ScalesUtils._ 
import scales.xml._, ScalesXml._ 

val doc = loadXml(new FileReader("data/example.xml")) 

이 가능 이러한 요소를 확대하는이 내가, 예를 들어, XML 파일의 모든 include 요소가 구문 분석 문서에서 할 unexpanded 나타납니다 다음을 작성하는 경우 기본적으로 발생하지 않습니다? 저울즈 0.6.0-M1을 사용하고 있습니다.

답변

1

예! 적어도 가능합니다. 당신이 상대 URI가있는 경우 그 생각

val doc = loadXml(
    source = new FileReader("data/example.xml"), 
    parsers = XIncludeSAXParserFactoryPool.parsers 
) 

참고 :

import javax.xml.parsers.{ SAXParser, SAXParserFactory } 
import scales.utils._, ScalesUtils._ 
import scales.xml._, ScalesXml._ 
import scales.xml.parser.sax.DefaultSaxSupport 

object XIncludeSAXParserFactoryPool extends 
    resources.SimpleUnboundedPool[SAXParserFactory] { pool => 

    def create = { 
    val parserFactory = SAXParserFactory.newInstance() 
    parserFactory.setNamespaceAware(true) 
    parserFactory.setFeature("http://xml.org/sax/features/namespaces", true) 
    parserFactory.setXIncludeAware(true) 
    parserFactory.setValidating(false) 
    parserFactory 
    } 

    val parsers = new resources.Loaner[SAXParser] with DefaultSaxSupport { 
    def loan[X](tThunk: SAXParser => X): X = 
     pool.loan(x => tThunk(x.newSAXParser)) 
    } 
} 

그런 다음 당신은 당신의 loadXml 호출 파서 풀을 지정할 수 있습니다 : 당신은 제공 DefaultSAXParserFactoryPool의 모델에 자신의 SAXParserFactory 풀을 쓸 수 있습니다 includehref이 문서의 위치 (현재 디렉토리가 아닌)에 비례하여 평가되기를 원하면 InputSource이 시스템 ID를 가져야합니다. 이를 수행하는 한 가지 방법은 다음과 같습니다.

import java.io.File 
import org.xml.sax.InputSource 

loadXml(
    source = new InputSource(new File("data/example.xml").toUri.toString), 
    parsers = XIncludeSAXParserFactoryPool.parsers 
) 

위의 모든 작업은 0.5.0과 0.6.0에서 작동합니다.

이 문제를 해결할 수있는 더 좋은 방법이 있다면, 나는 그것을 듣고 싶습니다.