use of javax.xml.transform.dom.DOMSource in project cas by apereo.
the class SamlUtils method transformSamlObject.
/**
* Transform saml object to String.
*
* @param configBean the config bean
* @param samlObject the saml object
* @return the string
* @throws SamlException the saml exception
*/
public static StringWriter transformSamlObject(final OpenSamlConfigBean configBean, final XMLObject samlObject) throws SamlException {
final StringWriter writer = new StringWriter();
try {
final Marshaller marshaller = configBean.getMarshallerFactory().getMarshaller(samlObject.getElementQName());
if (marshaller != null) {
final Element element = marshaller.marshall(samlObject);
final DOMSource domSource = new DOMSource(element);
final StreamResult result = new StreamResult(writer);
final TransformerFactory tf = TransformerFactory.newInstance();
final Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
}
} catch (final Exception e) {
throw new SamlException(e.getMessage(), e);
}
return writer;
}
use of javax.xml.transform.dom.DOMSource in project cas by apereo.
the class DefaultRelyingPartyTokenProducer method serializeRelyingPartyToken.
private String serializeRelyingPartyToken(final Element rpToken) {
try {
final StringWriter sw = new StringWriter();
final Transformer t = TransformerFactory.newInstance().newTransformer();
t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, BooleanUtils.toStringYesNo(Boolean.TRUE));
t.transform(new DOMSource(rpToken), new StreamResult(sw));
return sw.toString();
} catch (final TransformerException e) {
throw Throwables.propagate(e);
}
}
use of javax.xml.transform.dom.DOMSource in project bazel by bazelbuild.
the class ManifestMergerAction method removePermissions.
private static Path removePermissions(Path manifest, Path outputDir) throws IOException, ParserConfigurationException, TransformerConfigurationException, TransformerException, TransformerFactoryConfigurationError, SAXException {
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = docBuilder.parse(manifest.toFile());
for (String tag : PERMISSION_TAGS) {
NodeList permissions = doc.getElementsByTagName(tag);
if (permissions != null) {
for (int i = permissions.getLength() - 1; i >= 0; i--) {
Node permission = permissions.item(i);
permission.getParentNode().removeChild(permission);
}
}
}
// Write resulting manifest to the output directory, maintaining full path to prevent collisions
Path output = outputDir.resolve(manifest.toString().replaceFirst("^/", ""));
Files.createDirectories(output.getParent());
TransformerFactory.newInstance().newTransformer().transform(new DOMSource(doc), new StreamResult(output.toFile()));
return output;
}
use of javax.xml.transform.dom.DOMSource in project gwt-test-utils by gwt-test-utils.
the class XMLParserTest method convertXMLFileToString.
private String convertXMLFileToString(String fileName) {
try {
InputStream inputStream = this.getClass().getResourceAsStream(fileName);
org.w3c.dom.Document doc = XmlUtils.newDocumentBuilder().parse(inputStream);
StringWriter stw = new StringWriter();
Transformer serializer = TransformerFactory.newInstance().newTransformer();
serializer.transform(new DOMSource(doc), new StreamResult(stw));
return stw.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
use of javax.xml.transform.dom.DOMSource in project gephi by gephi.
the class LayoutPresetPersistence method savePreset.
public void savePreset(String name, Layout layout) {
Preset preset = addPreset(new Preset(name, layout));
FileOutputStream fos = null;
try {
//Create file if dont exist
FileObject folder = FileUtil.getConfigFile("layoutpresets");
if (folder == null) {
folder = FileUtil.getConfigRoot().createFolder("layoutpresets");
}
File presetFile = new File(FileUtil.toFile(folder), name + ".xml");
//Create doc
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
final Document document = documentBuilder.newDocument();
document.setXmlVersion("1.0");
document.setXmlStandalone(true);
//Write doc
preset.writeXML(document);
//Write XML file
fos = new FileOutputStream(presetFile);
Source source = new DOMSource(document);
Result result = new StreamResult(fos);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.transform(source, result);
} catch (Exception e) {
Logger.getLogger("").log(Level.SEVERE, "Error while writing preset file", e);
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException ex) {
}
}
}
}
Aggregations