001 //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/portal/standard/gazetteer/Hierarchy.java $ 002 /*---------------------------------------------------------------------------- 003 This file is part of deegree, http://deegree.org/ 004 Copyright (C) 2001-2009 by: 005 - Department of Geography, University of Bonn - 006 and 007 - lat/lon GmbH - 008 009 This library is free software; you can redistribute it and/or modify it under 010 the terms of the GNU Lesser General Public License as published by the Free 011 Software Foundation; either version 2.1 of the License, or (at your option) 012 any later version. 013 This library is distributed in the hope that it will be useful, but WITHOUT 014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 015 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 016 details. 017 You should have received a copy of the GNU Lesser General Public License 018 along with this library; if not, write to the Free Software Foundation, Inc., 019 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 020 021 Contact information: 022 023 lat/lon GmbH 024 Aennchenstr. 19, 53177 Bonn 025 Germany 026 http://lat-lon.de/ 027 028 Department of Geography, University of Bonn 029 Prof. Dr. Klaus Greve 030 Postfach 1147, 53001 Bonn 031 Germany 032 http://www.geographie.uni-bonn.de/deegree/ 033 034 e-mail: info@deegree.org 035 ----------------------------------------------------------------------------*/ 036 package org.deegree.portal.standard.gazetteer; 037 038 import java.net.URI; 039 import java.util.HashMap; 040 import java.util.Map; 041 042 import org.deegree.datatypes.QualifiedName; 043 import org.deegree.framework.xml.NamespaceContext; 044 import org.deegree.framework.xml.XMLFragment; 045 import org.deegree.framework.xml.XMLParsingException; 046 import org.deegree.framework.xml.XMLTools; 047 import org.deegree.ogcbase.CommonNamespaces; 048 import org.w3c.dom.Element; 049 050 /** 051 * Constructor expects a document formated like 052 * 053 * <pre> 054 * <Hierarchy name="MyGazetteer" address="http://localhost/deegree-gaz/services"> 055 * <Type name="Länder" freeSearch="true"> 056 * <FeatureType name="XYZ" namespace="http://www.deegree.org/app"/> 057 * <Type name="Kreise" freeSearch="false"> 058 * <FeatureType name="ABC" namespace="http://www.deegree.org/app"/> 059 * <Type name="Gemeinden" freeSearch="true"> 060 * <FeatureType name="MNO" namespace="http://www.deegree.org/app"/> 061 * </Type> 062 * </Type> 063 * </Type> 064 * </Hierarchy> 065 * </pre> 066 * 067 * @author <a href="mailto:name@deegree.org">Andreas Poth</a> 068 * @author last edited by: $Author: apoth $ 069 * 070 * @version $Revision: 22457 $, $Date: 2010-02-09 12:58:22 +0100 (Di, 09 Feb 2010) $ 071 */ 072 public class Hierarchy { 073 074 private String gazetteerAddress; 075 076 private String name; 077 078 private HierarchyNode root; 079 080 /** 081 * <pre> 082 * 083 * </pre> 084 * 085 * @param xml 086 * @throws XMLParsingException 087 */ 088 public Hierarchy( XMLFragment xml ) throws XMLParsingException { 089 NamespaceContext nsc = CommonNamespaces.getNamespaceContext(); 090 Element node = xml.getRootElement(); 091 name = XMLTools.getAttrValue( node, null, "name", null ); 092 gazetteerAddress = XMLTools.getAttrValue( node, null, "address", null ); 093 node = (Element) XMLTools.getNode( node, "Type", CommonNamespaces.getNamespaceContext() ); 094 095 String nm = XMLTools.getAttrValue( node, null, "name", null ); 096 boolean freeSearch = "true".equalsIgnoreCase( XMLTools.getAttrValue( node, null, "freeSearch", null ) ); 097 boolean strictMode = true; 098 boolean matchCase = true; 099 if ( freeSearch ) { 100 strictMode = "true".equalsIgnoreCase( XMLTools.getAttrValue( node, null, "strictMode", null ) ); 101 matchCase = "true".equalsIgnoreCase( XMLTools.getAttrValue( node, null, "matchCase", null ) ); 102 } 103 Element ftnode = (Element) XMLTools.getNode( node, "FeatureType", nsc ); 104 String ftName = XMLTools.getAttrValue( ftnode, null, "name", null ); 105 String nsp = XMLTools.getAttrValue( ftnode, null, "namespace", null ); 106 Map<String, String> properties = readPropertyNames( nsc, ftnode ); 107 108 root = new HierarchyNode( new QualifiedName( ftName, URI.create( nsp ) ), properties, nm, freeSearch, 109 strictMode, matchCase ); 110 HierarchyNode parent = root; 111 112 node = (Element) XMLTools.getNode( node, "Type", CommonNamespaces.getNamespaceContext() ); 113 while ( node != null ) { 114 nm = XMLTools.getAttrValue( node, null, "name", null ); 115 freeSearch = "true".equals( XMLTools.getAttrValue( node, null, "freeSearch", null ) ); 116 ftnode = (Element) XMLTools.getNode( node, "FeatureType", CommonNamespaces.getNamespaceContext() ); 117 ftName = XMLTools.getAttrValue( ftnode, null, "name", null ); 118 nsp = XMLTools.getAttrValue( ftnode, null, "namespace", null ); 119 properties = readPropertyNames( nsc, ftnode ); 120 HierarchyNode hn = new HierarchyNode( new QualifiedName( ftName, URI.create( nsp ) ), properties, nm, 121 freeSearch ); 122 parent.setChildNode( hn ); 123 parent = hn; 124 node = (Element) XMLTools.getNode( node, "Type", CommonNamespaces.getNamespaceContext() ); 125 } 126 } 127 128 private Map<String, String> readPropertyNames( NamespaceContext nsc, Element ftnode ) 129 throws XMLParsingException { 130 String geogrIdPr = XMLTools.getRequiredNodeAsString( ftnode, "./GeographicIdentifier/@property", nsc ); 131 String altGeogrIdPr = XMLTools.getNodeAsString( ftnode, "./AlternativeGeographicIdentifier/@property", nsc, 132 null ); 133 String displayName = XMLTools.getNodeAsString( ftnode, "./DisplayName/@property", nsc, geogrIdPr ); 134 String parentIdPr = XMLTools.getNodeAsString( ftnode, "./ParentIdentifier/@property", nsc, null ); 135 String geoExtPr = XMLTools.getRequiredNodeAsString( ftnode, "./GeographicExtent/@property", nsc ); 136 String highlightGeometry = XMLTools.getNodeAsString( ftnode, "./HighlightGeometry/@property", nsc, geoExtPr ); 137 String posPr = XMLTools.getNodeAsString( ftnode, "./Position/@property", nsc, null ); 138 Map<String, String> properties = new HashMap<String, String>(); 139 properties.put( "GeographicIdentifier", geogrIdPr ); 140 properties.put( "AlternativeGeographicIdentifier", altGeogrIdPr ); 141 properties.put( "ParentIdentifier", parentIdPr ); 142 properties.put( "GeographicExtent", geoExtPr ); 143 properties.put( "Position", posPr ); 144 properties.put( "DisplayName", displayName ); 145 properties.put( "HighlightGeometry", highlightGeometry ); 146 return properties; 147 } 148 149 /** 150 * 151 * @return root node of a gazetteer hierarchy 152 */ 153 public HierarchyNode getRoot() { 154 return root; 155 } 156 157 /** 158 * @return the gazetteerAddress 159 */ 160 public String getGazetteerAddress() { 161 return gazetteerAddress; 162 } 163 164 /** 165 * @return the name 166 */ 167 public String getName() { 168 return name; 169 } 170 171 /** 172 * 173 * @param name 174 * @return parent node for {@link HierarchyNode} matching passed name 175 */ 176 public HierarchyNode getParentOf( String name ) { 177 HierarchyNode node = root; 178 while ( node.getChildNode() != null ) { 179 if ( node.getChildNode().equals( name ) ) { 180 return node; 181 } 182 node = node.getChildNode(); 183 } 184 return null; 185 } 186 187 /** 188 * 189 * @param name 190 * @return child node for {@link HierarchyNode} matching passed name 191 */ 192 public HierarchyNode getChildOf( String name ) { 193 HierarchyNode node = root; 194 do { 195 if ( node.equals( name ) ) { 196 return node; 197 } 198 node = node.getChildNode(); 199 } while ( node.getChildNode() != null ); 200 return null; 201 } 202 203 @Override 204 public String toString() { 205 return name; 206 } 207 208 }