2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.config.persist.storage.file.xml.model;
10 import javax.xml.bind.ValidationEventHandler;
11 import javax.xml.bind.annotation.DomHandler;
12 import javax.xml.transform.Source;
13 import javax.xml.transform.stream.StreamResult;
14 import javax.xml.transform.stream.StreamSource;
15 import java.io.StringReader;
16 import java.io.StringWriter;
18 class SnapshotHandler implements DomHandler<String, StreamResult> {
20 private static final String START_TAG = "<configuration>";
21 private static final String END_TAG = "</configuration>";
23 private StringWriter xmlWriter = new StringWriter();
25 public StreamResult createUnmarshaller(ValidationEventHandler errorHandler) {
26 xmlWriter.getBuffer().setLength(0);
27 return new StreamResult(xmlWriter);
30 public String getElement(StreamResult rt) {
31 String xml = rt.getWriter().toString();
32 int beginIndex = xml.indexOf(START_TAG) + START_TAG.length();
33 int endIndex = xml.indexOf(END_TAG);
34 return xml.substring(beginIndex, endIndex);
37 public Source marshal(String n, ValidationEventHandler errorHandler) {
39 String xml = START_TAG + n.trim() + END_TAG;
40 StringReader xmlReader = new StringReader(xml);
41 return new StreamSource(xmlReader);
42 } catch(Exception e) {
43 throw new RuntimeException(e);