www.pudn.com > sxg.rar > GatewaySetRequestFactory.java
/* * Copyright (c) 2003 Jens Mueller * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; see the file COPYING. If not, write to * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ /** Provides static methodgetGatewaySetRequestfor converting a String-representation of a XML-document into the accordant application's data structureGatewaySetRequest@author Jens Mueller */ import java.io.IOException; import java.io.PrintWriter; import java.io.StringReader; import java.net.InetAddress; import java.net.MalformedURLException; import java.util.Iterator; import java.util.List; import org.dom4j.io.SAXReader; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; public class GatewaySetRequestFactory { /** converts an XML-document representing a set-request into aGatewaySetRequest@param xmldocString the XML-document representing the set-request @return GatewaySetRequest @throws MalformedSetRequestException */ public static GatewaySetRequest getGatewaySetRequest( String xmldocString ) throws MalformedSetRequestException { GatewaySetRequest gwrequest = new GatewaySetRequest(); SAXReader reader = new SAXReader(); reader.setValidation( false ); Document doc; try { doc = reader.read( new StringReader ( xmldocString ) ); } catch( DocumentException e ) { throw new MalformedSetRequestException( "transmitted data not a " + "valid XML-document: "+e.toString() ); } List contextElements = doc.selectNodes( "/snmp-data/context" ); for ( Iterator iterator = contextElements.iterator(); iterator.hasNext(); ) { Element currContext = ( Element )iterator.next(); Attribute ipaddrAttr = currContext.attribute( GatewayConf.IPADDR ); Attribute hostnameAttr = currContext.attribute( GatewayConf.HOSTNAME ); Attribute portAttr = currContext.attribute( GatewayConf.PORT ); Attribute communityAttr = currContext.attribute( GatewayConf.COMM_STR ); if ( ( ipaddrAttr == null && hostnameAttr == null ) || communityAttr == null ) throw new MalformedSetRequestException( "not all required context " + "attributes specified" ); String ipaddr = null; String hostname = null; String portString = null; if ( ipaddrAttr != null ) ipaddr = ipaddrAttr.getValue(); if ( hostnameAttr != null ) hostname = hostnameAttr.getValue(); if ( portAttr != null ) portString = portAttr.getValue(); String community = communityAttr.getValue(); int port; InetAddress inetAddress; try { port = Integer.parseInt( portString ); } catch( Exception e ) { throw new MalformedSetRequestException( "port attribute not valid" ); } if ( port < 1 ) throw new MalformedSetRequestException( "port attribute not valid" ); try { inetAddress = InetAddress.getByName( hostname ); } catch( Exception e ) { throw new MalformedSetRequestException( "hostname "+hostname+" not resolvable" ); } ContextSetRequest currConSetReq = new ContextSetRequest( ipaddr, inetAddress, port, community ); gwrequest._contextRequestVector.add( currConSetReq ); for( Iterator typeIterator = currContext.elementIterator(); typeIterator.hasNext(); ) { Element currComplexTypeElement = ( Element )typeIterator.next(); GwComplexType currComplexType; try { currComplexType = MibRepositoryHandler.getInstance().getType( currComplexTypeElement.getName(), currComplexTypeElement.getNamespacePrefix() ); } catch( DocumentException e ) { throw new MalformedSetRequestException( "Gateway misconfigured: " + e.getMessage() ); } catch( MalformedURLException e ) { throw new MalformedSetRequestException( "Gateway misconfigured: " + e.getMessage() ); } catch( IOException e ) { throw new MalformedSetRequestException( "Gateway misconfigured: " + e.getMessage() ); } if ( currComplexType != null ) { // copy xml-data into internal data structure for( Iterator ElementIterator = currComplexTypeElement.elementIterator(); ElementIterator.hasNext(); ) { Element currElement = ( Element )ElementIterator.next(); for ( Iterator gwElementIterator = currComplexType._typeElements.iterator(); gwElementIterator.hasNext(); ) { Object currGwObject = gwElementIterator.next(); if ( currGwObject instanceof GwComplexType ) { // TODO: tables of tables } if ( currGwObject instanceof GwElement ) { GwElement currGwElement = ( GwElement )currGwObject; if( currGwElement.getName().equals( currElement.getName() ) ) { currGwElement.setSnmpSetData( currElement.getText() ); break; } } } } for( Iterator AttributeIterator = currComplexTypeElement.attributeIterator(); AttributeIterator.hasNext(); ) { Attribute currAttribute = ( Attribute )AttributeIterator.next(); for ( Iterator gwElementIterator = currComplexType._typeAttributes.iterator(); gwElementIterator.hasNext(); ) { GwElement currGwAttribute = ( GwElement )gwElementIterator.next(); if ( currGwAttribute.getName().equals( currAttribute.getName() ) ) { currGwAttribute.setSnmpSetData( currAttribute.getValue() ); break; } } } currConSetReq._gwComplexTypeVector.add( currComplexType ); } } } return gwrequest; } }