001: //=============================================================================
002: //=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
003: //=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
004: //=== and United Nations Environment Programme (UNEP)
005: //===
006: //=== This program is free software; you can redistribute it and/or modify
007: //=== it under the terms of the GNU General Public License as published by
008: //=== the Free Software Foundation; either version 2 of the License, or (at
009: //=== your option) any later version.
010: //===
011: //=== This program is distributed in the hope that it will be useful, but
012: //=== WITHOUT ANY WARRANTY; without even the implied warranty of
013: //=== MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: //=== General Public License for more details.
015: //===
016: //=== You should have received a copy of the GNU General Public License
017: //=== along with this program; if not, write to the Free Software
018: //=== Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
019: //===
020: //=== Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
021: //=== Rome - Italy. email: geonetwork@osgeo.org
022: //==============================================================================
023:
024: package org.fao.geonet.lib;
025:
026: import java.util.ArrayList;
027: import java.util.HashSet;
028: import java.util.Iterator;
029: import java.util.Map;
030: import java.util.Set;
031: import java.util.StringTokenizer;
032: import jeeves.utils.Util;
033: import org.jdom.Attribute;
034: import org.jdom.Content;
035: import org.jdom.Element;
036: import org.jdom.Text;
037:
038: //=============================================================================
039:
040: public class ElementLib {
041: //-----------------------------------------------------------------------------
042: //---
043: //--- API methods
044: //---
045: //-----------------------------------------------------------------------------
046:
047: public Set<String> getIds(Element elem) {
048: HashSet<String> hs = new HashSet<String>();
049:
050: for (Object child : elem.getChildren())
051: hs.add(((Element) child).getChildText("id"));
052:
053: return hs;
054: }
055:
056: //-----------------------------------------------------------------------------
057:
058: public Element pruneChildren(Element elem, Set<String> ids) {
059: ArrayList<Element> alToPrune = new ArrayList<Element>();
060:
061: //--- collect elements to prune
062:
063: for (Object obj : elem.getChildren()) {
064: Element child = (Element) obj;
065: String id = child.getChildText("id");
066:
067: if (!ids.contains(id))
068: alToPrune.add(child);
069: }
070:
071: //--- remove collected elements
072:
073: for (Element child : alToPrune)
074: child.detach();
075:
076: return elem;
077: }
078:
079: //-----------------------------------------------------------------------------
080:
081: public void add(Element el, String name, Object value) {
082: if (value != null)
083: el.addContent(new Element(name).setText(value.toString()));
084: }
085:
086: //-----------------------------------------------------------------------------
087:
088: public String eval(Element elem, String path) {
089: StringTokenizer st = new StringTokenizer(path, "/");
090:
091: while (st.hasMoreTokens()) {
092: elem = elem.getChild(st.nextToken());
093:
094: if (elem == null)
095: return null;
096: }
097:
098: return elem.getText().trim();
099: }
100:
101: //-----------------------------------------------------------------------------
102:
103: public void substitute(Element el,
104: Map<String, ? extends Object> vars) {
105: //--- handle attributes
106:
107: for (Iterator i = el.getAttributes().iterator(); i.hasNext();) {
108: Attribute a = (Attribute) i.next();
109:
110: String text = a.getValue();
111: text = substitute(text, vars);
112: a.setValue(text);
113: }
114:
115: //--- handle children
116:
117: for (int i = 0; i < el.getContentSize(); i++) {
118: Content c = el.getContent(i);
119:
120: if (c instanceof Element)
121: substitute((Element) c, vars);
122:
123: else if (c instanceof Text) {
124: Text t = (Text) c;
125:
126: String text = t.getText();
127: text = substitute(text, vars);
128: t.setText(text);
129: }
130: }
131: }
132:
133: //-----------------------------------------------------------------------------
134:
135: private String substitute(String text,
136: Map<String, ? extends Object> vars) {
137: for (String name : vars.keySet())
138: text = Util.replaceString(text, name, vars.get(name)
139: .toString());
140:
141: return text;
142: }
143: }
144:
145: //=============================================================================
|