List of usage examples for org.w3c.dom Element getTextContent
public String getTextContent() throws DOMException;
From source file:com.evolveum.midpoint.model.client.ModelClientUtil.java
public static String getOrig(PolyStringType polyStringType) { if (polyStringType == null) { return null; }/* w w w .jav a 2s .c o m*/ StringBuilder sb = new StringBuilder(); for (Object o : polyStringType.getContent()) { if (o instanceof String) { sb.append(o); } else if (o instanceof Element) { Element e = (Element) o; if ("orig".equals(e.getLocalName())) { return e.getTextContent(); } } else if (o instanceof JAXBElement) { JAXBElement je = (JAXBElement) o; if ("orig".equals(je.getName().getLocalPart())) { return (String) je.getValue(); } } } return sb.toString(); }
From source file:Main.java
/** * // w ww . j a v a 2s. c om * @param currentNode * @param tagName * @param attributeValue * @return */ public static String getTextContentByElementNameANDAttributeValue(Node currentNode, String tagName, String attributeValue) { String result = ""; NodeList childNodeList = currentNode.getChildNodes(); for (int i = 0; i < childNodeList.getLength(); i++) { Node childNode = childNodeList.item(i); switch (childNode.getNodeType()) { case Node.DOCUMENT_NODE: break; case Node.ELEMENT_NODE: Element childElement = (Element) childNodeList.item(i); // logger.debug("childElement name : " + childElement.getTagName()); if (childElement != null && childElement.getNodeName().equals(tagName)) { NamedNodeMap attributes = childElement.getAttributes(); for (int j = 0; j < attributes.getLength(); j++) { Node current = attributes.item(j); if (current.getNodeName().equals("type") && current.getNodeValue().equals(attributeValue)) { result = childElement.getTextContent(); break; } } } case Node.TEXT_NODE: // logger.debug("textElement name : " + currentNode.getNodeValue()); break; case Node.COMMENT_NODE: break; case Node.PROCESSING_INSTRUCTION_NODE: break; case Node.ENTITY_REFERENCE_NODE: break; case Node.DOCUMENT_TYPE_NODE: break; } } return result; }
From source file:com.persistent.cloudninja.controller.AuthFilterUtils.java
/** * Get Certificate thumb print and Issuer Name from the ACS token. * @param acsToken the acs token/* w w w . j a v a2s .c o m*/ * @return returnData the Map containing Thumb print and issuer name of X509Certiificate * @throws NoSuchAlgorithmException * @throws CertificateEncodingException */ public static Map<String, String> getCertificateThumbPrintAndIssuerName(String acsToken) throws NoSuchAlgorithmException, CertificateEncodingException { byte[] acsTokenByteArray = null; Map<String, String> returnData = new HashMap<String, String>(); try { acsTokenByteArray = acsToken.getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { return null; } DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setNamespaceAware(true); DocumentBuilder docBuilder; String issuerName = null; StringBuffer thumbprint = null; try { docBuilder = builderFactory.newDocumentBuilder(); Document resultDoc = docBuilder.parse(new ByteArrayInputStream(acsTokenByteArray)); Element keyInfo = (Element) resultDoc.getDocumentElement() .getElementsByTagNameNS("http://www.w3.org/2000/09/xmldsig#", "KeyInfo").item(0); NodeList x509CertNodeList = keyInfo.getElementsByTagName("X509Certificate"); Element x509CertNode = (Element) x509CertNodeList.item(0); if (x509CertNode == null) { return null; } //generating Certificate to retrieve its detail. String x509CertificateData = x509CertNode.getTextContent(); InputStream inStream = new Base64InputStream(new ByteArrayInputStream(x509CertificateData.getBytes())); CertificateFactory x509CertificateFactory = CertificateFactory.getInstance("X.509"); X509Certificate x509Certificate = (X509Certificate) x509CertificateFactory .generateCertificate(inStream); String issuerDN = x509Certificate.getIssuerDN().toString(); String[] issuerDNData = issuerDN.split("="); issuerName = issuerDNData[1]; MessageDigest md = MessageDigest.getInstance("SHA-1"); byte[] der = x509Certificate.getEncoded(); md.update(der); thumbprint = new StringBuffer(); thumbprint.append(Hex.encodeHex(md.digest())); } catch (Exception e) { e.printStackTrace(); } returnData.put("IssuerName", issuerName); returnData.put("Thumbprint", thumbprint.toString().toUpperCase()); return returnData; }
From source file:cz.incad.kramerius.utils.solr.SolrUtils.java
public static String disectPid(Element topElem) throws XPathExpressionException { synchronized (topElem.getOwnerDocument()) { Element foundElement = XMLUtils.findElement(topElem, new XMLUtils.ElementsFilter() { @Override/*from w ww . j a v a2s . com*/ public boolean acceptElement(Element element) { return (element.getNodeName().equals("str") && element.getAttribute("name") != null && element.getAttribute("name").equals("PID")); } }); if (foundElement != null) { return foundElement.getTextContent().trim(); } else return null; } }
From source file:es.juntadeandalucia.panelGestion.negocio.utiles.PanelSettings.java
private static String getStringNodeValue(String nodeName, Element node) { String value = null;//w ww . j a v a 2 s . c o m NodeList nodeChildren = node.getElementsByTagName(nodeName); for (int i = 0; i < nodeChildren.getLength(); i++) { Node child = nodeChildren.item(i); if (child instanceof Element) { Element childElem = (Element) child; value = childElem.getTextContent(); break; } } return value; }
From source file:net.sourceforge.dita4publishers.word2dita.Word2DitaValidationHelper.java
/** * @param zipComponents /* w w w . j a va2 s . co m*/ * @param logDoc * @param documentDom * @param commentsDom * @param commentTemplate * @throws XPathExpressionException */ static void addMessagesToDocxXml(Document logDoc, Document documentDom, Document commentsDom, Element commentTemplate) throws XPathExpressionException { NodeList messagesNl = logDoc.getDocumentElement().getElementsByTagName("message"); for (int i = 0; i < messagesNl.getLength(); i++) { Element message = (Element) messagesNl.item(i); NodeList existingComments = commentsDom.getDocumentElement().getElementsByTagNameNS(wNs, "comment"); String commentId = String.valueOf(existingComments.getLength()); String messageText = message.getTextContent(); addCommentToComments(commentsDom, commentTemplate, messageText, commentId); String xpath = message.getAttribute("wordParaXPath"); // System.err.println("xpath=" + xpath); if (xpath == null || "".equals(xpath.trim())) { xpath = "/w:document/w:body[1]/w:p[1]"; } addCommentRefToParaForXPath(documentDom, commentId, xpath); } }
From source file:com.bluexml.side.form.utils.DOMUtil.java
/** * Gets the node value by tag name.//from w w w.j a va2 s.co m * * @param node * the node * @param name * the name * @param recursive * the recursive * * @return the node value by tag name */ public static String getNodeValueByTagName(Node node, String name, boolean recursive) { if (node instanceof Document) { return getNodeValueByTagName(((Document) node).getDocumentElement(), name, recursive); } if (node instanceof Element) { Element element = (Element) node; if (StringUtils.equals(element.getTagName(), name)) { return StringUtils.trimToNull(element.getTextContent()); } } NodeList childNodes = node.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node item = childNodes.item(i); if (item instanceof Element) { Element element = (Element) item; if (StringUtils.equals(element.getTagName(), name)) { return StringUtils.trimToNull(element.getTextContent()); } if (recursive) { String nodeValueByTagName = getNodeValueByTagName(item, name, true); if (nodeValueByTagName != null) { return nodeValueByTagName; } } } } return null; }
From source file:es.juntadeandalucia.panelGestion.negocio.utiles.PanelSettings.java
private static void readProjections(Document xmlConfiguration) { projections = new LinkedList<String>(); Element doc = xmlConfiguration.getDocumentElement(); NodeList nodesProjection = doc.getElementsByTagName("projection"); // iterate over the data base types for (int i = 0; i < nodesProjection.getLength(); i++) { // get the data base type node Node projectionNode = nodesProjection.item(i); if (projectionNode instanceof Element) { Element projectionElem = (Element) projectionNode; String projection = projectionElem.getTextContent(); projections.add(projection); }// w w w . j a v a2s.co m } }
From source file:com.fujitsu.dc.common.auth.token.TransCellAccessToken.java
/** * TransCellAccessToken????.// ww w.j a va 2 s.c o m * @param token * @return TransCellAccessToken(?) * @throws AbstractOAuth2Token.TokenParseException ? * @throws AbstractOAuth2Token.TokenDsigException ??? * @throws AbstractOAuth2Token.TokenRootCrtException CA? */ public static TransCellAccessToken parse(final String token) throws AbstractOAuth2Token.TokenParseException, AbstractOAuth2Token.TokenDsigException, AbstractOAuth2Token.TokenRootCrtException { try { byte[] samlBytes = DcCoreUtils.decodeBase64Url(token); ByteArrayInputStream bais = new ByteArrayInputStream(samlBytes); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder builder = null; try { builder = dbf.newDocumentBuilder(); } catch (ParserConfigurationException e) { // ???????????? throw new RuntimeException(e); } Document doc = builder.parse(bais); Element assertion = doc.getDocumentElement(); Element issuer = (Element) (doc.getElementsByTagName("Issuer").item(0)); Element subject = (Element) (assertion.getElementsByTagName("Subject").item(0)); Element subjectNameID = (Element) (subject.getElementsByTagName("NameID").item(0)); String id = assertion.getAttribute("ID"); String issuedAtStr = assertion.getAttribute("IssueInstant"); DateTime dt = new DateTime(issuedAtStr); NodeList audienceList = assertion.getElementsByTagName("Audience"); Element aud1 = (Element) (audienceList.item(0)); String target = aud1.getTextContent(); String schema = null; if (audienceList.getLength() > 1) { Element aud2 = (Element) (audienceList.item(1)); schema = aud2.getTextContent(); } List<Role> roles = new ArrayList<Role>(); NodeList attrList = assertion.getElementsByTagName("AttributeValue"); for (int i = 0; i < attrList.getLength(); i++) { Element attv = (Element) (attrList.item(i)); roles.add(new Role(new URL(attv.getTextContent()))); } NodeList nl = assertion.getElementsByTagName("Signature"); if (nl.getLength() == 0) { throw new TokenParseException("Cannot find Signature element"); } Element signatureElement = (Element) nl.item(0); // ???????TokenDsigException?? // Create a DOMValidateContext and specify a KeySelector // and document context. X509KeySelector x509KeySelector = new X509KeySelector(issuer.getTextContent()); DOMValidateContext valContext = new DOMValidateContext(x509KeySelector, signatureElement); // Unmarshal the XMLSignature. XMLSignature signature; try { signature = xmlSignatureFactory.unmarshalXMLSignature(valContext); } catch (MarshalException e) { throw new TokenDsigException(e.getMessage(), e); } // CA?? try { x509KeySelector.readRoot(x509RootCertificateFileNames); } catch (CertificateException e) { // CA????????500 throw new TokenRootCrtException(e.getMessage(), e); } // Validate the XMLSignature x509. boolean coreValidity; try { coreValidity = signature.validate(valContext); } catch (XMLSignatureException e) { if (e.getCause().getClass() == new KeySelectorException().getClass()) { throw new TokenDsigException(e.getCause().getMessage(), e.getCause()); } throw new TokenDsigException(e.getMessage(), e); } // http://www.w3.org/TR/xmldsig-core/#sec-CoreValidation // Check core validation status. if (!coreValidity) { // ?? boolean isDsigValid; try { isDsigValid = signature.getSignatureValue().validate(valContext); } catch (XMLSignatureException e) { throw new TokenDsigException(e.getMessage(), e); } if (!isDsigValid) { throw new TokenDsigException("Failed signature validation"); } // Iterator i = signature.getSignedInfo().getReferences().iterator(); for (int j = 0; i.hasNext(); j++) { boolean refValid; try { refValid = ((Reference) i.next()).validate(valContext); } catch (XMLSignatureException e) { throw new TokenDsigException(e.getMessage(), e); } if (!refValid) { throw new TokenDsigException("Failed to validate reference [" + j + "]"); } } throw new TokenDsigException("Signature failed core validation. unkwnon reason."); } return new TransCellAccessToken(id, dt.getMillis(), issuer.getTextContent(), subjectNameID.getTextContent(), target, roles, schema); } catch (UnsupportedEncodingException e) { throw new TokenParseException(e.getMessage(), e); } catch (SAXException e) { throw new TokenParseException(e.getMessage(), e); } catch (IOException e) { throw new TokenParseException(e.getMessage(), e); } }
From source file:io.personium.common.auth.token.TransCellAccessToken.java
/** * TransCellAccessToken????./* w ww.ja va2 s . c o m*/ * @param token * @return TransCellAccessToken(?) * @throws AbstractOAuth2Token.TokenParseException ? * @throws AbstractOAuth2Token.TokenDsigException ??? * @throws AbstractOAuth2Token.TokenRootCrtException CA? */ public static TransCellAccessToken parse(final String token) throws AbstractOAuth2Token.TokenParseException, AbstractOAuth2Token.TokenDsigException, AbstractOAuth2Token.TokenRootCrtException { try { byte[] samlBytes = PersoniumCoreUtils.decodeBase64Url(token); ByteArrayInputStream bais = new ByteArrayInputStream(samlBytes); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder builder = null; try { builder = dbf.newDocumentBuilder(); } catch (ParserConfigurationException e) { // ???????????? throw new RuntimeException(e); } Document doc = builder.parse(bais); Element assertion = doc.getDocumentElement(); Element issuer = (Element) (doc.getElementsByTagName("Issuer").item(0)); Element subject = (Element) (assertion.getElementsByTagName("Subject").item(0)); Element subjectNameID = (Element) (subject.getElementsByTagName("NameID").item(0)); String id = assertion.getAttribute("ID"); String issuedAtStr = assertion.getAttribute("IssueInstant"); DateTime dt = new DateTime(issuedAtStr); NodeList audienceList = assertion.getElementsByTagName("Audience"); Element aud1 = (Element) (audienceList.item(0)); String target = aud1.getTextContent(); String schema = null; if (audienceList.getLength() > 1) { Element aud2 = (Element) (audienceList.item(1)); schema = aud2.getTextContent(); } List<Role> roles = new ArrayList<Role>(); NodeList attrList = assertion.getElementsByTagName("AttributeValue"); for (int i = 0; i < attrList.getLength(); i++) { Element attv = (Element) (attrList.item(i)); roles.add(new Role(new URL(attv.getTextContent()))); } NodeList nl = assertion.getElementsByTagName("Signature"); if (nl.getLength() == 0) { throw new TokenParseException("Cannot find Signature element"); } Element signatureElement = (Element) nl.item(0); // ???????TokenDsigException?? // Create a DOMValidateContext and specify a KeySelector // and document context. X509KeySelector x509KeySelector = new X509KeySelector(issuer.getTextContent()); DOMValidateContext valContext = new DOMValidateContext(x509KeySelector, signatureElement); // Unmarshal the XMLSignature. XMLSignature signature; try { signature = xmlSignatureFactory.unmarshalXMLSignature(valContext); } catch (MarshalException e) { throw new TokenDsigException(e.getMessage(), e); } // CA?? try { x509KeySelector.readRoot(x509RootCertificateFileNames); } catch (CertificateException e) { // CA????????500 throw new TokenRootCrtException(e.getMessage(), e); } // Validate the XMLSignature x509. boolean coreValidity; try { coreValidity = signature.validate(valContext); } catch (XMLSignatureException e) { if (e.getCause().getClass() == new KeySelectorException().getClass()) { throw new TokenDsigException(e.getCause().getMessage(), e.getCause()); } throw new TokenDsigException(e.getMessage(), e); } // http://www.w3.org/TR/xmldsig-core/#sec-CoreValidation // Check core validation status. if (!coreValidity) { // ?? boolean isDsigValid; try { isDsigValid = signature.getSignatureValue().validate(valContext); } catch (XMLSignatureException e) { throw new TokenDsigException(e.getMessage(), e); } if (!isDsigValid) { throw new TokenDsigException("Failed signature validation"); } // Iterator i = signature.getSignedInfo().getReferences().iterator(); for (int j = 0; i.hasNext(); j++) { boolean refValid; try { refValid = ((Reference) i.next()).validate(valContext); } catch (XMLSignatureException e) { throw new TokenDsigException(e.getMessage(), e); } if (!refValid) { throw new TokenDsigException("Failed to validate reference [" + j + "]"); } } throw new TokenDsigException("Signature failed core validation. unkwnon reason."); } return new TransCellAccessToken(id, dt.getMillis(), issuer.getTextContent(), subjectNameID.getTextContent(), target, roles, schema); } catch (UnsupportedEncodingException e) { throw new TokenParseException(e.getMessage(), e); } catch (SAXException e) { throw new TokenParseException(e.getMessage(), e); } catch (IOException e) { throw new TokenParseException(e.getMessage(), e); } }