001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019 020package org.apache.isis.core.runtime.snapshot; 021 022import java.io.CharArrayWriter; 023import java.io.OutputStream; 024import java.io.OutputStreamWriter; 025import java.io.Writer; 026 027import javax.xml.transform.Result; 028import javax.xml.transform.Source; 029import javax.xml.transform.Transformer; 030import javax.xml.transform.TransformerConfigurationException; 031import javax.xml.transform.TransformerFactory; 032import javax.xml.transform.dom.DOMSource; 033import javax.xml.transform.stream.StreamResult; 034 035import org.w3c.dom.Element; 036 037public class DomSerializerJaxp implements DomSerializer { 038 039 private final TransformerFactory transformerFactory; 040 private final Transformer newTransformer; 041 042 public DomSerializerJaxp() throws TransformerConfigurationException { 043 this.transformerFactory = TransformerFactory.newInstance(); 044 this.newTransformer = transformerFactory.newTransformer(); 045 } 046 047 @Override 048 public String serialize(final Element domElement) { 049 final CharArrayWriter caw = new CharArrayWriter(); 050 try { 051 serializeTo(domElement, caw); 052 return caw.toString(); 053 } catch (final Exception e) { 054 return null; 055 } 056 } 057 058 @Override 059 public void serializeTo(final Element domElement, final OutputStream os) throws Exception { 060 final OutputStreamWriter osw = new OutputStreamWriter(os); 061 serializeTo(domElement, osw); 062 } 063 064 @Override 065 public void serializeTo(final Element domElement, final Writer writer) throws Exception { 066 final Source source = new DOMSource(domElement); 067 final Result result = new StreamResult(writer); 068 newTransformer.transform(source, result); 069 } 070 071}