| <!DOCTYPE html > |
| <html> |
| <head> |
| <meta http-equiv="X-UA-Compatible" content="IE=edge" /> |
| <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" /> |
| <title>Apache Daffodil 3.2.1 Scala API - org.apache.daffodil</title> |
| <meta name="description" content="Apache Daffodil 3.2.1 Scala API - org.apache.daffodil" /> |
| <meta name="keywords" content="Apache Daffodil 3.2.1 Scala API org.apache.daffodil" /> |
| <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> |
| |
| |
| <link href="../../../lib/index.css" media="screen" type="text/css" rel="stylesheet" /> |
| <link href="../../../lib/template.css" media="screen" type="text/css" rel="stylesheet" /> |
| <link href="../../../lib/diagrams.css" media="screen" type="text/css" rel="stylesheet" id="diagrams-css" /> |
| <script type="text/javascript" src="../../../lib/jquery.min.js"></script> |
| <script type="text/javascript" src="../../../lib/jquery.panzoom.min.js"></script> |
| <script type="text/javascript" src="../../../lib/jquery.mousewheel.min.js"></script> |
| <script type="text/javascript" src="../../../lib/index.js"></script> |
| <script type="text/javascript" src="../../../index.js"></script> |
| <script type="text/javascript" src="../../../lib/scheduler.js"></script> |
| <script type="text/javascript" src="../../../lib/template.js"></script> |
| |
| <script type="text/javascript"> |
| /* this variable can be used by the JS to determine the path to the root document */ |
| var toRoot = '../../../'; |
| </script> |
| |
| </head> |
| <body> |
| <div id="search"> |
| <span id="doc-title">Apache Daffodil 3.2.1 Scala API<span id="doc-version"></span></span> |
| <span class="close-results"><span class="left"><</span> Back</span> |
| <div id="textfilter"> |
| <span class="input"> |
| <input autocapitalize="none" placeholder="Search" id="index-input" type="text" accesskey="/" /> |
| <i class="clear material-icons"></i> |
| <i id="search-icon" class="material-icons"></i> |
| </span> |
| </div> |
| </div> |
| <div id="search-results"> |
| <div id="search-progress"> |
| <div id="progress-fill"></div> |
| </div> |
| <div id="results-content"> |
| <div id="entity-results"></div> |
| <div id="member-results"></div> |
| </div> |
| </div> |
| <div id="content-scroll-container" style="-webkit-overflow-scrolling: touch;"> |
| <div id="content-container" style="-webkit-overflow-scrolling: touch;"> |
| <div id="subpackage-spacer"> |
| <div id="packages"> |
| <h1>Packages</h1> |
| <ul> |
| <li name="_root_.root" visbl="pub" class="indented0 " data-isabs="false" fullComment="yes" group="Ungrouped"> |
| <a id="_root_"></a><a id="root:_root_"></a> |
| <span class="permalink"> |
| <a href="../../../index.html" title="Permalink"> |
| <i class="material-icons"></i> |
| </a> |
| </span> |
| <span class="modifier_kind"> |
| <span class="modifier"></span> |
| <span class="kind">package</span> |
| </span> |
| <span class="symbol"> |
| <a title="This is the documentation for the Apache Daffodil Scala API." href="../../../index.html"><span class="name">root</span></a> |
| </span> |
| |
| <p class="shortcomment cmt">This is the documentation for the Apache Daffodil Scala API.</p><div class="fullcomment"><div class="comment cmt"><p>This is the documentation for the Apache Daffodil Scala API.</p><h5> Package structure </h5><p><a href="sapi/index.html" class="extype" name="org.apache.daffodil.sapi">org.apache.daffodil.sapi</a> - Provides the classes necessary to compile DFDL schemas, parse and unparse files using the compiled objects, and retrieve results and parsing diagnostics</p><p><a href="udf/index.html" class="extype" name="org.apache.daffodil.udf">org.apache.daffodil.udf</a> - Provides the classes necessary to create User Defined Functions to extend the DFDL expression language</p></div><dl class="attributes block"> <dt>Definition Classes</dt><dd><a href="../../../index.html" class="extype" name="_root_">root</a></dd></dl></div> |
| </li><li name="_root_.org" visbl="pub" class="indented1 " data-isabs="false" fullComment="yes" group="Ungrouped"> |
| <a id="org"></a><a id="org:org"></a> |
| <span class="permalink"> |
| <a href="../../../org/index.html" title="Permalink"> |
| <i class="material-icons"></i> |
| </a> |
| </span> |
| <span class="modifier_kind"> |
| <span class="modifier"></span> |
| <span class="kind">package</span> |
| </span> |
| <span class="symbol"> |
| <a title="" href="../../index.html"><span class="name">org</span></a> |
| </span> |
| |
| <div class="fullcomment"><dl class="attributes block"> <dt>Definition Classes</dt><dd><a href="../../../index.html" class="extype" name="_root_">root</a></dd></dl></div> |
| </li><li name="org.apache" visbl="pub" class="indented2 " data-isabs="false" fullComment="yes" group="Ungrouped"> |
| <a id="apache"></a><a id="apache:apache"></a> |
| <span class="permalink"> |
| <a href="../../../org/apache/index.html" title="Permalink"> |
| <i class="material-icons"></i> |
| </a> |
| </span> |
| <span class="modifier_kind"> |
| <span class="modifier"></span> |
| <span class="kind">package</span> |
| </span> |
| <span class="symbol"> |
| <a title="" href="../index.html"><span class="name">apache</span></a> |
| </span> |
| |
| <div class="fullcomment"><dl class="attributes block"> <dt>Definition Classes</dt><dd><a href="../../index.html" class="extype" name="org">org</a></dd></dl></div> |
| </li><li name="org.apache.daffodil" visbl="pub" class="indented3 current" data-isabs="false" fullComment="yes" group="Ungrouped"> |
| <a id="daffodil"></a><a id="daffodil:daffodil"></a> |
| <span class="permalink"> |
| <a href="../../../org/apache/daffodil/index.html" title="Permalink"> |
| <i class="material-icons"></i> |
| </a> |
| </span> |
| <span class="modifier_kind"> |
| <span class="modifier"></span> |
| <span class="kind">package</span> |
| </span> |
| <span class="symbol"> |
| <span class="name">daffodil</span> |
| </span> |
| |
| <div class="fullcomment"><dl class="attributes block"> <dt>Definition Classes</dt><dd><a href="../index.html" class="extype" name="org.apache">apache</a></dd></dl></div> |
| </li><li name="org.apache.daffodil.sapi" visbl="pub" class="indented4 " data-isabs="false" fullComment="yes" group="Ungrouped"> |
| <a id="sapi"></a><a id="sapi:sapi"></a> |
| <span class="permalink"> |
| <a href="../../../org/apache/daffodil/sapi/index.html" title="Permalink"> |
| <i class="material-icons"></i> |
| </a> |
| </span> |
| <span class="modifier_kind"> |
| <span class="modifier"></span> |
| <span class="kind">package</span> |
| </span> |
| <span class="symbol"> |
| <a title="Provides the classes necessary to compile DFDL schemas, parse and unparse files using the compiled objects, and retrieve results and parsing diagnostics" href="sapi/index.html"><span class="name">sapi</span></a> |
| </span> |
| |
| <p class="shortcomment cmt">Provides the classes necessary to compile DFDL schemas, parse and |
| unparse files using the compiled objects, and retrieve results and |
| parsing diagnostics</p><div class="fullcomment"><div class="comment cmt"><p>Provides the classes necessary to compile DFDL schemas, parse and |
| unparse files using the compiled objects, and retrieve results and |
| parsing diagnostics</p><h5> Overview </h5><p>The <a href="sapi/Daffodil$.html" class="extype" name="org.apache.daffodil.sapi.Daffodil">Daffodil</a> object is a factory object to create a <a href="sapi/Compiler.html" class="extype" name="org.apache.daffodil.sapi.Compiler">Compiler</a>. The |
| <a href="sapi/Compiler.html" class="extype" name="org.apache.daffodil.sapi.Compiler">Compiler</a> provides a method to compile a provided DFDL schema into a |
| <a href="sapi/ProcessorFactory.html" class="extype" name="org.apache.daffodil.sapi.ProcessorFactory">ProcessorFactory</a>, which creates a <a href="sapi/DataProcessor.html" class="extype" name="org.apache.daffodil.sapi.DataProcessor">DataProcessor</a>:</p><pre><span class="kw">val</span> c = Daffodil.compiler() |
| <span class="kw">val</span> pf = c.compileFile(file) |
| <span class="kw">val</span> dp = pf.onPath(<span class="lit">"/"</span>)</pre><p>The <a href="sapi/DataProcessor.html" class="extype" name="org.apache.daffodil.sapi.DataProcessor">DataProcessor</a> provides the necessary functions to parse and unparse |
| data, returning a <a href="sapi/ParseResult.html" class="extype" name="org.apache.daffodil.sapi.ParseResult">ParseResult</a> or <a href="sapi/UnparseResult.html" class="extype" name="org.apache.daffodil.sapi.UnparseResult">UnparseResult</a>, respectively. These |
| contain information about the parse/unparse, such as whether or not the |
| processing succeeded with any diagnostic information.</p><p>The <a href="sapi/DataProcessor.html" class="extype" name="org.apache.daffodil.sapi.DataProcessor">DataProcessor</a> also provides two functions that can be used to perform parsing/unparsing |
| via the SAX API. The first creates a <a href="sapi/DaffodilParseXMLReader.html" class="extype" name="org.apache.daffodil.sapi.DaffodilParseXMLReader">DaffodilParseXMLReader</a> which is used for parsing, and the |
| second creates a <a href="sapi/DaffodilUnparseContentHandler.html" class="extype" name="org.apache.daffodil.sapi.DaffodilUnparseContentHandler">DaffodilUnparseContentHandler</a> which is used for unparsing.</p><pre><span class="kw">val</span> xmlReader = dp.newXMLReaderInstance |
| <span class="kw">val</span> unparseContentHandler = dp.newContentHandlerInstance(output)</pre><p>The <a href="sapi/DaffodilParseXMLReader.html" class="extype" name="org.apache.daffodil.sapi.DaffodilParseXMLReader">DaffodilParseXMLReader</a> has several methods that allow one to set properties and handlers |
| (such as ContentHandlers or ErrorHandlers) for the reader. One can use any |
| contentHandler/errorHandler as long as they extend the org.xml.sax.ContentHandler and |
| org.xml.sax.ErrorHandler interfaces respectively. One can also set properties for the |
| <a href="sapi/DaffodilParseXMLReader.html" class="extype" name="org.apache.daffodil.sapi.DaffodilParseXMLReader">DaffodilParseXMLReader</a> using <a href="sapi/DaffodilParseXMLReader.html#setProperty(name:String,value:AnyRef):Unit" class="extmbr" name="org.apache.daffodil.sapi.DaffodilParseXMLReader#setProperty">DaffodilParseXMLReader.setProperty</a>.</p><p>The following properties can be set as follows:</p><p><i>The constants below have literal values starting with |
| "urn:ogf:dfdl:2013:imp:daffodil.apache.org:2018:sax:" and ending with "BlobDirectory", |
| "BlobPrefix" and "BlobSuffix" respectively.</i></p><pre>xmlReader.setProperty(DaffodilParseXMLReader.DAFFODIL_SAX_URN_BLOBDIRECTORY, |
| Paths.get(System.getProperty(<span class="lit">"java.io.tmpdir"</span>))) <span class="cmt">// value type: java.nio.file.Paths</span> |
| xmlReader.setProperty(DaffodilParseXMLReader.DAFFODIL_SAX_URN_BLOBPREFIX, <span class="lit">"daffodil-sax-"</span>) <span class="cmt">// value type String</span> |
| xmlReader.setProperty(DaffodilParseXMLReader.DAFFODIL_SAX_URN_BLOBSUFFIX, <span class="lit">".bin"</span>) <span class="cmt">// value type String</span></pre><p>The properties can be retrieved using the same variables with |
| <a href="sapi/DaffodilParseXMLReader.html#getProperty(name:String):AnyRef" class="extmbr" name="org.apache.daffodil.sapi.DaffodilParseXMLReader#getProperty">DaffodilParseXMLReader.getProperty</a> and casting |
| to the appropriate type as listed above.</p><p>The following handlers can be set as follows:</p><pre>xmlReader.setContentHandler(contentHandler) |
| xmlReader.setErrorHandler(errorHandler)</pre><p>The handlers above must implement the following interfaces respectively:</p><pre>org.xml.sax.ContentHandler |
| org.xml.sax.ErrorHandler</pre><p>The <a href="sapi/ParseResult.html" class="extype" name="org.apache.daffodil.sapi.ParseResult">ParseResult</a> can be found as a property within the <a href="sapi/DaffodilParseXMLReader.html" class="extype" name="org.apache.daffodil.sapi.DaffodilParseXMLReader">DaffodilParseXMLReader</a> using this |
| uri: "urn:ogf:dfdl:2013:imp:daffodil.apache.org:2018:sax:ParseResult" or |
| <a href="sapi/DaffodilParseXMLReader$.html#DAFFODIL_SAX_URN_PARSERESULT:String" class="extmbr" name="org.apache.daffodil.sapi.DaffodilParseXMLReader#DAFFODIL_SAX_URN_PARSERESULT">DaffodilParseXMLReader.DAFFODIL_SAX_URN_PARSERESULT</a></p><p>In order for a successful unparse to happen, the SAX API requires the |
| unparse to be kicked off by a parse call to any org.xml.sax.XMLReader implementation that has the |
| <a href="sapi/DaffodilUnparseContentHandler.html" class="extype" name="org.apache.daffodil.sapi.DaffodilUnparseContentHandler">DaffodilUnparseContentHandler</a> registered as its content handler. To retrieve the <a href="sapi/UnparseResult.html" class="extype" name="org.apache.daffodil.sapi.UnparseResult">UnparseResult</a>, |
| one can use <a href="sapi/DaffodilUnparseContentHandler.html#getUnparseResult:org.apache.daffodil.sapi.UnparseResult" class="extmbr" name="org.apache.daffodil.sapi.DaffodilUnparseContentHandler#getUnparseResult">DaffodilUnparseContentHandler.getUnparseResult</a> once the XMLReader.parse run is |
| complete.</p><h6> Parse </h6><h6> Dataprocessor Parse </h6><p>The <a href="sapi/DataProcessor.html#parse(input:org.apache.daffodil.sapi.io.InputSourceDataInputStream,output:org.apache.daffodil.sapi.infoset.InfosetOutputter):org.apache.daffodil.sapi.ParseResult" class="extmbr" name="org.apache.daffodil.sapi.DataProcessor#parse">DataProcessor.parse</a> method accepts input data to parse in the form of a <a href="sapi/io/InputSourceDataInputStream.html" class="extype" name="org.apache.daffodil.sapi.io.InputSourceDataInputStream">InputSourceDataInputStream</a> and an <a href="sapi/infoset/InfosetOutputter.html" class="extype" name="org.apache.daffodil.sapi.infoset.InfosetOutputter">InfosetOutputter</a> to determine |
| the output representation of the infoset (e.g. Scala XML Nodes, JDOM2 Documents, etc.):</p><pre><span class="kw">val</span> scalaOutputter = <span class="kw">new</span> ScalaXMLInfosetOutputter() |
| <span class="kw">val</span> is = <span class="kw">new</span> InputSourceDataInputStream(data) |
| <span class="kw">val</span> pr = dp.parse(is, scalaOutputter) |
| <span class="kw">val</span> node = scalaOutputter.getResult</pre><p>The <a href="sapi/DataProcessor.html#parse(input:org.apache.daffodil.sapi.io.InputSourceDataInputStream,output:org.apache.daffodil.sapi.infoset.InfosetOutputter):org.apache.daffodil.sapi.ParseResult" class="extmbr" name="org.apache.daffodil.sapi.DataProcessor#parse">DataProcessor.parse</a> method is thread-safe and may be called multiple times without the need to |
| create other data processors. However, <a href="sapi/infoset/InfosetOutputter.html" class="extype" name="org.apache.daffodil.sapi.infoset.InfosetOutputter">InfosetOutputter</a>'s are not |
| thread safe, requiring a unique instance per thread. An <a href="sapi/infoset/InfosetOutputter.html" class="extype" name="org.apache.daffodil.sapi.infoset.InfosetOutputter">InfosetOutputter</a> |
| should call <a href="sapi/infoset/InfosetOutputter.html#reset():Unit" class="extmbr" name="org.apache.daffodil.sapi.infoset.InfosetOutputter#reset">InfosetOutputter.reset</a> before reuse (or a new one |
| should be allocated). For example:</p><pre><span class="kw">val</span> scalaOutputter = <span class="kw">new</span> ScalaXMLInfosetOutputter() |
| files.foreach { f <span class="kw">=></span> { |
| outputter.reset |
| <span class="kw">val</span> is = <span class="kw">new</span> InputSourceDataInputStream(<span class="kw">new</span> FileInputStream(f)) |
| <span class="kw">val</span> pr = dp.parse(is, scalaOutputter) |
| <span class="kw">val</span> node = scalaOutputter.getResult |
| }</pre><p>One can repeat calls to parse() using the same InputSourceDataInputStream to continue parsing |
| where the previous parse ended. For example:</p><pre><span class="kw">val</span> is = <span class="kw">new</span> InputSourceDataInputStream(dataStream) |
| <span class="kw">val</span> scalaOutputter = <span class="kw">new</span> ScalaXMLInfosetOutputter() |
| <span class="kw">val</span> keepParsing = <span class="kw">true</span> |
| <span class="kw">while</span> (keepParsing && is.hasData()) { |
| scalaOutputter.reset() |
| <span class="kw">val</span> pr = dp.parse(is, jdomOutputter) |
| ... |
| keepParsing = !pr.isError() |
| }</pre><h6> SAX Parse </h6><p>The <a href="sapi/DaffodilParseXMLReader.html#parse(isdis:org.apache.daffodil.sapi.io.InputSourceDataInputStream):Unit" class="extmbr" name="org.apache.daffodil.sapi.DaffodilParseXMLReader#parse">DaffodilParseXMLReader.parse</a> method accepts input data to parse in the form of a |
| <a href="sapi/io/InputSourceDataInputStream.html" class="extype" name="org.apache.daffodil.sapi.io.InputSourceDataInputStream">InputSourceDataInputStream</a>. The output representation of the |
| infoset, as well as how parse errors are handled, are dependent on the content handler and the |
| error handler provided to the <a href="sapi/DaffodilParseXMLReader.html" class="extype" name="org.apache.daffodil.sapi.DaffodilParseXMLReader">DaffodilParseXMLReader</a>. For example, the org.jdom2.input.sax.SAXHandler |
| provides a JDOM representation, whereas other ContentHandlers may output directly to a |
| java.io.OutputStream or java.io.Writer.</p><pre><span class="kw">val</span> contentHandler = <span class="kw">new</span> SAXHandler() |
| xmlReader.setContentHandler(contentHandler) |
| <span class="kw">val</span> is = <span class="kw">new</span> InputSourceDataInputStream(data) |
| xmlReader.parse(is) |
| <span class="kw">val</span> pr = xmlReader.getProperty(DaffodilParseXMLReader.DAFFODIL_SAX_URN_PARSERESULT) |
| <span class="kw">val</span> doc = saxHandler.getDocument</pre><p>The <a href="sapi/DaffodilParseXMLReader.html#parse(isdis:org.apache.daffodil.sapi.io.InputSourceDataInputStream):Unit" class="extmbr" name="org.apache.daffodil.sapi.DaffodilParseXMLReader#parse">DaffodilParseXMLReader.parse</a> method is not thread-safe and may only be called again/reused once |
| a parse operation is completed. This can be done multiple times without the need to create new |
| DaffodilParseXMLReaders, ContentHandlers or ErrorHandlers. It might be necessary to reset whatever |
| ContentHandler is used (or allocate a new one). A thread-safe implementation would require unique |
| instances of the DaffodilParseXMLReader and its components. For example:</p><pre><span class="kw">val</span> contentHandler = <span class="kw">new</span> SAXHandler() |
| xmlReader.setContentHandler(contentHandler) |
| files.foreach { f <span class="kw">=></span> { |
| contentHandler.reset |
| <span class="kw">val</span> is = <span class="kw">new</span> InputSourceDataInputStream(<span class="kw">new</span> FileInputStream(f)) |
| xmlReader.parse(is) |
| <span class="kw">val</span> pr = xmlReader.getProperty(DaffodilParseXMLReader.DAFFODIL_SAX_URN_PARSERESULT) |
| <span class="kw">val</span> doc = saxHandler.getDocument |
| }</pre><p>The value of the supported features cannot be changed during a parse, and the parse will run |
| with the value of the features as they were when the parse was kicked off. To run a parse with |
| different feature values, one must wait until the running parse finishes, set the feature values |
| using the XMLReader's setFeature and run the parse again.</p><p>One can repeat calls to parse() using the same InputSourceDataInputStream to continue parsing |
| where the previous parse ended. For example:</p><pre><span class="kw">val</span> is = <span class="kw">new</span> InputSourceDataInputStream(dataStream) |
| <span class="kw">val</span> contentHandler = <span class="kw">new</span> SAXHandler() |
| xmlReader.setContentHandler(contentHandler) |
| <span class="kw">val</span> keepParsing = <span class="kw">true</span> |
| <span class="kw">while</span> (keepParsing && is.hasData()) { |
| contentHandler.reset() |
| xmlReader.parse(is) |
| <span class="kw">val</span> pr = xmlReader.getProperty(DaffodilParseXMLReader.DAFFODIL_SAX_URN_PARSERESULT) |
| ... |
| keepParsing = !pr.isError() |
| }</pre><h6> Unparse </h6><h6> Dataprocessor Unparse </h6><p>The same <a href="sapi/DataProcessor.html" class="extype" name="org.apache.daffodil.sapi.DataProcessor">DataProcessor</a> used for parse can be used to unparse an infoset |
| via the <a href="sapi/DataProcessor.html#unparse(input:org.apache.daffodil.sapi.infoset.InfosetInputter,output:java.nio.channels.WritableByteChannel):org.apache.daffodil.sapi.UnparseResult" class="extmbr" name="org.apache.daffodil.sapi.DataProcessor#unparse">DataProcessor.unparse</a> method. An |
| <a href="sapi/infoset/InfosetInputter.html" class="extype" name="org.apache.daffodil.sapi.infoset.InfosetInputter">InfosetInputter</a> provides the infoset to unparse, with the unparsed |
| data written to the provided java.nio.channels.WritableByteChannel. For example:</p><pre><span class="kw">val</span> inputter = <span class="kw">new</span> ScalaXMLInfosetInputter(node) |
| <span class="kw">val</span> ur = dp.unparse(inputter, wbc)</pre><h6> SAX Unparse </h6><p>In order to kick off an unparse via the SAX API, one must register the <a href="sapi/DaffodilUnparseContentHandler.html" class="extype" name="org.apache.daffodil.sapi.DaffodilUnparseContentHandler">DaffodilUnparseContentHandler</a> |
| as the contentHandler for an XMLReader implementation. The call to the |
| <a href="sapi/DataProcessor.html#newContentHandlerInstance(output:java.nio.channels.WritableByteChannel):org.apache.daffodil.sapi.DaffodilUnparseContentHandler" class="extmbr" name="org.apache.daffodil.sapi.DataProcessor#newContentHandlerInstance">DataProcessor.newContentHandlerInstance</a> method |
| must be provided with the java.nio.channels.WritableByteChannel, where the unparsed data ought to |
| be written to. Any XMLReader implementation is permissible, as long as they have XML |
| Namespace support.</p><pre><span class="kw">val</span> is = <span class="kw">new</span> ByteArrayInputStream(data) |
| <span class="kw">val</span> os = <span class="kw">new</span> ByteArrayOutputStream() |
| <span class="kw">val</span> wbc = java.nio.channels.Channels.newChannel(os) |
| <span class="kw">val</span> unparseContentHandler = dp.newContentHandlerInstance(wbc) |
| <span class="kw">val</span> xmlReader = SAXParserFactory.newInstance.newSAXParser.getXMLReader |
| xmlReader.setContentHandler(unparseContentHandler) |
| <span class="kw">try</span> { |
| xmlReader.parse(is) |
| } <span class="kw">catch</span> { |
| <span class="kw">case</span> _: DaffodilUnparseErrorSAXException <span class="kw">=></span> ... |
| <span class="kw">case</span> _: DaffodilUnhandledSAXException <span class="kw">=></span> ... |
| }</pre><p>The call to the XMLReader.parse method must be wrapped in a try/catch, as <a href="sapi/DaffodilUnparseContentHandler.html" class="extype" name="org.apache.daffodil.sapi.DaffodilUnparseContentHandler">DaffodilUnparseContentHandler</a> |
| relies on throwing an exception to end processing in the case of anyerrors/failures. |
| There are two kinds of errors to expect: <a href="sapi/DaffodilUnparseErrorSAXException.html" class="extype" name="org.apache.daffodil.sapi.DaffodilUnparseErrorSAXException">DaffodilUnparseErrorSAXException</a>, for the case when |
| the <a href="sapi/UnparseResult.html#isError():Boolean" class="extmbr" name="org.apache.daffodil.sapi.UnparseResult#isError">UnparseResult.isError</a>, and <a href="sapi/DaffodilUnhandledSAXException.html" class="extype" name="org.apache.daffodil.sapi.DaffodilUnhandledSAXException">DaffodilUnhandledSAXException</a>, for any other errors.</p><p>In the case of an <a href="sapi/DaffodilUnhandledSAXException.html" class="extype" name="org.apache.daffodil.sapi.DaffodilUnhandledSAXException">DaffodilUnhandledSAXException</a>,<a href="sapi/DaffodilUnparseContentHandler.html#getUnparseResult:org.apache.daffodil.sapi.UnparseResult" class="extmbr" name="org.apache.daffodil.sapi.DaffodilUnparseContentHandler#getUnparseResult">DaffodilUnparseContentHandler.getUnparseResult</a> |
| will return null.</p><pre><span class="kw">try</span> { |
| xmlReader.parse(<span class="kw">new</span> InputSource(is)) |
| } <span class="kw">catch</span> { |
| <span class="kw">case</span> _: DaffodilUnhandledSAXException <span class="kw">=></span> ... |
| <span class="kw">case</span> _: DaffodilUnparseErrorSAXException <span class="kw">=></span> ... |
| } |
| <span class="kw">val</span> ur = unparseContentHandler.getUnparseResult</pre><h5> Failures and Diagnostics </h5><p>It is possible that failures could occur during the creation of the |
| <a href="sapi/ProcessorFactory.html" class="extype" name="org.apache.daffodil.sapi.ProcessorFactory">ProcessorFactory</a>, <a href="sapi/DataProcessor.html" class="extype" name="org.apache.daffodil.sapi.DataProcessor">DataProcessor</a>, or <a href="sapi/ParseResult.html" class="extype" name="org.apache.daffodil.sapi.ParseResult">ParseResult</a>. However, rather than |
| throwing an exception on error (e.g. invalid DFDL schema, parse |
| error, etc), these classes extend <a href="sapi/WithDiagnostics.html" class="extype" name="org.apache.daffodil.sapi.WithDiagnostics">WithDiagnostics</a>, which is used to |
| determine if an error occurred, and any diagnostic information (see |
| <a href="sapi/Diagnostic.html" class="extype" name="org.apache.daffodil.sapi.Diagnostic">Diagnostic</a>) related to the step. Thus, before continuing, one must check |
| <a href="sapi/WithDiagnostics.html#isError():Boolean" class="extmbr" name="org.apache.daffodil.sapi.WithDiagnostics#isError">WithDiagnostics.isError</a>. For example:</p><pre><span class="kw">val</span> pf = c.compile(file) |
| <span class="kw">if</span> (pf.isError()) { |
| <span class="kw">val</span> diags = pf.getDiagnostics() |
| diags.foreach { d <span class="kw">=></span> |
| System.out.println(d.toString()) |
| } |
| <span class="kw">return</span> -<span class="num">1</span>; |
| }</pre><h5> Saving and Reloading Parsers </h5><p>In some cases, it may be beneficial to save a parser and reload it. |
| For example, when starting up, it may be quicker to reload an |
| already compiled parser than to compile it from scratch. To save a |
| <a href="sapi/DataProcessor.html" class="extype" name="org.apache.daffodil.sapi.DataProcessor">DataProcessor</a>:</p><pre><span class="kw">val</span> dp = pf.onPath(<span class="lit">"/"</span>) |
| dp.save(saveFile);</pre><p>And to restore a saved <a href="sapi/DataProcessor.html" class="extype" name="org.apache.daffodil.sapi.DataProcessor">DataProcessor</a>:</p><pre><span class="kw">val</span> dp = Daffodil.reload(saveFile);</pre><p>And use like below:</p><pre><span class="kw">val</span> pr = dp.parse(data);</pre><p>or</p><pre><span class="kw">val</span> xmlReader = dp.newXMLReaderInstance |
| ... <span class="cmt">// setting appropriate handlers</span> |
| xmlReader.parse(data) |
| <span class="kw">val</span> pr = xmlReader.getProperty(<span class="lit">"...ParseResult"</span>)</pre></div></div> |
| </li><li name="org.apache.daffodil.udf" visbl="pub" class="indented4 " data-isabs="false" fullComment="no" group="Ungrouped"> |
| <a id="udf"></a><a id="udf:udf"></a> |
| <span class="permalink"> |
| <a href="../../../org/apache/daffodil/udf/index.html" title="Permalink"> |
| <i class="material-icons"></i> |
| </a> |
| </span> |
| <span class="modifier_kind"> |
| <span class="modifier"></span> |
| <span class="kind">package</span> |
| </span> |
| <span class="symbol"> |
| <a title="" href="udf/index.html"><span class="name">udf</span></a> |
| </span> |
| |
| |
| </li> |
| </ul> |
| </div> |
| </div> |
| <div id="content"> |
| <body class="package value"> |
| <div id="definition"> |
| <div class="big-circle package">p</div> |
| <p id="owner"><a href="../../index.html" class="extype" name="org">org</a>.<a href="../index.html" class="extype" name="org.apache">apache</a></p> |
| <h1>daffodil<span class="permalink"> |
| <a href="../../../org/apache/daffodil/index.html" title="Permalink"> |
| <i class="material-icons"></i> |
| </a> |
| </span></h1> |
| |
| </div> |
| |
| <h4 id="signature" class="signature"> |
| <span class="modifier_kind"> |
| <span class="modifier"></span> |
| <span class="kind">package</span> |
| </span> |
| <span class="symbol"> |
| <span class="name">daffodil</span> |
| </span> |
| </h4> |
| |
| |
| <div id="comment" class="fullcommenttop"></div> |
| |
| |
| |
| |
| <div id="template"> |
| <div id="allMembers"> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| </div> |
| |
| <div id="inheritedMembers"> |
| |
| |
| </div> |
| |
| <div id="groupedMembers"> |
| <div class="group" name="Ungrouped"> |
| <h3>Ungrouped</h3> |
| |
| </div> |
| </div> |
| |
| </div> |
| |
| <div id="tooltip"></div> |
| |
| <div id="footer"> </div> |
| </body> |
| </div> |
| </div> |
| </div> |
| </body> |
| </html> |