package net.sf.tomp.xml.tree; //import bak.pcj.map.ObjectKeyIntMap; //import bak.pcj.map.ObjectKeyIntOpenHashMap; import org.xml.sax.*; import java.util.*; /** * DOCUMENT ME! * * @author $author$ * @version $Revision$ */ public class Document { // IDs /** DOCUMENT ME! */ public static final String ID_LOCALNAME = "id"; /** DOCUMENT ME! */ public static final String ID_NAMESPACE = ""; /** DOCUMENT ME! */ public static final String ID_QNAME = "id"; /** DOCUMENT ME! */ public static final String ID_TYPE = "CDATA"; /** DOCUMENT ME! */ protected static Map nextIds = new HashMap(); // list for the queue /** DOCUMENT ME! */ protected List q = new ArrayList(); // map for the (id -> element) index // protected ObjectKeyIntMap index = new ObjectKeyIntOpenHashMap(); /** DOCUMENT ME! */ protected Map index = new HashMap(); /** DOCUMENT ME! */ protected Set duplicateIds = new HashSet(); /** * DOCUMENT ME! * * @param e DOCUMENT ME! * * @return DOCUMENT ME! */ public static String extractId(Element e) { //dump(e.getAttributes()); return extractId(e.getAttributes()); } /** * DOCUMENT ME! * * @param a DOCUMENT ME! * * @return DOCUMENT ME! */ public static String extractId(Attributes a) { //dump(e.getAttributes()); String id = a.getValue(ID_NAMESPACE, ID_LOCALNAME); //System.out.println("returning id="+id); return id; } /** * DOCUMENT ME! * * @param e DOCUMENT ME! */ public void add(Node e) { //System.out.println("NodeQueue: putting "+e+" to queue"); q.add(e); } /** * DOCUMENT ME! * * @param pos DOCUMENT ME! * * @return DOCUMENT ME! */ public Node get(int pos) { return (Node) (q.get(pos)); } /** * DOCUMENT ME! * * @return DOCUMENT ME! */ public int size() { return q.size(); } /** * DOCUMENT ME! * * @param e DOCUMENT ME! */ public void add(Element e) { String id = extractId(e); if (id != null) { if (index.containsKey(id)) { duplicateIds.add(id); } else { index.put(id, e); } } //System.out.println("NodeQueue: ELEMENT "+e+" with id="+id+" to queue"); q.add(e); } /** * calculates next id for a given current id: separates the section after * the last dot character, interpretes it as an integer and increments * it. * * @param id id (for example XXXYYYZZZ.123) * * @return next id (for example XXXYYYZZZ.124) */ public static String nextId(String id) { String nextId = null; String lastNextId = (String) nextIds.get(id); if (lastNextId == null) { nextId = makeNextId(id); } else { nextId = makeNextId(lastNextId); } nextIds.put(id, nextId); return nextId; } /** * DOCUMENT ME! * * @param id DOCUMENT ME! * * @return DOCUMENT ME! */ public static String makeNextId(String id) { int lastDot = id.lastIndexOf('.'); if (lastDot == -1) { return id + ".1"; // no dot, enter new one } else { try { String beforeAndLastDot = id.substring(0, lastDot + 1); String lastNumStr = id.substring(lastDot + 1); int nextNum = Integer.parseInt(lastNumStr) + 1; return beforeAndLastDot + nextNum; } catch (NumberFormatException nfe) { return id + ".1"; } } } /** * DOCUMENT ME! * * @param id DOCUMENT ME! * * @return DOCUMENT ME! */ public Element findElement(String id) { return (Element) (index.get(id)); } /** * DOCUMENT ME! */ public void dump() { for (int i = 0; i < q.size(); i++) { System.out.println(get(i)); } } /** * DOCUMENT ME! * * @param a DOCUMENT ME! */ public void dump(Attributes a) { for (int i = 0; i < a.getLength(); i++) { System.out.println("attribute: {" + a.getURI(i) + "}" + a.getLocalName(i) + "=" + a.getValue(i)); } } } /* The contents of this file are subject to the * Mozilla Public License Version 1.1 (the "License"); * you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * The Original Code is: all this file. * The Initial Developer of the Original Code is: * Tomas Pitner, Masaryk University in Brno, Czech Republic. * Contributor(s): */