📄 xmlobjectmap.java
字号:
/* * Copyright (c) 2005, John Mettraux, OpenWFE.org * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * . Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * . Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * . Neither the name of the "OpenWFE" nor the names of its contributors may be * used to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * $Id: XmlObjectMap.java,v 1.6 2005/05/17 16:41:06 jmettraux Exp $ *///// XmlObjectMap.java//// jmettraux@openwfe.org//// generated with // jtmpl 1.1.00 16.08.2003 John Mettraux (jmettraux@openwfe.org)//package openwfe.org.xdbc;/** * no comment * * <p><font size=2>CVS Info : * <br>$Author: jmettraux $ * <br>$Id: XmlObjectMap.java,v 1.6 2005/05/17 16:41:06 jmettraux Exp $ </font> * * @author jmettraux@openwfe.org */public class XmlObjectMap{ static org.apache.log4j.Logger log = org.apache.log4j.Logger .getLogger(XmlObjectMap.class.getName()); // // CONSTANTS & co public final static java.util.Comparator elementComparator = new java.util.Comparator () { public int compare (Object o1, Object o2) { if ( ! (o1 instanceof XmlObject) || ! (o2 instanceof XmlObject)) { return 0; } XmlObject xo1 = (XmlObject)o1; XmlObject xo2 = (XmlObject)o2; return (int)(xo1.getOrder() - xo2.getOrder()); } public boolean equals (Object other) { return (other == this); } }; // // FIELDS protected long rootElementId = -1; protected java.util.Map elementMap = new java.util.HashMap(); protected java.util.Map parentElementMap = new java.util.HashMap(); protected java.util.Map attributeMap = new java.util.HashMap(); protected org.jdom.Element rootElement = null; // // CONSTRUCTORS public XmlObjectMap () { } // // PUBLIC METHODS public void put (XmlObject object) { long id = object.getId(); if (object.getObject() instanceof org.jdom.Element) { org.jdom.Element elt = (org.jdom.Element)object.getObject(); //if (id == this.rootElementId) this.rootElement = elt; if (id == object.getDocumentId()) { this.rootElementId = id; this.rootElement = elt; } this.elementMap.put(new Long(id), elt); addToParentElementMap(object); } else if (object.getObject() instanceof org.jdom.Text) { org.jdom.Text txt = (org.jdom.Text)object.getObject(); this.elementMap.put(new Long(id), txt); addToParentElementMap(object); } else if (object.getObject() instanceof org.jdom.CDATA) { org.jdom.CDATA cdt = (org.jdom.CDATA)object.getObject(); this.elementMap.put(new Long(id), cdt); addToParentElementMap(object); } else if (object.getObject() instanceof org.jdom.Attribute) { org.jdom.Attribute att = (org.jdom.Attribute)object.getObject(); addToAttributeMap(object); } } public org.jdom.Document buildDocument () { // // start all the recursion... fetchContent(this.rootElementId, this.rootElement); fetchAttributes(this.rootElementId, this.rootElement); if (this.rootElement == null) { log.warn("***************************************************"); } return new org.jdom.Document(this.rootElement); } // // METHODS protected void fetchContent (long elementId, org.jdom.Element elt) { java.util.List children = (java.util.List)this.parentElementMap .get(new Long(elementId)); if (children == null) return; // // sort children list /* * not necessary anymore, the SELECT takes care of it * by ORDERING its results (see SessionImpl.java) * java.util.Collections.sort(children, elementComparator); */ // // add children to element java.util.Iterator it = children.iterator(); while (it.hasNext()) { XmlObject xo = (XmlObject)it.next(); Object o = xo.getObject(); if (o instanceof org.jdom.Element) { org.jdom.Element selt = (org.jdom.Element)o; fetchContent(xo.getId(), selt); fetchAttributes(xo.getId(), selt); // fetch content and attributes of child // before adding child elt.addContent(selt); } else if (o instanceof org.jdom.Text) { elt.addContent((org.jdom.Text)o); } else if (o instanceof org.jdom.CDATA) { elt.addContent((org.jdom.CDATA)o); } } } protected void fetchAttributes (long elementId, org.jdom.Element elt) { java.util.List attributes = (java.util.List)this.attributeMap .get(new Long(elementId)); if (attributes == null) return; java.util.Iterator it = attributes.iterator(); while (it.hasNext()) { org.jdom.Attribute att = (org.jdom.Attribute)it.next(); elt.setAttribute(att); } } protected void addToParentElementMap (XmlObject object) { long parentElementId = object.getParentElementId(); java.util.List children = (java.util.List)this.parentElementMap .get(new Long(parentElementId)); if (children == null) { children = new java.util.ArrayList(10); this.parentElementMap.put(new Long(parentElementId), children); } children.add(object); } protected void addToAttributeMap (XmlObject object) { long parentElementId = object.getParentElementId(); java.util.List children = (java.util.List)this.attributeMap .get(new Long(parentElementId)); if (children == null) { children = new java.util.ArrayList(10); this.attributeMap.put(new Long(parentElementId), children); } children.add(object.getObject()); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -