List of usage examples for org.dom4j Element attribute
Attribute attribute(QName qName);
From source file:com.cladonia.xml.XMLUtilities.java
License:Open Source License
/** * Sets the Schema Location attribute on the root element * * @param document The Exchanger document * @param schemaType The schema type (either schemaLocation or noNamespaceSchemaLocation) * @param namespace The namespace/*w ww.j a v a 2 s . c om*/ * @param schemaURL The URL or the schema * */ public static void setSchemaLocation(ExchangerDocument document, String schemaType, String namespace, String schemaURL) { schemaURL = URLUtilities.encodeURL(schemaURL); XDocument xdoc = document.getDocument(); Element root = xdoc.getRootElement(); if (schemaType.equals(SCHEMALOCATION)) { Attribute attrNoNS = root.attribute(NOSCHEMALOCATION); if (attrNoNS != null) { root.remove(attrNoNS); } // need to set both namspace and url Attribute attr = root.attribute(SCHEMALOCATION); if (attr == null) { // does the schema instance already exist Namespace ns = root.getNamespaceForURI(SCHEMAINSTANCE); if (ns != null) { String schemaInstancePrefix = ns.getPrefix(); StringBuffer name = new StringBuffer(); name.append(schemaInstancePrefix); name.append(":"); name.append(SCHEMALOCATION); StringBuffer value = new StringBuffer(); value.append(namespace); value.append(" "); value.append(schemaURL); root.addAttribute(name.toString(), value.toString()); } else { root.addNamespace("xsi", SCHEMAINSTANCE); StringBuffer name = new StringBuffer(); name.append("xsi"); name.append(":"); name.append(SCHEMALOCATION); StringBuffer value = new StringBuffer(); value.append(namespace); value.append(" "); value.append(schemaURL); root.addAttribute(name.toString(), value.toString()); } } else { String attrValue = attr.getValue(); // break up all the namespace and url pairs ArrayList stringValues = new ArrayList(); StringTokenizer st = new StringTokenizer(attrValue); while (st.hasMoreTokens()) { stringValues.add(st.nextToken()); } // update existing attribute, Note: it may have multiple attribute pairs StringBuffer value = new StringBuffer(); value.append(namespace); value.append(" "); value.append(schemaURL); //need to start at the third value (i.e we only replace the first namespace-url pair) for (int i = 2; i < stringValues.size(); i++) { value.append(" "); value.append((String) stringValues.get(i)); } attr.setValue(value.toString()); } } else { // is of type "no schema location" Attribute attrSchema = root.attribute(SCHEMALOCATION); if (attrSchema != null) { root.remove(attrSchema); } // just need to set the url Attribute attr = root.attribute(NOSCHEMALOCATION); if (attr == null) { // does the schema instance already exist Namespace ns = root.getNamespaceForURI(SCHEMAINSTANCE); if (ns != null) { String schemaInstancePrefix = ns.getPrefix(); StringBuffer name = new StringBuffer(); name.append(schemaInstancePrefix); name.append(":"); name.append(NOSCHEMALOCATION); root.addAttribute(name.toString(), schemaURL); } else { root.addNamespace("xsi", SCHEMAINSTANCE); StringBuffer name = new StringBuffer(); name.append("xsi"); name.append(":"); name.append(NOSCHEMALOCATION); root.addAttribute(name.toString(), schemaURL); } } else { // update existing attribute attr.setValue(schemaURL); } } }
From source file:com.cladonia.xngreditor.SchemaLocationDialog.java
License:Open Source License
/** * Sets the original values displayed by the dialog, plus what should be enabled\disabled *///from w w w .j a v a 2 s .com private void setCurrentValues() { XDocument xdoc = document.getDocument(); Element ele = xdoc.getRootElement(); Attribute attr = ele.attribute(SCHEMALOCATION); if (attr == null) { // check for no namespace schema Attribute attr2 = ele.attribute(NOSCHEMALOCATION); if (attr2 == null) { // set the defaults schemaButton.setSelected(true); Namespace ns = ele.getNamespace(); if (ns != Namespace.NO_NAMESPACE) { namespaceField.setText(ns.getURI()); } else { namespaceField.setText(""); } urlField.setText(""); } else { // we have a "no namespace schema" schemaNoNSButton.setSelected(true); namespaceField.setText(""); namespaceField.setEnabled(false); namespaceLabel.setEnabled(false); urlField.setText(attr2.getValue()); } } else { // we hava a namepace schema schemaButton.setSelected(true); // we have namespace schema, set the namespace and the URL String attrValue = attr.getValue(); // break up all the namespace and url pairs ArrayList stringValues = new ArrayList(); StringTokenizer st = new StringTokenizer(attrValue); while (st.hasMoreTokens()) { stringValues.add(st.nextToken()); } String namespaceValue = (String) stringValues.get(0); if (namespaceValue != null) { namespaceField.setText(namespaceValue); } else { namespaceField.setText(""); } String urlValue = (String) stringValues.get(1); if (urlValue != null) { urlField.setText(urlValue); } else { urlField.setText(""); } } }
From source file:com.core.util.wx.XmlUtils.java
License:Apache License
/** * @param ??xml//from w w w . ja v a 2 s . c o m * @return * @throws UnsupportedEncodingException * @throws DocumentException * ???xml??? */ public static Map<String, String> sfxmlToMap(String xml) throws UnsupportedEncodingException, DocumentException { LOG.info("xml:" + xml); Map<String, String> map = new HashMap<String, String>(); Document doc = null; try { doc = DocumentHelper.parseText(xml); } catch (DocumentException e) { LOG.error(e.getMessage(), e); } if (null == doc) return map; Element root = doc.getRootElement(); for (Iterator iterator = root.elementIterator(); iterator.hasNext();) { Element e = (Element) iterator.next(); if (e.getName().equals("Head")) { if (e.getText().equals("ERR")) { return null; } } if (e.getName().equals("Body")) { Element e1 = e.element("OrderResponse"); if (e1 != null) { if (e1.attribute("mailno") != null) { map.put("mailno", e1.attribute("mailno").getText()); } if (e1.attribute("destcode") != null) { map.put("destcode", e1.attribute("destcode").getText()); } if (e1.attribute("filter_result") != null) { map.put("filter_result", e1.attribute("filter_result").getText()); } } Element e2 = e.element("OrderZDResponse"); if (e2 != null) { Element s = e2.element("OrderZDResponse"); if (s.attribute("mailno_zd") != null) { map.put("mailno_zd", s.attribute("mailno_zd").getText()); } } break; } } return map; }
From source file:com.cy.dcts.identityVerify.service.impl.IdentityVerifyServiceImpl.java
private Map<String, String> paresXml2Map(String xml) { Map<String, String> map = new HashMap<String, String>(); Document document = null;/*from w w w.j av a2 s .com*/ String errormesage = ""; String errormesagecol = ""; String idNumberRst = ""; String xmRst = ""; String errorMsg = ""; String errorCode = ""; try { document = DocumentHelper.parseText(xml); Element rootElement = document.getRootElement();//? Attribute rootAttr = rootElement.attribute("errorcode"); if (rootAttr != null) { Node errorCodeNode = rootElement.selectSingleNode("//RESPONSE/ROWS/ROW/ErrorCode"); Node errorMsgNode = rootElement.selectSingleNode("//RESPONSE/ROWS/ROW/ErrorMsg"); if (errorCodeNode != null && errorMsgNode != null) { errorMsg = errorMsgNode.getText(); errorCode = errorCodeNode.getText(); } map.put("errorCode", errorCode); map.put("errorMsg", errorMsg); return map; } Node idNumberNode = rootElement.selectSingleNode("//ROWS/ROW/INPUT/gmsfhm"); String idNumber = idNumberNode.getText(); String name = rootElement.selectSingleNode("//ROWS/ROW/INPUT/xm").getText(); map.put("gmsfhm", idNumber); map.put("xm", name); Node sfNode = rootElement.selectSingleNode("//ROWS/ROW/OUTPUT/ITEM/result_gmsfhm"); Node xmNode = rootElement.selectSingleNode("//ROWS/ROW/OUTPUT/ITEM/result_xm"); if (sfNode == null || xmNode == null) { Node errorMsgNode = rootElement.selectSingleNode("//ROWS/ROW/OUTPUT/ITEM/errormesage"); Node errorColNode = rootElement.selectSingleNode("//ROWS/ROW/OUTPUT/ITEM/errormesagecol"); if (errorMsgNode != null && errorColNode != null) { errormesage = errorMsgNode.getText(); errormesagecol = errorColNode.getText(); } } else { idNumberRst = sfNode.getText(); xmRst = xmNode.getText(); } map.put("result_gmsfhm", idNumberRst); map.put("result_xm", xmRst); map.put("errormesage", errormesage); map.put("errormesagecol", errormesagecol); } catch (DocumentException e) { e.printStackTrace(); } return map; }
From source file:com.devoteam.srit.xmlloader.diameter.dictionary.Application.java
License:Open Source License
private void parseVendor(Element root) throws ParsingException { int code = -1; try {//from w ww .j a va 2 s . c om if (null != root.attribute("code")) { code = Integer.parseInt(root.attributeValue("code")); } else { Dictionary.traceWarning("No vendor.code, skipping"); return; } } catch (Exception e) { Dictionary.traceWarning("Invalid vendor.code, skipping"); return; } String vendor_id = null; if (null != root.attributeValue("vendor-id")) vendor_id = root.attributeValue("vendor-id"); else { Dictionary.traceWarning("No vendor.vendor-id, skipping"); return; } String name = null; if (null != root.attributeValue("name")) name = root.attributeValue("name"); VendorDef vendorDef = new VendorDef(code, vendor_id, name); if (null != getVendorDefByCode(code)) Dictionary.traceWarning("VendorDef of code " + code + " already exists, overwriting"); if (null != getVendorDefByName(vendor_id)) Dictionary.traceWarning("VendorDef of vendor-id " + vendor_id + " already exists, overwriting"); vendorDefByName.put(vendor_id, vendorDef); vendorDefByCode.put(Integer.toString(code), vendorDef); }
From source file:com.devoteam.srit.xmlloader.diameter.dictionary.Application.java
License:Open Source License
private void parseAvp(Element root) throws ParsingException { String name = null;/*from ww w. j av a2 s . co m*/ String description = null; String may_encrypt = null; String mandatory = null; String protected_ = null; String vendor_bit = null; if (null != root.attributeValue("name")) name = root.attributeValue("name"); else { Dictionary.traceWarning("Invalid avp.name, skipping"); return; } if (null != root.attributeValue("description")) description = root.attributeValue("description"); if (null != root.attributeValue("may-encrypt")) may_encrypt = root.attributeValue("may-encrypt"); if (null != root.attributeValue("mandatory")) mandatory = root.attributeValue("mandatory"); if (null != root.attributeValue("protected")) protected_ = root.attributeValue("protected"); if (null != root.attributeValue("vendor-bit")) vendor_bit = root.attributeValue("vendor-bit"); int code; try { code = Integer.parseInt(root.attributeValue("code")); } catch (Exception e) { code = -1; } if (code == -1) { Dictionary.traceWarning("Missing avp.code, skipping"); return; } boolean constrained = false; if (null != root.attributeValue("constrained")) constrained = Boolean.parseBoolean(root.attributeValue("constrained")); VendorDef vendor_id = null; if (null != root.attributeValue("vendor-id")) vendor_id = Dictionary.getInstance().getVendorDefByName(root.attributeValue("vendor-id"), _name); TypeDef type = null; { List<Element> elements; elements = root.elements("type"); for (Element element : elements) { String type_name = element.attributeValue("type-name"); type = Dictionary.getInstance().getTypeDefByName(type_name, _name); if (type == null) { Dictionary.traceWarning("Invalid avp.type (" + type_name + "), skipping"); } } } AvpDef avpDef = new AvpDef(name, code, description, may_encrypt, protected_, vendor_bit, mandatory, constrained, type, vendor_id); // parse enums { List<Element> elements; elements = root.elements("enum"); for (Element element : elements) { String enum_name = null; if (null != element.attribute("name")) { enum_name = element.attributeValue("name"); } else { Dictionary.traceWarning("No avp.enum.name in " + avpDef.get_name() + ", skipping this enum"); continue; } int enum_code = -1; if (null != element.attribute("code")) { try { enum_code = Integer.parseInt(element.attributeValue("code")); } catch (Exception e) { Dictionary.traceWarning( "Invalid avp.enum.code in " + avpDef.get_name() + ", skipping this enum"); continue; } } else { Dictionary.traceWarning("No avp.enum.code in " + avpDef.get_name() + ", skipping this enum"); continue; } avpDef.addEnum(enum_name, enum_code); } } // parse grouped { Element elementGrouped = root.element("grouped"); if (null != elementGrouped) { List<Element> elements; elements = elementGrouped.elements("gavp"); for (Element element : elements) { String gavp_name = element.attributeValue("name"); avpDef.addGroupedAvpName(gavp_name); } } } if (null != getAvpDefByCode(avpDef.get_code())) Dictionary.traceWarning("AvpDef of code " + avpDef.get_code() + " already exists, overwriting"); if (null != getAvpDefByName(avpDef.get_name())) Dictionary.traceWarning("AvpDef of name " + avpDef.get_name() + " already exists, overwriting"); avpDefByName.put(avpDef.get_name(), avpDef); avpDefByCode.put(Integer.toString(avpDef.get_code()), avpDef); }
From source file:com.devoteam.srit.xmlloader.diameter.dictionary.Application.java
License:Open Source License
private void parseCommand(Element root) throws ParsingException { int code = -1; try {//from w w w . j av a 2 s . co m if (null != root.attribute("code")) code = Integer.parseInt(root.attributeValue("code")); else { Dictionary.traceWarning("No commands.code, skipping"); return; } } catch (Exception e) { Dictionary.traceWarning("Invalid command.code, skipping"); return; } VendorDef vendor_id = null; if (null != root.attributeValue("vendor-id")) Dictionary.getInstance().getVendorDefByName(root.attributeValue("vendor-id"), _name); String name = null; if (null != root.attributeValue("name")) name = root.attributeValue("name"); else { Dictionary.traceWarning("Invalid command.name, skipping"); return; } CommandDef commandDef = new CommandDef(code, name, vendor_id); if (null != getCommandDefByCode(code)) Dictionary.traceWarning("CommandDef of code " + code + " already exists, overwriting"); if (null != getCommandDefByName(name)) Dictionary.traceWarning("CommandDef of name " + name + " already exists, overwriting"); commandDefByName.put(name, commandDef); commandDefByCode.put(Integer.toString(code), commandDef); }
From source file:com.devoteam.srit.xmlloader.diameter.dictionary.Dictionary.java
License:Open Source License
private void parseApplication(Element root) throws ParsingException { boolean isBase = false; if (root.getName().equals("base")) { isBase = true;// w ww . j a v a 2 s . c o m } int id = -1; try { if (null != root.attribute("id")) { id = Integer.parseInt(root.attributeValue("id")); } else if (false == isBase) { traceWarning("No application.id, skipping"); return; } } catch (Exception e) { traceWarning("Invalid application.code, skipping"); return; } String name = null; if (null != root.attribute("name")) { name = root.attributeValue("name"); } else if (false == isBase) { traceWarning("No application.name, skipping"); return; } if (isBase) { name = "0"; id = 0; } traceDebug("parsing application " + name); Application application = getApplicationById(id); if (application == null) { application = new Application(name, id); } applicationByName.put(name, application); applicationById.put(Integer.toString(id), application); application.parseApplication(root); application.fillGroupedAvpsReferences(); }
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); }//www. j a va2 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.Param.java
License:Open Source License
public String applySubstitution(String text, Msg msg) throws Exception { String msgAvecParametres = ""; if (getRegle() != null) { String[] regexTab = getRegle().split("#"); // Si la regle est sous forme d'expression rgulire if (regexTab[0].toUpperCase().contains("REGEX")) { GlobalLogger.instance().getApplicationLogger().debug(TextEvent.Topic.CORE, "Replace parameter " + getRegle() + " => " + name); String[] regexRule = Arrays.copyOfRange(regexTab, 1, regexTab.length); msgAvecParametres = regexRemplace(regexRule, 0, text); }/*from w ww. ja v a 2 s. co m*/ // Si la regle est sous forme de xpath else if (regexTab[0].toUpperCase().contains("XPATH")) { GlobalLogger.instance().getApplicationLogger().debug(TextEvent.Topic.CORE, "Replace parameter " + getRegle() + " => " + name); // Rcupration des paramtres String xpathRule = regexTab[1]; String attribut = regexTab[2]; attribut = attribut.replace("@", ""); // Cration de l'arbre DOM correspondant au message SAXReader reader = new SAXReader(); try { Document document = reader.read(new ByteArrayInputStream(text.getBytes("UTF-8"))); // Cration de l'objet XPATH ) selectionner XPath xpath = new DefaultXPath(xpathRule); // Rcupration des noeuds correspondants List<Node> nodes = xpath.selectNodes(document.getRootElement(), xpath); // Pour chaque noeuds modifier Element aRemplacer = null; for (Node n : nodes) { setRemplacedValue(n.getText()); if (n instanceof Element) { aRemplacer = (Element) n; } else { aRemplacer = n.getParent(); } String newValue = getName(); String oldValue = aRemplacer.attribute(attribut).getValue(); // On regarde si on est dans le cas de paramtres mixtes if (regexTab.length > 3) { if (regexTab[3].equals("REGEX")) { setRemplacedValue(null); String[] regexRule = Arrays.copyOfRange(regexTab, 4, regexTab.length); newValue = regexRemplace(regexRule, 0, oldValue); } } aRemplacer.setAttributeValue(attribut, newValue); } // Convertion en chane de caractre de l'arbre DOM du message msgAvecParametres = document.getRootElement().asXML(); } catch (Exception e) { } } // si la rgle est sous forme de pathkey else if (regexTab[0].toUpperCase().contains("PATHKEY")) { String valeurARemplacer = null; // On rcupre la valeur remplacer String pathKeyWord = regexTab[1]; if (msg != null) { Parameter valeurParamARemplacer = msg.getParameter(pathKeyWord); if (valeurParamARemplacer.length() > 0) { valeurARemplacer = valeurParamARemplacer.get(0).toString(); } // On remplace dans tout le message par le parametre if (valeurARemplacer != null) { msgAvecParametres = text.replace(valeurARemplacer, getName()); if (!msgAvecParametres.equals(text)) { GlobalLogger.instance().getApplicationLogger().debug(TextEvent.Topic.CORE, "Replace parameter " + valeurARemplacer + " => " + name); setRemplacedValue(valeurARemplacer); } } } } } // Si le message n'a pas subit de modification, on retourne null if (!isUsed()) { return null; } // Sinon on retourne le message modifi avec les paramtres return msgAvecParametres; }