List of usage examples for org.dom4j Element attributeValue
String attributeValue(QName qName);
From source file:com.devoteam.srit.xmlloader.diameter.MsgDiameterParser.java
License:Open Source License
/** Parses then returns the header from the XML root element */ private void parseMessageHeader(MessageHeader messageHeader, Boolean request, Element root) throws Exception { ///*from w ww . j a v a2 s. c o m*/ // Parse header tag // Element element = root.element("header"); // // header tag is mandatory // if (null == element) throw new ParsingException("can't get header tag"); // // applicationId // String applicationId = element.attributeValue("applicationId"); messageHeader.application_id = Integer.parseInt(applicationId); // // Command code // String commandCode = element.attributeValue("command"); messageHeader.command_code = Integer.parseInt(commandCode); // // Request // String strRequest = element.attributeValue("request"); if (strRequest != null) { request = Boolean.parseBoolean(strRequest); } if (request == null) { request = false; } messageHeader.setRequest(request); // // Proxyable // String proxyable = element.attributeValue("proxiable"); if (proxyable != null) { messageHeader.setProxiable(Boolean.parseBoolean(proxyable)); } // // Error // String error = element.attributeValue("error"); if (error != null) { messageHeader.setError(Boolean.parseBoolean(error)); } // // Retransmit // String retransmit = element.attributeValue("retransmit"); if (retransmit != null) { messageHeader.setRetransmit(Boolean.parseBoolean(retransmit)); } // // Flags attribute is not used // if (null != element.attributeValue("flags")) throw new ParsingException("Flags attribute is currently not supported in <header> tag"); // // Version attribute is not used // if (null != element.attributeValue("version")) throw new ParsingException("Version attribute is currently not supported in <header> tag"); // // EndToEnd // String endToEnd = element.attributeValue("endToEnd"); if (endToEnd != null) { messageHeader.end_to_end_identifier = Integer.parseInt(endToEnd); } else { messageHeader.end_to_end_identifier = IDProvider.nextId(); } // // HopByHop // String hopByHop = element.attributeValue("hopByHop"); if (hopByHop != null) { messageHeader.hop_by_hop_identifier = Integer.parseInt(hopByHop); } else { messageHeader.hop_by_hop_identifier = IDProvider.nextId(); } }
From source file:com.devoteam.srit.xmlloader.diameter.MsgDiameterParser.java
License:Open Source License
/** Parses an element <avp>; recursively if it contains other AVPs, used by parseMessage */ private AVP parseAvp(Element element) throws Exception { AVP avp;/*www . j a v a 2 s . c o m*/ // avp attributes String code_str = element.attributeValue("code"); String value = element.attributeValue("value"); String type = element.attributeValue("type"); String vendor_id_str = element.attributeValue("vendorId"); String mandatory_str = element.attributeValue("mandatory"); String private_str = element.attributeValue("private"); String state = element.attributeValue("state"); boolean bState = true; if (state != null && state.equalsIgnoreCase("false")) { bState = false; } if (!bState) { return null; } int vendor_id = 0; int code = 0; boolean mandatory_bool = false; boolean private_bool = false; // error if no code is specified if (null == code_str) throw new ParsingException("In element :" + element + "\n" + "no avp code"); // flags attribute is not used if (null != element.attributeValue("flags")) throw new ParsingException( "In element :" + element + "\n" + "flags attribute is currently not supported in avp tag"); // vendorSpecific attribute is not used if (null != element.attributeValue("vendorSpecific")) throw new ParsingException("In element :" + element + "\n" + "VendorSpecific attribute is currently not supported in avp tag"); if (null == type) { type = "OctetString"; } // // AVP Code // code = Integer.parseInt(code_str); // // AVP Mandatory Flag // if (null != mandatory_str) { mandatory_bool = Boolean.parseBoolean(mandatory_str); } // // AVP Private Flag // if (null != private_str) { private_bool = Boolean.parseBoolean(private_str); } // // AVP Vendor ID // if (null != vendor_id_str) { vendor_id = Integer.parseInt(vendor_id_str); } // // Grouped AVP // if (type.equalsIgnoreCase("grouped")) { // // Parse child avps // List<AVP> avpList = new LinkedList<AVP>(); List<Element> list = element.elements("avp"); for (Element e : list) { AVP subAvp = parseAvp(e); if (subAvp != null) { avpList.add(subAvp); } } // create the AVP // add parsed childs to grouped AVP AVP_Grouped gAvp = new AVP_Grouped(code, avpList.toArray(new AVP[0])); avp = gAvp; } else { // // Error if no value is specified // if (null == value) throw new ParsingException("In element :" + element + "\n" + "AVP of code " + code_str + " should have a value since it is not a grouped AVP"); // // Create the AVP // if (type.equalsIgnoreCase("OctetString")) { try { avp = new AVP_OctetString(code, Utils.parseBinaryString(value)); } catch (Exception e) { avp = new AVP_OctetString(code, value.getBytes()); } } else if (type.equalsIgnoreCase("IPAddress")) { avp = new AVP_OctetString(code, InetAddress.getByName(value).getAddress()); } else if (type.equalsIgnoreCase("UTF8String")) { avp = new AVP_OctetString(code, value.getBytes()); } else if (type.equalsIgnoreCase("Integer32")) { avp = new AVP_Integer32(code, Integer.parseInt(value)); } else if (type.equalsIgnoreCase("Integer64")) { avp = new AVP_Integer64(code, Long.parseLong(value)); } else if (type.equalsIgnoreCase("Unsigned32")) { UnsignedInt32 unsignedInt32 = new UnsignedInt32(value); avp = new AVP_Unsigned32(code, unsignedInt32.intValue()); } else if (type.equalsIgnoreCase("Unsigned64")) { UnsignedInt64 unsignedInt64 = new UnsignedInt64(value); avp = new AVP_Unsigned64(code, unsignedInt64.longValue()); } else { throw new ParsingException("no matching avp type in protocol stack for type " + type); } } avp.vendor_id = vendor_id; avp.setMandatory(mandatory_bool); avp.setPrivate(private_bool); return avp; }
From source file:com.devoteam.srit.xmlloader.diameter.MsgDiameterParser.java
License:Open Source License
public void doDictionnary(Element root, String applicationId, boolean recurse) throws ParsingException { Application application = Dictionary.getInstance().getApplication(applicationId); if (null == application) { throw new ParsingException("Unknown \"applicationId\" attribute in header: " + applicationId); }/*from w w w. ja v a2 s . c o m*/ Element unmodifiedRoot = root.createCopy(); if (root.getName().equalsIgnoreCase("header")) { // // ApplicationId // String attributeValue; attributeValue = root.attributeValue("applicationId"); if (!Utils.isInteger(attributeValue)) { root.attribute("applicationId").setValue(Integer.toString(application.get_id())); } // // CommandCode // attributeValue = root.attributeValue("command"); if (!Utils.isInteger(attributeValue)) { CommandDef commandDef = Dictionary.getInstance().getCommandDefByName(attributeValue, applicationId); if (commandDef == null) { throw (new ParsingException( "Unknown \"command\" attribute in header: " + attributeValue + "skipp it")); } root.attribute("command").setValue(Integer.toString(commandDef.get_code())); } } else if (root.getName().equalsIgnoreCase("avp")) { boolean isTypeAppId = false; boolean isTypeVendorId = false; String attributeValue; attributeValue = root.attributeValue("code"); // // Set default values implied by code in XMLTree from dictionnary // if (null != attributeValue) { AvpDef avpDef; if (!Utils.isInteger(attributeValue)) { avpDef = Dictionary.getInstance().getAvpDefByName(attributeValue, applicationId); } else { avpDef = Dictionary.getInstance().getAvpDefByCode(Integer.parseInt(attributeValue), applicationId); } if (null == avpDef) { // // If the code value is an integer, we don't necessary have to know it in the dictionnary. // However, if it isn't, we have to. // } // // Handle the code attribute // if (null != avpDef) { root.addAttribute("code", Integer.toString(avpDef.get_code())); } // // Handle the type attribute // if (null == root.attribute("type") && null != avpDef) { TypeDef typeDef = avpDef.get_type(); if (null != typeDef) { while (null != typeDef.get_type_parent()) { if (typeDef.get_type_name().equalsIgnoreCase("AppId")) isTypeAppId = true; if (typeDef.get_type_name().equalsIgnoreCase("VendorId")) isTypeVendorId = true; typeDef = typeDef.get_type_parent(); } root.addAttribute("type", typeDef.get_type_name()); } } // // Handle the vendorId attribute // if (null == root.attribute("vendorId") && null != avpDef) { VendorDef vendorDef = avpDef.get_vendor_id(); if (null != vendorDef) { root.addAttribute("vendorId", Integer.toString(vendorDef.get_code())); } } // // Handle the mandatory attribute // if (null == root.attribute("mandatory")) { if (null != avpDef && null != avpDef.get_mandatory() && avpDef.get_mandatory().equals("mustnot")) { root.addAttribute("mandatory", "false"); } else { root.addAttribute("mandatory", "true"); } } // // Handle the private attribute // if (null == root.attribute("private") && null != avpDef) { if (null != avpDef && null != avpDef.get_protected() && avpDef.get_protected().equals("mustnot")) { root.addAttribute("private", "false"); } else { root.addAttribute("private", "true"); } } // // Parse the enumerated value that could be present in "value" // if (null != root.attribute("value") && null != avpDef) { String enumName = root.attributeValue("value"); long enumValue = avpDef.getEnumCodeByName(enumName); if (enumValue != -1) { root.attribute("value").setValue(Long.toString(enumValue)); } } } else { throw new ParsingException( "in element: " + unmodifiedRoot + "\n" + "code is a mandatory attribute"); } // // Set the vendorId code (in case it isn't referenced by the avp Code via dictionnary, or overwritten). // attributeValue = root.attributeValue("vendorId"); if (null != attributeValue) { if (!Utils.isInteger(attributeValue)) { VendorDef vendorDef = Dictionary.getInstance().getVendorDefByName(attributeValue, applicationId); if (null != vendorDef) { root.attribute("vendorId").setValue(Integer.toString(vendorDef.get_code())); } else { throw new ParsingException("in element: " + unmodifiedRoot + "\n" + attributeValue + " is not a valid vendor id in element"); } } } // // Set the top-parent type (in case it isn't referenced by the avp Code via dictionnary, or overwritten). // if (root.elements().size() > 0) { root.addAttribute("type", "grouped"); } attributeValue = root.attributeValue("type"); if (null != attributeValue) { if (!attributeValue.equalsIgnoreCase("grouped")) { if (null != attributeValue) { TypeDef typeDef = Dictionary.getInstance().getTypeDefByName(attributeValue, applicationId); if (null != typeDef) { while (null != typeDef && null != typeDef.get_type_parent()) { if (typeDef.get_type_name().equalsIgnoreCase("AppId")) isTypeAppId = true; if (typeDef.get_type_name().equalsIgnoreCase("VendorId")) isTypeVendorId = true; typeDef = typeDef.get_type_parent(); } root.attribute("type").setValue(typeDef.get_type_name()); } else { throw new ParsingException("In element: " + unmodifiedRoot + "\n" + attributeValue + " is not a valid type"); } } } } // // Handle the value in case it is an appId or vendorId avp, enum should have already been handled at this point // attributeValue = root.attributeValue("value"); if (null != attributeValue) { if (isTypeAppId) { Application anApplication = Dictionary.getInstance().getApplication(attributeValue); if (null != anApplication) { root.attribute("value").setValue(Integer.toString(anApplication.get_id())); } } if (isTypeVendorId) { VendorDef vendorDef = Dictionary.getInstance().getVendorDefByName(attributeValue, applicationId); if (null != vendorDef) { root.attribute("value").setValue(Integer.toString(vendorDef.get_code())); } } } else { if (!root.attributeValue("type").equalsIgnoreCase("grouped")) { throw new ParsingException("in element: " + unmodifiedRoot + "\n" + "value is a mandatory attribute for element <avp .../> if it is not a grouped avp"); } } } if (recurse) { List<Element> list = root.elements(); for (Element element : list) { doDictionnary(element, applicationId, recurse); } } }
From source file:com.devoteam.srit.xmlloader.genscript.ScriptGenerator.java
License:Open Source License
public void generateTest() throws Exception { // Si le fichier n'existe pas dj if (!fileRoot.exists()) { test = new Test("Genscript", "Script converted from capture"); }/*from w ww . j a v a 2 s. c o m*/ // Si le fichier de sortie existe dj else { // On ouvre le fichier existant File xml = fileRoot; Document testDoc; SAXReader reader = new SAXReader(); testDoc = reader.read(xml); Element testExistant = testDoc.getRootElement(); // On gnre un nouvel objet test partir de l'lment existant dans le fichier test = new Test(testExistant.attributeValue("name"), testExistant.attributeValue("description")); test.setTest(testExistant); } // On tente de rcuprer le testcase gnrer Element testcaseExistant = null; for (Iterator i = test.getTest().elements().iterator(); i.hasNext();) { Element elem = (Element) i.next(); if (elem.getName().equals("testcase") && elem.attribute("name").getText().equals(testcaseName)) { testcaseExistant = elem; } } // On rcupre les paramtres existants for (Iterator i = test.getTest().elements().iterator(); i.hasNext();) { Element elem = (Element) i.next(); if (elem.getName().equals("parameter")) { Param p = new Param(elem.attributeValue("name"), "test", elem.attributeValue("operation") + "," + elem.attributeValue("value"), null, Param.TARGET_SENDCLIENT); p.setName(p.getFamily()); p.setRemplacedValue(elem.attributeValue("value")); ParamGenerator.getInstance().recordParam(p); } } // Si le testcase n'existe pas encore if (testcaseExistant == null) { // On le crait testcase = new TestCase(testcaseName, "Testcase generate from capture", "true"); test.getTest().add(testcase.getTestCase()); } // Sinon on gnre un testcase partir de celui existant dans le fichier else { testcase = new TestCase(testcaseExistant.attributeValue("name"), testcaseExistant.attributeValue("description"), testcaseExistant.attributeValue("state")); testcase.setTestCase(testcaseExistant); // On rcupre les paramtres existants for (Iterator i = testcase.getTestCase().elements().iterator(); i.hasNext();) { Element elem = (Element) i.next(); if (elem.getName().equals("parameter")) { Param p = new Param(elem.attributeValue("name"), "testcase", elem.attributeValue("operation") + "," + elem.attributeValue("value"), null, Param.TARGET_SENDCLIENT); p.setName(p.getFamily()); p.setRemplacedValue(elem.attributeValue("value")); ParamGenerator.getInstance().recordParam(p); } } } // On ajoute le testcase dans le test test.addTestCase(testcase); // On tente de rcuprer le scenario Element scenarioExistant = null; // On enregistre les scenarios de ce testcase for (Iterator j = testcase.getTestCase().elements().iterator(); j.hasNext();) { Element elem = (Element) j.next(); if (elem.getName().equals("scenario") && elem.getText().contains(listeFiltre.get(0).getHostPort().toString())) { scenarioExistant = elem; } else if (elem.getName().equals("scenario")) { Scenario sc = new Scenario(elem.attributeValue("name"), elem.getText(), listeFiltre); testcase.addScenario(sc); } } // Si le scenario n'existe pas encore if (scenarioExistant == null) { // On le crait scenario = new Scenario(getScenarioName(), getScenarioPath(), listeFiltre); testcase.getTestCase().add(scenario.toXmlElement()); } else { scenario = new Scenario(scenarioExistant.attributeValue("name"), scenarioExistant.getText(), listeFiltre); scenario.setScenario(scenarioExistant); } // On ajoute ce scenario au testcase testcase.addScenario(scenario); }
From source file:com.devoteam.srit.xmlloader.gtp.data.HeaderGTPPrime.java
License:Open Source License
@Override public void parseFromXML(Element header, Dictionary dictionary) throws Exception { this.dictionary = dictionary; String strType = header.attributeValue("type"); if (strType != null) { EnumerationField field = (EnumerationField) dictionary.getHeaderFieldByName("Message Type"); this.type = field.getEnumValue(strType); }/*from ww w . j a v a 2s . co m*/ EnumerationField field = (EnumerationField) dictionary.getHeaderFieldByName("Message Type"); this.label = field.getEnumLabelByValue(this.type); String attribute; String attrFlag; attribute = header.attributeValue("sequenceNumber"); if (attribute != null) { this.sequenceNumber = Integer.parseInt(attribute); } attribute = header.attributeValue("version"); if (attribute != null) { this.version = Integer.parseInt(attribute); } attribute = header.attributeValue("spare"); if (attribute != null) { this.spare = Integer.parseInt(attribute); } attribute = header.attributeValue("headerLengthBit"); if (attribute != null) { this.headerLengthBit = Integer.parseInt(attribute); } }
From source file:com.devoteam.srit.xmlloader.gtp.data.HeaderGTPV1.java
License:Open Source License
@Override public void parseFromXML(Element header, Dictionary dictionary) throws Exception { this.dictionary = dictionary; String strType = header.attributeValue("type"); if (strType != null) { EnumerationField field = (EnumerationField) dictionary.getHeaderFieldByName("Message Type"); this.type = field.getEnumValue(strType); }/*ww w . j ava 2 s. c o m*/ EnumerationField field = (EnumerationField) dictionary.getHeaderFieldByName("Message Type"); this.label = field.getEnumLabelByValue(this.type); String attribute; String attrFlag; attribute = header.attributeValue("tunnelEndpointId"); if (attribute != null) { this.tunnelEndpointId = Long.parseLong(attribute); } attrFlag = header.attributeValue("sequenceNumberFlag"); if (attrFlag != null) { this.seqNumFlag = Integer.parseInt(attrFlag); } attribute = header.attributeValue("sequenceNumber"); if (attribute != null) { this.sequenceNumber = Integer.parseInt(attribute); if (attrFlag == null) { this.seqNumFlag = 1; } } else { if (attrFlag == null) { this.seqNumFlag = 0; } } attrFlag = header.attributeValue("nPduNumberFlag"); if (attrFlag != null) { this.nPduFlag = Integer.parseInt(attrFlag); } attribute = header.attributeValue("nPduNumber"); if (attribute != null) { this.nPduNumber = Integer.parseInt(attribute); if (attrFlag == null) { this.nPduFlag = 1; } } else { if (attrFlag == null) { this.nPduFlag = 0; } } attrFlag = header.attributeValue("extensionHeaderFlag"); if (attrFlag != null) { this.extensionFlag = Integer.parseInt(attrFlag); } attribute = header.attributeValue("nextExtensionType"); if (attribute != null) { this.nextExtensionType = Integer.parseInt(attribute); if (attrFlag == null) { this.extensionFlag = 1; } } else { if (attrFlag == null) { this.extensionFlag = 0; } } }
From source file:com.devoteam.srit.xmlloader.gtp.data.HeaderGTPV2.java
License:Open Source License
@Override public void parseFromXML(Element header, Dictionary dictionary) throws Exception { this.dictionary = dictionary; String strType = header.attributeValue("type"); if (strType != null) { EnumerationField field = (EnumerationField) dictionary.getHeaderFieldByName("Message Type"); this.type = field.getEnumValue(strType); }/*ww w.j a v a 2 s . c o m*/ EnumerationField field = (EnumerationField) dictionary.getHeaderFieldByName("Message Type"); this.label = field.getEnumLabelByValue(this.type); String attribute; String attrFlag; attribute = header.attributeValue("piggyFlag"); if (attribute != null) { this.piggyFlag = Integer.parseInt(attribute); } attrFlag = header.attributeValue("teidFlag"); if (attrFlag != null) { this.teidFlag = Integer.parseInt(attrFlag); } attribute = header.attributeValue("tunnelEndpointId"); if (attribute != null) { this.tunnelEndpointId = Long.parseLong(attribute); if (attrFlag == null) { this.teidFlag = 1; } } else { if (attrFlag == null) { this.teidFlag = 0; } } attribute = header.attributeValue("sequenceNumber"); if (attribute != null) { this.sequenceNumber = Integer.parseInt(attribute); } attribute = header.attributeValue("spare"); if (attribute != null) { this.spare = Integer.parseInt(attribute); } }
From source file:com.devoteam.srit.xmlloader.gtp.StackGtp.java
License:Open Source License
/** Creates a Channel specific to each Stack */ @Override//from w w w . j ava2s . c om public Channel parseChannelFromXml(Element root, String protocol) throws Exception { String name = root.attributeValue("name"); String localHost = root.attributeValue("localHost"); String localPort = root.attributeValue("localPort"); String remoteHost = root.attributeValue("remoteHost"); String remotePort = root.attributeValue("remotePort"); if (existsChannel(name)) { return getChannel(name); } else { if (null != localHost) localHost = InetAddress.getByName(localHost).getHostAddress(); else localHost = "0.0.0.0"; if (null != remoteHost) remoteHost = InetAddress.getByName(remoteHost).getHostAddress(); return new ChannelGtp(name, localHost, localPort, remoteHost, remotePort, protocol); } }
From source file:com.devoteam.srit.xmlloader.gtppr.data.GtpHeaderPrime.java
License:Open Source License
public void parseXml(Element header, GtppDictionary dictionary) throws Exception { String msgName = header.attributeValue("name"); String msgType = header.attributeValue("type"); if ((msgType != null) && (msgName != null)) throw new Exception("type and name of the message " + msgName + " must not be set both"); if ((msgType == null) && (msgName == null)) throw new Exception("One of the parameter type or name of the message header must be set"); if (msgName != null) { this.name = msgName; this.messageType = dictionary.getMessageTypeFromName(msgName); } else if (msgType != null) { this.messageType = Integer.parseInt(msgType); this.name = dictionary.getMessageNameFromType(this.messageType); }//from w ww .j a v a 2 s . c o m String msgSeqNum = header.attributeValue("sequenceNumber"); sequenceNumber = Integer.parseInt(msgSeqNum); }
From source file:com.devoteam.srit.xmlloader.gtppr.data.GtpHeaderV1.java
License:Open Source License
public void parseXml(Element header, GtppDictionary dictionary) throws Exception { String msgName = header.attributeValue("name"); String msgType = header.attributeValue("type"); if ((msgType != null) && (msgName != null)) throw new Exception("type and name of the message " + msgName + " must not be set both"); if ((msgType == null) && (msgName == null)) throw new Exception("One of the parameter type or name of the message header must be set"); if (msgName != null) { this.name = msgName; this.messageType = dictionary.getMessageTypeFromName(msgName); } else if (msgType != null) { this.messageType = Integer.parseInt(msgType); this.name = dictionary.getMessageNameFromType(this.messageType); }/*from w w w . ja v a 2 s . c o m*/ String msgSeqNum = header.attributeValue("sequenceNumber"); if (msgSeqNum != null) { this.sequenceNumberFlag = 1; sequenceNumber = Integer.parseInt(msgSeqNum); } String nPduNum = header.attributeValue("nPduNumber"); if (nPduNum != null) { this.nPduNumberFlag = 1; nPduNumber = Integer.parseInt(nPduNum); } String extNum = header.attributeValue("nextExtensionType"); if (extNum != null) { this.extensionHeaderFlag = 1; nextExtensionType = Integer.parseInt(extNum); } }