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 com.google.common.base.Preconditions;
11 import java.io.StringReader;
12 import java.io.StringWriter;
13 import javax.xml.bind.ValidationEventHandler;
14 import javax.xml.bind.annotation.DomHandler;
15 import javax.xml.transform.Source;
16 import javax.xml.transform.stream.StreamResult;
17 import javax.xml.transform.stream.StreamSource;
19 class SnapshotHandler implements DomHandler<String, StreamResult> {
21 private static final String START_TAG = "<configuration>";
22 private static final String END_TAG = "</configuration>";
24 private StringWriter xmlWriter = new StringWriter();
27 public StreamResult createUnmarshaller(final ValidationEventHandler errorHandler) {
28 xmlWriter.getBuffer().setLength(0);
29 return new StreamResult(xmlWriter);
33 public String getElement(final StreamResult rt) {
34 String xml = rt.getWriter().toString();
35 int beginIndex = xml.indexOf(START_TAG) + START_TAG.length();
36 int endIndex = xml.indexOf(END_TAG);
37 Preconditions.checkArgument(beginIndex != -1 && endIndex != -1,
38 "Unknown element present in config snapshot(expected only configuration): %s", xml);
39 return xml.substring(beginIndex, endIndex);
43 public Source marshal(final String n, final ValidationEventHandler errorHandler) {
45 String xml = START_TAG + n.trim() + END_TAG;
46 StringReader xmlReader = new StringReader(xml);
47 return new StreamSource(xmlReader);
48 } catch(final Exception e) {
49 throw new RuntimeException(e);