1
2
3
4
5
6
7
8
9
10
11
12 package sk.uniba.euromath.document;
13 import java.net.MalformedURLException;
14 import java.net.URISyntaxException;
15 import java.net.URL;
16 import java.util.Collections;
17 import java.util.Map;
18 import org.w3c.dom.Document;
19 import org.w3c.dom.DocumentFragment;
20 import org.w3c.dom.Node;
21 import sk.baka.ikslibs.ids.FailEnum;
22 import sk.baka.ikslibs.ids.IDManager;
23 import sk.baka.ikslibs.ptr.DomPointer;
24 import sk.baka.ikslibs.ref.EntityManager;
25 import sk.baka.ikslibs.splitted.ISplittedListener;
26 import sk.baka.ikslibs.splitted.SplittedDocChangeCollector;
27 import sk.baka.ikslibs.splitted.SplittedDocHolder;
28 import sk.uniba.euromath.Const;
29 import sk.uniba.euromath.document.schema.DocumentSchema;
30 import sk.uniba.euromath.tools.URLDir;
31 /***
32 * Manages access to the document. Class is not intended to be used,
33 * instantiated nor subclassed by clients.
34 * @author Martin Vysny
35 * @deprecated merge with XMLAccess
36 */
37 @Deprecated
38 public final class DomCore {
39 /***
40 * Enclosed document.
41 */
42 private final Document doc;
43 /***
44 * Manages ID for this document.
45 */
46 private final IDManager idMan;
47 /***
48 * Manages namespaces and prefixes for this document.
49 */
50 private final NamespaceManager nsManager;
51 /***
52 * Provides access to document's contents.
53 */
54 private final DocumentContent content;
55 /***
56 * The document schema instance.
57 */
58 private final DocumentSchema schema;
59 /***
60 * The entity manager.
61 */
62 private final EntityManager entityManager;
63 /***
64 * Splitted document.
65 */
66 private final SplittedDocHolder splittedDoc;
67 /***
68 * Collects changes in the splitted document.
69 */
70 private final SplittedDocChangeCollector splittedChanges;
71 /***
72 * Placeholder for the instance of <code>XMLAccess</code> object owning
73 * this instance.
74 */
75 XMLAccess xmlAccess;
76 /***
77 * The name of the document file, including the extension, without the path
78 * specifier.
79 */
80 public final String fileName;
81 /***
82 * The name of the document file, including the extension and full path. URI
83 * address.
84 */
85 public final URL fileURL;
86 /***
87 * URL pointing to the directory where the document is located.
88 */
89 public final URLDir root;
90 /***
91 * Returns blah
92 * @return
93 */
94 public SplittedDocChangeCollector getSplittedChanges(){
95 return splittedChanges;
96 }
97 /***
98 * Constructor of the <code>DOMProxy</code> object.
99 * @param doc Document that this object will work with.
100 * @param textEntities all entities encountered during the parse.
101 * @param root URL pointing to the directory where the document is located.
102 * @param fileName the name of the document file, including the extension,
103 * without the path specifier.
104 * @throws DocumentException when error occurs during document processing.
105 */
106 DomCore(Document doc, Map<String, String> textEntities, URLDir root,
107 String fileName) throws DocumentException {
108 super();
109 this.doc = doc;
110 this.root = root;
111 this.fileName = fileName;
112 try {
113 this.fileURL = root.resolve(fileName);
114 } catch (URISyntaxException ex) {
115 throw new DocumentException(ex);
116 } catch (MalformedURLException ex) {
117 throw new DocumentException(ex);
118 }
119 idMan = new IDManager(doc, Const.EM_ATTRIBUTE_ID_QNAME,
120 FailEnum.FailWhenIdExists);
121 idMan.observe(doc);
122 entityManager = new EntityManager(doc);
123 entityManager.createEntities(textEntities, true);
124 nsManager = new NamespaceManager(this);
125 content = new DocumentContent(this);
126 schema = new DocumentSchema(this);
127 splittedChanges = new SplittedDocChangeCollector();
128 splittedDoc = SplittedDocHolder.newFromDocument(doc,
129 SplittedDocHolder.PI_GENEREF_TARGET, Collections
130 .singletonList((ISplittedListener) splittedChanges));
131 }
132 /***
133 * Returns document object that this object works with. Other objects must
134 * not modify this document in any way, except <code>DocumentModifier</code>.
135 * @return <code>Document</code> object.
136 */
137 public Document getDocument() {
138 return doc;
139 }
140 /***
141 * Checks the node if it is from our document.
142 * @param node node to check.
143 * @throws IllegalArgumentException if node is not from bound document
144 */
145 public void checkNode(Node node) {
146 if (!isOurNode(node))
147 throw new IllegalArgumentException("Node is not from our document.");
148 }
149 /***
150 * Checks the pointer if it points into our document.
151 * @param ptr pointer to check.
152 * @throws IllegalArgumentException if pointer is not from bound document
153 */
154 public void checkPtr(DomPointer ptr) {
155 if ((ptr.getDocument() == null) || !isOurNode(ptr.getDocument()))
156 throw new IllegalArgumentException(
157 "Pointer is not from our document.");
158 }
159 /***
160 * Checks the node if it is from our document.
161 * @param node node to check.
162 * @return true if the node was created by this document, false otherwise.
163 */
164 public boolean isOurNode(Node node) {
165 if (node.getNodeType() == Node.DOCUMENT_NODE)
166 return node == doc;
167 return (node.getOwnerDocument() == doc);
168 }
169 /***
170 * Returns <code>IDManager</code> instance for this document.
171 * @return An <code>IDManager</code> for this document.
172 */
173 public IDManager getIDManager() {
174 return idMan;
175 }
176 /***
177 * Returns <code>DocumentContent</code> instance for this document.
178 * @return An <code>DocumentContent</code> for this document.
179 */
180 public DocumentContent getContent() {
181 return content;
182 }
183 /***
184 * Returns namespace manager for this document.
185 * @return namespace manager for this document.
186 */
187 public NamespaceManager getNsManager() {
188 return nsManager;
189 }
190 /***
191 * Returns <code>DocumentSchema</code> instance for this document.
192 * @return An <code>DocumentSchema</code> for this document.
193 */
194 public DocumentSchema getSchema() {
195 return schema;
196 }
197 /***
198 * Returns <code>DomToSplitted</code> instance for this document.
199 * @return An <code>DomToSplitted</code> for this document.
200 */
201 public SplittedDocHolder getSplittedDoc() {
202 return splittedDoc;
203 }
204 /***
205 * Returns <code>EntityManager</code> instance for this document.
206 * @return An <code>EntityManager</code> for this document.
207 */
208 public sk.baka.ikslibs.ref.EntityManager getEntityManager() {
209 return entityManager;
210 }
211 /***
212 * Returns the nametree from the original document, that the
213 * <code>emp:mark</code> element points to.
214 * @param mark the mark element, or <code>null</code>.
215 * @return the nametree represented as a document fragment. This fragment
216 * must not be modified. If <code>null</code> is provided then root
217 * fragment is returned.
218 * @throws IllegalArgumentException if the element is not
219 * <code>emp:mark</code> or the ID does not exist.
220 */
221 public DocumentFragment getSource(Node mark) {
222 if (mark == null)
223 return splittedDoc.getRootFragment();
224 return splittedDoc.getDomFragment(splittedDoc.getRef(mark));
225 }
226 }