001    //$HeadURL: 
002    /*----------------------------------------------------------------------------
003     This file is part of deegree, http://deegree.org/
004     Copyright (C) 2001-2010 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.cataloguemanager.control;
037    
038    import java.io.IOException;
039    import java.net.MalformedURLException;
040    import java.net.URI;
041    import java.net.URL;
042    import java.util.ArrayList;
043    import java.util.HashMap;
044    import java.util.Iterator;
045    import java.util.List;
046    import java.util.Map;
047    
048    import org.deegree.framework.xml.NamespaceContext;
049    import org.deegree.framework.xml.XMLFragment;
050    import org.deegree.framework.xml.XMLParsingException;
051    import org.deegree.framework.xml.XMLTools;
052    import org.deegree.ogcbase.CommonNamespaces;
053    import org.w3c.dom.Element;
054    import org.xml.sax.SAXException;
055    
056    /**
057     * 
058     * 
059     * 
060     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
061     * @author last edited by: $Author: apoth $
062     * 
063     * @version $Revision: 27621 $, $Date: 2010-10-29 12:01:41 +0200 (Fr, 29 Okt 2010) $
064     */
065    public class CatalogueManagerConfiguration extends XMLFragment {
066    
067        private static final long serialVersionUID = -4330651238416870990L;
068    
069        private String catalogueURL;
070    
071        private Map<String, String> templateURL = new HashMap<String, String>();
072    
073        private String metadataSchema;
074    
075        private Map<String, String> xPathes = new HashMap<String, String>();
076    
077        private List<SpatialExtent> spatialExtents;
078    
079        private List<String> searchableProperties = new ArrayList<String>();
080    
081        private List<String> dateProperties = new ArrayList<String>();
082    
083        private List<String> searchableCSW = new ArrayList<String>();
084    
085        private List<RespPartyConfiguration> respParties;
086    
087        private char[] ignoreCharacters;
088    
089        private int stepSize;
090    
091        private URL briefHTMLXSL;
092    
093        private URL fullHTMLXSL;
094    
095        private URL pdfXSL;
096    
097        private URL linkageXSL;
098        
099        private String templateDirectory = "WEB-INF/conf/cataloguemanager/templates";
100    
101        private static NamespaceContext nsContext = null;
102        static {
103            if ( nsContext == null ) {
104                nsContext = CommonNamespaces.getNamespaceContext();
105                nsContext.addNamespace( "md", URI.create( "http://www.deegree.org/cataloguemanager" ) );
106            }
107        }
108    
109        /**
110         * @param url
111         * @throws IOException
112         * @throws SAXException
113         * @throws XMLParsingException
114         */
115        CatalogueManagerConfiguration( URL url ) throws IOException, SAXException, XMLParsingException {
116            super( url );
117            parseConfiguration();
118        }
119    
120        /**
121         * @throws XMLParsingException
122         * @throws MalformedURLException
123         */
124        private void parseConfiguration()
125                                throws XMLParsingException, MalformedURLException {
126            catalogueURL = XMLTools.getRequiredNodeAsString( getRootElement(),
127                                                             "./md:CatalogueService/md:onlineResource/@xlink:href",
128                                                             nsContext );
129            metadataSchema = XMLTools.getRequiredNodeAsString( getRootElement(), "./md:CatalogueService/md:metadataSchema",
130                                                               nsContext );
131            List<Element> list = XMLTools.getElements( getRootElement(), "./md:Templates/md:Template", nsContext );
132            for ( Element element : list ) {
133                String level = XMLTools.getRequiredNodeAsString( element, "./@hierarchyLevel", nsContext );
134                String template = XMLTools.getRequiredNodeAsString( element, "./md:onlineResource/@xlink:href", nsContext );
135                templateURL.put( level, template );
136            }
137            list = XMLTools.getElements( getRootElement(), "./md:MD_Elements/md:element", nsContext );
138            for ( Element element : list ) {
139                String name = XMLTools.getRequiredNodeAsString( element, "./@name", nsContext );
140                String value = XMLTools.getRequiredNodeAsString( element, "./text()", nsContext );
141                xPathes.put( name, value );
142            }
143    
144            list = XMLTools.getElements( getRootElement(), "./md:BoundingBoxes/md:BBOX", nsContext );
145            spatialExtents = new ArrayList<SpatialExtent>( list.size() );
146            for ( Element element : list ) {
147                String id = XMLTools.getRequiredNodeAsString( element, "./@id", nsContext );
148                String name = XMLTools.getRequiredNodeAsString( element, "./@name", nsContext );
149                String bbox = XMLTools.getStringValue( element );
150                SpatialExtent se = new SpatialExtent();
151                se.setBbox( bbox );
152                se.setId( id );
153                se.setName( name );
154                spatialExtents.add( se );
155            }
156            parseResponsibleParties();
157            parseSearchSection();
158        }
159    
160        /**
161         * @throws XMLParsingException
162         * 
163         */
164        private void parseResponsibleParties()
165                                throws XMLParsingException {
166            List<Element> list = XMLTools.getElements( getRootElement(), "./md:ResponsibleParties/md:ResponsibleParty",
167                                                       nsContext );
168            respParties = new ArrayList<RespPartyConfiguration>( list.size() );
169            for ( Element el : list ) {
170                RespPartyConfiguration respParty = new RespPartyConfiguration();
171                respParty.setDisplayName( XMLTools.getRequiredNodeAsString( el, "md:DisplayName", nsContext ) );
172                respParty.setIndividualName( XMLTools.getRequiredNodeAsString( el, "md:IndividualName", nsContext ) );
173                respParty.setOrganisationName( XMLTools.getRequiredNodeAsString( el, "md:OrganisationName", nsContext ) );
174                respParty.setStreet( XMLTools.getRequiredNodeAsString( el, "md:Street", nsContext ) );
175                respParty.setCity( XMLTools.getRequiredNodeAsString( el, "md:City", nsContext ) );
176                respParty.setCountry( XMLTools.getRequiredNodeAsString( el, "md:Country", nsContext ) );
177                respParty.setPostalCode( XMLTools.getRequiredNodeAsString( el, "md:PostalCode", nsContext ) );
178                respParty.setVoice( XMLTools.getRequiredNodeAsString( el, "md:Voice", nsContext ) );
179                respParty.setFacsimile( XMLTools.getNodeAsString( el, "md:Facsimile", nsContext, "" ) );
180                respParty.setEmail( XMLTools.getRequiredNodeAsString( el, "md:EMail", nsContext ) );
181                respParties.add( respParty );
182            }
183        }
184    
185        /**
186         * @throws XMLParsingException
187         * @throws MalformedURLException
188         * 
189         */
190        private void parseSearchSection()
191                                throws XMLParsingException, MalformedURLException {
192            String xpath = "md:Search/md:searchableProperties/md:Property";
193            List<Element> elements = XMLTools.getRequiredElements( getRootElement(), xpath, nsContext );
194            for ( Element element : elements ) {
195                String s = '{' + element.getAttribute( "namespace" ) + "}:" + element.getAttribute( "name" );
196                searchableProperties.add( s );
197            }
198            xpath = "md:Search/md:dateProperties/md:Property";
199            elements = XMLTools.getRequiredElements( getRootElement(), xpath, nsContext );
200            for ( Element element : elements ) {
201                String s = '{' + element.getAttribute( "namespace" ) + "}:" + element.getAttribute( "name" );
202                dateProperties.add( s );
203            }
204            xpath = "md:Search/md:ignoreCharacters";
205            String s = XMLTools.getNodeAsString( getRootElement(), xpath, nsContext, "" );
206            ignoreCharacters = new char[s.length()];
207            for ( int i = 0; i < s.length(); i++ ) {
208                ignoreCharacters[i] = s.charAt( i );
209            }
210            xpath = "md:Search/md:stepSize";
211            stepSize = XMLTools.getRequiredNodeAsInt( getRootElement(), xpath, nsContext );
212    
213            xpath = "md:Search/md:searchableCSW/md:CSW";
214            elements = XMLTools.getElements( getRootElement(), xpath, nsContext );
215            for ( Element element : elements ) {
216                searchableCSW.add( element.getAttribute( "xlink:href" ) );
217            }
218    
219            xpath = "md:Search/md:briefHTMLFormat/@xlink:href";
220            s = XMLTools.getRequiredNodeAsString( getRootElement(), xpath, nsContext );
221            briefHTMLXSL = resolve( s );
222    
223            xpath = "md:Search/md:fullHTMLFormat/@xlink:href";
224            s = XMLTools.getRequiredNodeAsString( getRootElement(), xpath, nsContext );
225            fullHTMLXSL = resolve( s );
226    
227            xpath = "md:Search/md:pdfFormat/@xlink:href";
228            s = XMLTools.getNodeAsString( getRootElement(), xpath, nsContext, null );
229            if ( s != null ) {
230                pdfXSL = resolve( s );
231            }
232    
233            xpath = "md:Search/md:linkageFormat/@xlink:href";
234            s = XMLTools.getNodeAsString( getRootElement(), xpath, nsContext, null );
235            if ( s != null ) {
236                linkageXSL = resolve( s );
237            }
238        }
239    
240        /**
241         * 
242         * @return list of available responsible parties
243         */
244        List<RespPartyConfiguration> getResponsibleParties() {
245            return respParties;
246        }
247    
248        /**
249         * 
250         * @return base URL to CSW
251         */
252        String getCatalogueURL() {
253            return catalogueURL;
254        }
255    
256        /**
257         * 
258         * @return metadata schema editing is performed on
259         */
260        String getMetadataSchema() {
261            return metadataSchema;
262        }
263    
264        /**
265         * 
266         * @param hierarchyLevel
267         * @return URL to metadata template depending on hierarchy level
268         */
269        String getTemplateURL( String hierarchyLevel ) {
270            return templateURL.get( hierarchyLevel );
271        }
272    
273        /**
274         * 
275         * @param name
276         * @return xPath assigned to the passed name
277         */
278        String getXPath( String name ) {
279            return xPathes.get( name );
280        }
281    
282        /**
283         * 
284         * @return list of names of all available xpath expressions
285         */
286        List<String> getXPathNames() {
287            List<String> list = new ArrayList<String>( 50 );
288            Iterator<String> iterator = xPathes.keySet().iterator();
289            while ( iterator.hasNext() ) {
290                list.add( iterator.next() );
291            }
292            return list;
293        }
294    
295        /**
296         * 
297         * @return available spatial extents
298         */
299        List<SpatialExtent> getSpatialExtents() {
300            return spatialExtents;
301        }
302    
303        public List<String> getSearchableCSW() {
304            return searchableCSW;
305        }
306    
307        public List<String> getSearchableProperties() {
308            return searchableProperties;
309        }
310    
311        public List<String> getDateProperties() {
312            return dateProperties;
313        }
314    
315        public char[] getIgnoreCharacters() {
316            return ignoreCharacters;
317        }
318    
319        public int getStepSize() {
320            return stepSize;
321        }
322    
323        /**
324         * @return the briefHTMLXSL
325         */
326        public URL getBriefHTMLXSL() {
327            return briefHTMLXSL;
328        }
329    
330        /**
331         * @return the fullHTMLXSL
332         */
333        public URL getFullHTMLXSL() {
334            return fullHTMLXSL;
335        }
336    
337        /**
338         * @return the pdfXSL
339         */
340        public URL getPdfXSL() {
341            return pdfXSL;
342        }
343    
344        /**
345         * @return the linkageXSL
346         */
347        public URL getLinkageXSL() {
348            return linkageXSL;
349        }
350        
351        /**
352         * 
353         * @return relative path of the directory where available metadata templates can be found
354         */
355        public String getTemplateDirectory() {
356            return templateDirectory;
357        }
358    
359    }