List of usage examples for org.w3c.dom Document appendChild
public Node appendChild(Node newChild) throws DOMException;
newChild
to the end of the list of children of this node. From source file:net.solarnetwork.support.XmlSupport.java
/** * Turn an object into a simple XML Document, supporting custom property * editors./*w w w . j a v a 2s. c o m*/ * * <p> * The returned XML will be a document with a single element with all * JavaBean properties turned into attributes. For example: * <p> * * <pre> * <powerDatum * id="123" * pvVolts="123.123" * ... /> * </pre> * * <p> * {@link PropertyEditor} instances can be registered with the supplied * {@link BeanWrapper} for custom handling of properties, e.g. dates. * </p> * * @param bean * the object to turn into XML * @param elementName * the name of the XML element * @return the element, as an XML DOM Document */ public Document getDocument(BeanWrapper bean, String elementName) { Document dom = null; try { dom = getDocBuilderFactory().newDocumentBuilder().newDocument(); dom.appendChild(getElement(bean, elementName, dom)); } catch (ParserConfigurationException e) { throw new RuntimeException(e); } return dom; }
From source file:es.itecban.deployment.security.client.ws.LogonWS.java
private Element getRequestElement(String operationName) { // Create the DOM document Document doc = documentBuilder.newDocument(); doc.setDocumentURI(NAMESPACE_URI); // Create the root element Element requestElement = doc.createElementNS(NAMESPACE_URI, operationName); // Element requestElement = doc.createElement(operationName); doc.appendChild(requestElement); // Return it/*ww w .ja va 2 s . c om*/ return requestElement; }
From source file:io.cos.cas.authentication.handler.support.OpenScienceFrameworkPrincipalFromRequestRemoteUserNonInteractiveCredentialsAction.java
/** * Normalize the Remote Principal credential. * * @param credential the credential object bearing the username, password, etc... * @return the json object to serialize for authorization with the OSF API * @throws ParserConfigurationException a parser configuration exception * @throws TransformerException a transformer exception *//*w w w . j a va2 s .c o m*/ private JSONObject normalizeRemotePrincipal(final OpenScienceFrameworkCredential credential) throws ParserConfigurationException, TransformerException { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final DocumentBuilder builder = factory.newDocumentBuilder(); final Document document = builder.newDocument(); final Element rootElement = document.createElement("auth"); document.appendChild(rootElement); for (final String key : credential.getAuthenticationHeaders().keySet()) { final Element attribute = document.createElement("attribute"); attribute.setAttribute("name", key); attribute.setAttribute("value", credential.getAuthenticationHeaders().get(key)); rootElement.appendChild(attribute); } // run the auth document through the transformer final DOMSource source = new DOMSource(document); final StringWriter writer = new StringWriter(); final StreamResult result = new StreamResult(writer); this.institutionsAuthTransformer.transform(source, result); // convert transformed xml to json return XML.toJSONObject(writer.getBuffer().toString()); }
From source file:com.gargoylesoftware.htmlunit.javascript.host.XSLTProcessor.java
/** * @return {@link Node} or {@link String} *///from www.j a v a 2s . com private Object transform(final Node source) { try { Source xmlSource = new DOMSource(source.getDomNodeOrDie()); final Source xsltSource = new DOMSource(style_.getDomNodeOrDie()); final org.w3c.dom.Document containerDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder() .newDocument(); final org.w3c.dom.Element containerElement = containerDocument.createElement("container"); containerDocument.appendChild(containerElement); final DOMResult result = new DOMResult(containerElement); final Transformer transformer = TransformerFactory.newInstance().newTransformer(xsltSource); for (final Map.Entry<String, Object> entry : parameters_.entrySet()) { transformer.setParameter(entry.getKey(), entry.getValue()); } transformer.transform(xmlSource, result); final org.w3c.dom.Node transformedNode = result.getNode(); if (transformedNode.getFirstChild().getNodeType() == Node.ELEMENT_NODE) { return transformedNode; } //output is not DOM (text) xmlSource = new DOMSource(source.getDomNodeOrDie()); final StringWriter writer = new StringWriter(); final Result streamResult = new StreamResult(writer); transformer.transform(xmlSource, streamResult); return writer.toString(); } catch (final Exception e) { throw Context.reportRuntimeError("Exception: " + e); } }
From source file:eu.eidas.encryption.SAMLAuthnResponseEncrypter.java
@Nonnull private Response performEncryption(@Nonnull Response samlResponseEncryptee, @Nonnull Credential credential) throws EncryptionException { try {// w w w. j a v a 2 s.c o m // Set Data Encryption parameters EncryptionParameters encParams = new EncryptionParameters(); encParams.setAlgorithm(getDataEncAlgorithm()); // Set Key Encryption parameters KeyEncryptionParameters kekParams = new KeyEncryptionParameters(); kekParams.setEncryptionCredential(credential); kekParams.setAlgorithm(getKeyEncAlgorithm()); KeyInfoGeneratorFactory kigf = Configuration.getGlobalSecurityConfiguration() .getKeyInfoGeneratorManager().getDefaultManager().getFactory(credential); kekParams.setKeyInfoGenerator(kigf.newInstance()); // Setup Open SAML Encrypter Encrypter encrypter = new Encrypter(encParams, kekParams); encrypter.setKeyPlacement(Encrypter.KeyPlacement.INLINE); if (getJcaProviderName() != null) { encrypter.setJCAProviderName(getJcaProviderName()); } for (Assertion assertion : samlResponseEncryptee.getAssertions()) { if (assertion.getDOM() == null) { OpenSamlHelper.marshallToDom(assertion); } manageNamespaces(assertion); } List<EncryptedAssertion> encryptedAssertions = new ArrayList<>(); for (Assertion assertion : samlResponseEncryptee.getAssertions()) { EncryptedAssertion encryptedAssertion = encrypter.encrypt(assertion); encryptedAssertions.add(encryptedAssertion); } Element previousDom = samlResponseEncryptee.getDOM(); if (null == previousDom) { previousDom = OpenSamlHelper.marshallToDom(samlResponseEncryptee); } Document ownerDocument = previousDom.getOwnerDocument(); // Deep copy the previous DOM into a new one using importNode() Document newDocument = DocumentBuilderFactoryUtil.newDocument(); Node copiedRoot = newDocument.importNode(ownerDocument.getDocumentElement(), true); newDocument.appendChild(copiedRoot); Element newRootElement = newDocument.getDocumentElement(); NodeList assertionList = newRootElement.getElementsByTagNameNS( Assertion.DEFAULT_ELEMENT_NAME.getNamespaceURI(), Assertion.DEFAULT_ELEMENT_NAME.getLocalPart()); // Replace the encrypted assertions by the decrypted assertions in the new DOM tree: for (int i = 0, n = assertionList.getLength(); i < n; i++) { Node assertion = assertionList.item(i); EncryptedAssertion encryptedAssertion = encryptedAssertions.get(i); Element encryptedAssertionDOM = encryptedAssertion.getDOM(); Node copiedEncryptedAssertion; if (null == encryptedAssertionDOM) { encryptedAssertionDOM = OpenSamlHelper.marshallToDom(encryptedAssertion); } // we may use adoptNode() instead of importNode() because the unmarshaller rectifies the ID-ness: copiedEncryptedAssertion = newDocument.adoptNode(encryptedAssertionDOM); newRootElement.replaceChild(copiedEncryptedAssertion, assertion); } // Finally unmarshall the updated DOM into a new XMLObject graph: // The unmarshaller rectifies the ID-ness: // See org.opensaml.saml1.core.impl.AssertionUnmarshaller.unmarshall() // See org.opensaml.saml2.core.impl.AssertionUnmarshaller.processAttribute() // And org.opensaml.saml1.core.impl.ResponseAbstractTypeUnmarshaller.unmarshall() // And org.opensaml.saml2.core.impl.StatusResponseTypeUnmarshaller.processAttribute() Response encryptedResponse = (Response) OpenSamlHelper.unmarshallFromDom(newDocument); if (LOGGER.isTraceEnabled()) { try { LOGGER.trace("SAML Response XMLObject encrypted: " + EidasStringUtil.toString(DocumentBuilderFactoryUtil.marshall(newDocument, true))); } catch (TransformerException e) { LOGGER.error(e.getMessage(), e); } } return encryptedResponse; } catch (org.opensaml.xml.encryption.EncryptionException | ParserConfigurationException | MarshallException | UnmarshallException e) { throw new EncryptionException(e); } }
From source file:com.quest.keycloak.protocol.wsfed.builders.RequestSecurityTokenResponseBuilder.java
protected Document signAssertion(Document samlDocument) throws ProcessingException { Element originalAssertionElement = samlDocument.getDocumentElement(); //org.keycloak.saml.common.util.DocumentUtil.getChildElement(samlDocument.getDocumentElement(), new QName(JBossSAMLURIConstants.ASSERTION_NSURI.get(), JBossSAMLConstants.ASSERTION.get())); if (originalAssertionElement == null) return samlDocument; Node clonedAssertionElement = originalAssertionElement.cloneNode(true); Document temporaryDocument; try {/*from w w w . j a v a 2 s. com*/ temporaryDocument = org.keycloak.saml.common.util.DocumentUtil.createDocument(); } catch (ConfigurationException e) { throw new ProcessingException(e); } temporaryDocument.adoptNode(clonedAssertionElement); temporaryDocument.appendChild(clonedAssertionElement); signDocument(temporaryDocument); return temporaryDocument; }
From source file:com.liferay.portal.editor.fckeditor.receiver.impl.BaseCommandReceiver.java
private Node _createRoot(Document document, String command, String resourceType, String path, String url) { Element rootElement = document.createElement("Connector"); document.appendChild(rootElement); rootElement.setAttribute("command", command); rootElement.setAttribute("resourceType", resourceType); Element currentFolderElement = document.createElement("CurrentFolder"); rootElement.appendChild(currentFolderElement); currentFolderElement.setAttribute("path", path); currentFolderElement.setAttribute("url", url); return rootElement; }
From source file:cz.cas.lib.proarc.common.export.mets.MetsUtils.java
/** * * Returns a node from the xml document defined by the Xpath * * @param elements/* w ww . j a v a2 s. c o m*/ * @param xPath * @return */ public static Node xPathEvaluateNode(List<Element> elements, String xPath) throws MetsExportException { Document document = null; try { document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); } catch (ParserConfigurationException e1) { throw new MetsExportException("Error while evaluating xPath " + xPath, false, e1); } for (Element element : elements) { Node newNode = element.cloneNode(true); document.adoptNode(newNode); document.appendChild(newNode); } XPath xpathObject = XPathFactory.newInstance().newXPath(); try { return (Node) xpathObject.compile(xPath).evaluate(document, XPathConstants.NODE); } catch (XPathExpressionException e) { throw new MetsExportException("Error while evaluating xPath " + xPath, false, e); } }
From source file:org.apache.cxf.cwiki.SiteExporter.java
public static synchronized Space getSpace(String key) { Space space = spaces.get(key);//from w w w . j ava 2 s.com if (space == null) { try { doLogin(); Document doc = DOMUtils.newDocument(); Element el = doc.createElementNS(SOAPNS, "ns1:getSpace"); Element el2 = doc.createElement("in0"); el.appendChild(el2); el2.setTextContent(loginToken); el2 = doc.createElement("in1"); el.appendChild(el2); el2.setTextContent(key); doc.appendChild(el); Document out = getDispatch().invoke(doc); space = new Space(out); spaces.put(key, space); } catch (Exception e) { e.printStackTrace(); } } return space; }