/* BasicWFSReader Copyright (C) 2006 Open Geospatial Consortium Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* * Author: Raj Singh * Online version: http://www.ogcnetwork.net/system/files?file=BasicWFSReader_java.txt * Last modified June 25, 2006 * Tested using JSE 1.5. JSE 1.4 should work with the JAXP 1.3 library. */ package org.ogc.wfs.client; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import java.util.Vector; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class BasicWFSReader { public Document capabilities = null; private URL serviceURL = null; private static String wfsVersion = "1.0.0"; Document xmlDocument = null; DocumentBuilderFactory documentBuilderFactory; public Document getFeatureFromFile(String filePath) { try { DocumentBuilder db = documentBuilderFactory.newDocumentBuilder(); File f = new File(filePath); xmlDocument = db.parse( new FileInputStream(f) ); return xmlDocument; } catch (Exception e) { e.printStackTrace(); } return null; } public Document getFeature(String wfsQuery, int maxFeatures) { String maxfeatures = ""; if ( maxFeatures > 0 ) maxfeatures = " maxFeatures=\"3\""; try { // set up the URL connection HttpURLConnection httpcon = (HttpURLConnection) serviceURL.openConnection(); httpcon.setDoInput(true); httpcon.setDoOutput(true); httpcon.setUseCaches(false); httpcon.setAllowUserInteraction(false); String q = ""; q += ""; q += wfsQuery; q += ""; // send POST request DataOutputStream dos = new DataOutputStream( httpcon.getOutputStream() ); dos.writeBytes( q ); dos.close(); DocumentBuilder db = documentBuilderFactory.newDocumentBuilder(); xmlDocument = db.parse( httpcon.getInputStream() ); return xmlDocument; } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } /** * getFeatures * @param featuretype WFS FeatureType * @param maxfeatures limit of features to return (0 means no limit) * @return XML document of features; null if featuretype doesn't exist or exception caught */ public Document getFeatureBasic(String featuretype, int maxfeatures) { if ( !hasFeatureType(featuretype) ) { System.err.println("in getFeature, feature type '"+featuretype+"' not found."); return null; } try { DocumentBuilder db = documentBuilderFactory.newDocumentBuilder(); featuretype = URLEncoder.encode( featuretype, "UTF-8" ); String req = "&request=GetFeature&TypeName=" + featuretype; if ( maxfeatures > 0 ) req += "&MaxFeatures=" + maxfeatures; URL gfurl = new URL( serviceURL.toString() + req ); xmlDocument = db.parse( gfurl.openStream() ); return xmlDocument; } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } public Document getFeatureBasic(String featuretype) { return getFeatureBasic(featuretype, 0); } public Document describeFeatureType(String featuretype) { if ( !hasFeatureType(featuretype) ) { System.err.println("in describeFeatureType, feature type '"+featuretype+"' not found."); return null; } try { DocumentBuilder db = documentBuilderFactory.newDocumentBuilder(); featuretype = URLEncoder.encode( featuretype, "UTF-8" ); URL dfturl = new URL( serviceURL.toString() + "&request=DescribeFeatureType&TypeName=" + featuretype ); xmlDocument = db.parse( dfturl.openStream() ); return xmlDocument; } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } public String[] getFeatureTypeNames() { if ( !hasCapabilities() ) return null; Element rootel = capabilities.getDocumentElement(); Element ftlist = (Element)rootel.getElementsByTagName("FeatureTypeList").item(0); NodeList fts = ftlist.getElementsByTagName("FeatureType"); Vector ftnamesvec = new Vector(fts.getLength()); for (int i=0; i