1 /* This file is part of EdiTeX, an editor of mathematical
2 * expressions based on TeX syntax.
4 * Copyright (C) 2002-2003 Luca Padovani <lpadovan@cs.unibo.it>,
5 * 2003 Paolo Marinelli <pmarinel@cs.unibo.it>.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * For more information, please visit the project's home page
22 * http://helm.cs.unibo.it/editex/
23 * or send an email to <lpadovan@cs.unibo.it>
31 #include "TDocument.hh"
33 TDocument::TDocument()
35 DOM::DOMImplementation di;
37 doc = di.createDocument(TML_NS_URI, "tml:tex", dt);
38 DOM::Element root = doc.get_documentElement();
40 root.setAttributeNS(XMLNS_NS_URI, "xmlns:tml", TML_NS_URI);
42 DOM::EventTarget et(doc);
44 et.addEventListener("DOMSubtreeModified", *this, false);
47 TDocument::~TDocument()
49 //DOM::Element root = doc.get_documentElement();
50 DOM::EventTarget et(doc);
52 et.removeEventListener("DOMSubtreeModified", *this, false);
58 DOM::Element root = doc.createElementNS(TML_NS_URI, "tml:tex");
59 root.setAttributeNS(XMLNS_NS_URI, "xmlns:tml", TML_NS_URI);
60 doc.replaceChild(root, doc.get_documentElement());
65 TDocument::serialize(const char* filename) const
67 DOM::DOMImplementation di;
68 di.saveDocumentToFile(doc, filename, GDOME_SAVE_LIBXML_INDENT);
72 TDocument::makeId(unsigned id)
74 std::ostringstream os;
80 TDocument::create(const std::string& name, unsigned id) const
82 DOM::Element elem = doc.createElementNS(TML_NS_URI, "tml:" + name);
83 if (id > 0) elem.setAttribute("id", makeId(id));
88 TDocument::createC(const std::string& name, unsigned id) const
90 TNode m = create("c", id);
96 TDocument::createT(const std::string& name, const std::string& text, unsigned id) const
98 TNode t = create(name, id);
104 TDocument::nodeDepth(const DOM::Node& node)
112 n = n.get_parentNode();
118 TDocument::findCommonAncestor(const DOM::Node& node1, const DOM::Node& node2)
120 DOM::Node n1 = node1;
121 DOM::Node n2 = node2;
123 unsigned d1 = nodeDepth(n1);
124 unsigned d2 = nodeDepth(n2);
126 // cout << "finding common ancestor " << d1 << " " << d2 << endl;
131 n2 = n2.get_parentNode();
138 n1 = n1.get_parentNode();
146 n1 = n1.get_parentNode();
147 n2 = n2.get_parentNode();
154 TDocument::findIdNode(const DOM::Node& node)
159 if (n.get_nodeType() == DOM::Node::ELEMENT_NODE)
162 if (el.hasAttribute("id")) return el;
164 n = n.get_parentNode();
171 TDocument::getNodeByIdAux(const TNode& node, const std::string& id)
173 if (node.hasId(id)) return node;
175 for (TNode p = node.first(); p; p = p.next())
176 if (TNode res = getNodeByIdAux(p, id)) return res;
181 TDocument::getNodeById(unsigned id) const
183 DOM::Element root = doc.get_documentElement();
185 return getNodeByIdAux(root, makeId(id));
189 TDocument::handleEvent(const DOM::Event& ev)
191 DOM::MutationEvent me(ev);
196 cout << "TDocument::handleEvent DIRTY BEFORE = " << dirty.getAttribute("id") << endl;
198 cout << "TDocument::handleEvent DIRTY BEFORE = (nil)" << endl;
201 if (DOM::Node node = me.get_target())
203 dirty = findIdNode(findCommonAncestor(dirty, node));
205 dirty = findIdNode(node);
210 cout << "TDocument::handleEvent target = " << DOM::Node(me.get_target()).get_nodeName() << " DIRTY AFTER = "
211 << dirty.getAttribute("id") << " ME = " << DOM::Node(me.get_target()).get_nodeName() << endl;