1 /**
2 * Portions of this software was developed by employees of the National Institute
3 * of Standards and Technology (NIST), an agency of the Federal Government and is
4 * being made available as a public service. Pursuant to title 17 United States
5 * Code Section 105, works of NIST employees are not subject to copyright
6 * protection in the United States. This software may be subject to foreign
7 * copyright. Permission in the United States and in foreign countries, to the
8 * extent that NIST may hold copyright, to use, copy, modify, create derivative
9 * works, and distribute this software and its documentation without fee is hereby
10 * granted on a non-exclusive basis, provided that this notice and disclaimer
11 * of warranty appears in all copies.
12 *
13 * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
14 * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
15 * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
17 * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
18 * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT
19 * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
20 * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
21 * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
22 * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
23 * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
24 * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
25 */
26
27 package gov.nist.secauto.decima.xml.document;
28
29 import gov.nist.secauto.decima.core.document.Document;
30 import gov.nist.secauto.decima.core.document.DocumentException;
31 import gov.nist.secauto.decima.xml.document.context.XMLContextResolver;
32
33 import org.jdom2.Element;
34 import org.jdom2.output.Format;
35
36 import java.io.File;
37 import java.io.FileNotFoundException;
38 import java.io.IOException;
39
40 import javax.xml.transform.Source;
41 import javax.xml.xpath.XPathFactoryConfigurationException;
42
43 /**
44 * Represents an instance of XML that can be reused for various forms of XML processing.
45 * Implementations of this class can choose to locally cache the XML and apply any pre-processing
46 * steps required to prepare the XML for use.
47 */
48 public interface XMLDocument extends Document, XMLContextResolver {
49
50 /**
51 * Retrieves the document as an XML transformation source. This instance of the document is expected
52 * to be in a ready-to-be-processed state. Modifications to the returned instance should not affect
53 * the actual template. It may be necessary to make a defensive copy to ensure this.
54 *
55 * @return an input source based on a copy of the template
56 */
57 Source getSource();
58
59 /**
60 * Returns an XPath 2.0 evaluator.
61 *
62 * @return the evaluator
63 * @throws XPathFactoryConfigurationException
64 * if an error occurred while instantiating a new XPathFactory instance
65 */
66 XPathEvaluator newXPathEvaluator() throws XPathFactoryConfigurationException;
67
68 XMLDocumentFragment newXMLDocumentFragment(String xpath) throws DocumentException;
69
70 /**
71 * Creates an {@link XMLDocumentFragment} based on a sub-tree of the current document instance.
72 *
73 * @param element
74 * the element in the current document instance to use as the base of the sub-tree
75 * @return a new {@link XMLDocumentFragment} for the sub-tree
76 * @throws DocumentException
77 * if an error occurred while parsing the document
78 */
79 XMLDocumentFragment newXMLDocumentFragment(Element element) throws DocumentException;
80
81 /**
82 * Provides access to a (defensive) copy of the underlying JDOM document.
83 *
84 * @return a copy of the underlying JDOM document
85 */
86 org.jdom2.Document getJDOMDocument();
87
88 /**
89 * Writes a copy of the document to the provided file.
90 *
91 * @param outputFile
92 * the file to write to
93 * @throws FileNotFoundException
94 * if the path to the file does not exist
95 * @throws IOException
96 * if an error occurs while writing to the file
97 */
98 void copyTo(File outputFile) throws FileNotFoundException, IOException;
99
100 /**
101 * Retrieves the document as a string.
102 *
103 * @param format
104 * the format to use during the conversion
105 * @return a string representation of the document
106 * @throws IOException
107 * if an error occurs while outputting the document as a dtring
108 */
109 String asString(Format format) throws IOException;
110
111 }