001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/ogcwebservices/csw/manager/XMLFactory.java $ 002 /*---------------- FILE HEADER ------------------------------------------ 003 004 This file is part of deegree. 005 Copyright (C) 2001-2006 by: 006 EXSE, Department of Geography, University of Bonn 007 http://www.giub.uni-bonn.de/deegree/ 008 lat/lon GmbH 009 http://www.lat-lon.de 010 011 This library is free software; you can redistribute it and/or 012 modify it under the terms of the GNU Lesser General Public 013 License as published by the Free Software Foundation; either 014 version 2.1 of the License, or (at your option) any later version. 015 016 This library is distributed in the hope that it will be useful, 017 but WITHOUT ANY WARRANTY; without even the implied warranty of 018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 019 Lesser General Public License for more details. 020 021 You should have received a copy of the GNU Lesser General Public 022 License along with this library; if not, write to the Free Software 023 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 024 025 Contact: 026 027 Andreas Poth 028 lat/lon GmbH 029 Aennchenstr. 19 030 53115 Bonn 031 Germany 032 E-Mail: poth@lat-lon.de 033 034 Prof. Dr. Klaus Greve 035 Department of Geography 036 University of Bonn 037 Meckenheimer Allee 166 038 53115 Bonn 039 Germany 040 E-Mail: greve@giub.uni-bonn.de 041 042 ---------------------------------------------------------------------------*/ 043 package org.deegree.ogcwebservices.csw.manager; 044 045 import java.net.URI; 046 import java.util.List; 047 048 import org.deegree.framework.log.ILogger; 049 import org.deegree.framework.log.LoggerFactory; 050 import org.deegree.framework.xml.XMLException; 051 import org.deegree.framework.xml.XMLParsingException; 052 import org.deegree.framework.xml.XMLTools; 053 import org.deegree.model.filterencoding.Filter; 054 import org.deegree.ogcbase.CommonNamespaces; 055 import org.deegree.ogcwebservices.OGCWebServiceException; 056 import org.deegree.ogcwebservices.wfs.operation.transaction.TransactionResponse; 057 import org.w3c.dom.Document; 058 import org.w3c.dom.Element; 059 import org.w3c.dom.Node; 060 061 /** 062 * 063 * @version $Revision: 7903 $ 064 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a> 065 * @author last edited by: $Author: apoth $ 066 * 067 * @version $Revision: 7903 $, $Date: 2007-08-06 10:23:40 +0200 (Mo, 06 Aug 2007) $ 068 * 069 */ 070 public class XMLFactory { 071 072 private static ILogger LOG = LoggerFactory.getLogger( XMLFactory.class ); 073 074 private static final URI CSWNS = CommonNamespaces.CSWNS; 075 076 /** 077 * @return a XML representation of a {@link Transaction} object 078 * 079 * @param transaction 080 * @throws XMLParsingException 081 * @throws XMLException 082 * @throws OGCWebServiceException 083 */ 084 public static final TransactionDocument export( Transaction transaction ) throws XMLParsingException, 085 OGCWebServiceException { 086 087 TransactionDocument transDoc = new TransactionDocument(); 088 try { 089 transDoc.createEmptyDocument(); 090 } catch ( Exception e ) { 091 throw new XMLParsingException( e.getMessage() ); 092 } 093 094 transDoc.getRootElement().setAttribute( "service", "CSW" ); 095 String version = transaction.getVersion(); 096 if( version == null || "".equals( version.trim() ) ){ 097 version = "2.0.0"; 098 } 099 transDoc.getRootElement().setAttribute( "version", version ); 100 transDoc.getRootElement().setAttribute( "verboseResponse", "" + transaction.verboseResponse() ); 101 102 List<Operation> ops = transaction.getOperations(); 103 for ( int i = 0; i < ops.size(); i++ ) { 104 Operation operation = ops.get( i ); 105 appendOperation( transDoc.getRootElement(), operation ); 106 } 107 108 return transDoc; 109 110 } 111 112 /** 113 * @return an XML representation of a {@link TransactionResponse} object 114 * 115 * @param response 116 * @throws XMLParsingException 117 * @throws OGCWebServiceException 118 */ 119 public static final HarvetResultDocument export( HarvestResult response ) throws XMLParsingException { 120 121 HarvetResultDocument harvestRespDoc = new HarvetResultDocument(); 122 try { 123 harvestRespDoc.createEmptyDocument(); 124 } catch ( Exception e ) { 125 throw new XMLParsingException( e.getMessage() ); 126 } 127 128 Element root = harvestRespDoc.getRootElement(); 129 String version = response.getRequest().getVersion(); 130 if( version == null || "".equals( version.trim() ) ){ 131 version = "2.0.1"; 132 } 133 root.setAttribute( "version", version ); 134 135 Element elem = XMLTools.appendElement( root, CommonNamespaces.CSWNS, "csw:TransactionSummary" ); 136 root.appendChild( elem ); 137 XMLTools.appendElement( elem, 138 CommonNamespaces.CSWNS, 139 "csw:totalInserted", 140 Integer.toString( response.getTotalInserted() ) ); 141 XMLTools.appendElement( elem, 142 CommonNamespaces.CSWNS, 143 "csw:totalUpdated", 144 Integer.toString( response.getTotalUpdated() ) ); 145 XMLTools.appendElement( elem, 146 CommonNamespaces.CSWNS, 147 "csw:totalDeleted", 148 Integer.toString( response.getTotalDeleted() ) ); 149 150 List<Node> records = response.getResults().getRecords(); 151 if ( records.size() > 0 ) { 152 elem = XMLTools.appendElement( root, CommonNamespaces.CSWNS, "csw:InsertResult" ); 153 Document owner = root.getOwnerDocument(); 154 for ( int i = 0; i< records.size(); ++i ) { 155 LOG.logDebug( "("+i+" of "+ records.size() +") trying to insert xmlnode: " + records.get( i ).getNodeName() ); 156 Node a = owner.importNode( records.get( i ), true); 157 elem.appendChild( a ); 158 } 159 } 160 161 return harvestRespDoc; 162 } 163 164 /** 165 * @return an XML representation of a {@link TransactionResponse} object 166 * 167 * @param response 168 * @throws XMLParsingException 169 * @throws OGCWebServiceException 170 */ 171 public static final TransactionResultDocument export( TransactionResult response ) throws XMLParsingException { 172 173 TransactionResultDocument transRespDoc = new TransactionResultDocument(); 174 try { 175 transRespDoc.createEmptyDocument(); 176 } catch ( Exception e ) { 177 throw new XMLParsingException( e.getMessage() ); 178 } 179 180 Element root = transRespDoc.getRootElement(); 181 String version = response.getRequest().getVersion(); 182 if( version == null || "".equals( version.trim() ) ){ 183 version = "2.0.1"; 184 } 185 root.setAttribute( "version", version ); 186 187 Element elem = XMLTools.appendElement( root, CommonNamespaces.CSWNS, "csw:TransactionSummary" ); 188 root.appendChild( elem ); 189 XMLTools.appendElement( elem, 190 CommonNamespaces.CSWNS, 191 "csw:totalInserted", 192 Integer.toString( response.getTotalInserted() ) ); 193 XMLTools.appendElement( elem, 194 CommonNamespaces.CSWNS, 195 "csw:totalUpdated", 196 Integer.toString( response.getTotalUpdated() ) ); 197 XMLTools.appendElement( elem, 198 CommonNamespaces.CSWNS, 199 "csw:totalDeleted", 200 Integer.toString( response.getTotalDeleted() ) ); 201 202 List<Node> records = response.getResults().getRecords(); 203 if ( records.size() > 0 ) { 204 elem = XMLTools.appendElement( root, CommonNamespaces.CSWNS, "csw:InsertResult" ); 205 Document owner = root.getOwnerDocument(); 206 for ( int i = 0; i< records.size(); ++i ) { 207 LOG.logDebug( "("+i+" of "+ records.size() +") trying to insert xmlnode: " + records.get( i ).getNodeName() ); 208 Node a = owner.importNode( records.get( i ), true); 209 elem.appendChild( a ); 210 } 211 LOG.logDebug( "Successfully inserted " + records.size() +" brief records into the result documents" ); 212 //root.appendChild( elem ); 213 } 214 215 return transRespDoc; 216 217 } 218 219 /** 220 * 221 * @param root 222 * @param operation 223 * @throws OGCWebServiceException 224 */ 225 public static void appendOperation( Element root, Operation operation ) throws OGCWebServiceException { 226 227 if ( "Insert".equals( operation.getName() ) ) { 228 appendInsert( root, (Insert) operation ); 229 } else if ( "Update".equals( operation.getName() ) ) { 230 appendUpdate( root, (Update) operation ); 231 } else if ( "Delete".equals( operation.getName() ) ) { 232 appendDelete( root, (Delete) operation ); 233 } else { 234 throw new OGCWebServiceException( "unknown CS-W transaction operation: " + operation.getName() ); 235 } 236 } 237 238 /** 239 * appends an Delete operation to the passed root element 240 * 241 * @param root 242 * @param delete 243 */ 244 public static void appendDelete( Element root, Delete delete ) { 245 Document doc = root.getOwnerDocument(); 246 Element op = doc.createElementNS( CSWNS.toASCIIString(), "csw:" + delete.getName() ); 247 if ( delete.getHandle() != null ) { 248 op.setAttribute( "handle", delete.getHandle() ); 249 } 250 if ( delete.getTypeName() != null ) { 251 252 op.setAttribute( "typeName", delete.getTypeName().toASCIIString() ); 253 } 254 255 Filter filter = delete.getConstraint(); 256 Element constraint = doc.createElementNS( CSWNS.toASCIIString(), "csw:Constraint" ); 257 constraint.setAttribute( "version", "1.0.0" ); 258 op.appendChild( constraint ); 259 org.deegree.model.filterencoding.XMLFactory.appendFilter( constraint, filter ); 260 root.appendChild( op ); 261 262 } 263 264 /** 265 * appends an Update operation to the passed root element 266 * 267 * @param root 268 * @param update 269 */ 270 public static void appendUpdate( Element root, Update update ) { 271 Document doc = root.getOwnerDocument(); 272 Element op = doc.createElementNS( CSWNS.toASCIIString(), "csw:" + update.getName() ); 273 if ( update.getHandle() != null ) { 274 op.setAttribute( "handle", update.getHandle() ); 275 } 276 if ( update.getTypeName() != null ) { 277 op.setAttribute( "typeName", update.getTypeName().toASCIIString() ); 278 } 279 XMLTools.insertNodeInto( update.getRecord(), op ); 280 Filter filter = update.getConstraint(); 281 Element constraint = doc.createElementNS( CSWNS.toASCIIString(), "csw:Constraint" ); 282 constraint.setAttribute( "version", "1.0.0" ); 283 op.appendChild( constraint ); 284 org.deegree.model.filterencoding.XMLFactory.appendFilter( constraint, filter ); 285 root.appendChild( op ); 286 } 287 288 /** 289 * appends an Insert operation to the passed root element 290 * 291 * @param root 292 * @param insert 293 */ 294 public static void appendInsert( Element root, Insert insert ) { 295 Document doc = root.getOwnerDocument(); 296 Element tmp = doc.createElementNS( CSWNS.toASCIIString(), "csw:" + insert.getName() ); 297 Element op = (Element)root.appendChild( tmp ); 298 if ( insert.getHandle() != null ) { 299 op.setAttribute( "handle", insert.getHandle() ); 300 } 301 List<Element> list = insert.getRecords(); 302 for ( Element e : list ) { 303 Node copy = doc.importNode( e, true ); 304 op.appendChild( copy ); 305 } 306 // for ( int i = 0; i < list.size(); i++ ) { 307 // XMLTools.insertNodeInto( list.get( i ), op ); 308 // } 309 310 311 } 312 }