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, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hdfs.tools.offlineImageViewer;
019
020import java.io.IOException;
021import java.util.LinkedList;
022
023/**
024 * An XmlImageVisitor walks over an fsimage structure and writes out
025 * an equivalent XML document that contains the fsimage's components.
026 */
027public class XmlImageVisitor extends TextWriterImageVisitor {
028  final private LinkedList<ImageElement> tagQ =
029                                          new LinkedList<ImageElement>();
030
031  public XmlImageVisitor(String filename) throws IOException {
032    super(filename, false);
033  }
034
035  public XmlImageVisitor(String filename, boolean printToScreen)
036       throws IOException {
037    super(filename, printToScreen);
038  }
039
040  @Override
041  void finish() throws IOException {
042    super.finish();
043  }
044
045  @Override
046  void finishAbnormally() throws IOException {
047    write("\n<!-- Error processing image file.  Exiting -->\n");
048    super.finishAbnormally();
049  }
050
051  @Override
052  void leaveEnclosingElement() throws IOException {
053    if(tagQ.size() == 0)
054      throw new IOException("Tried to exit non-existent enclosing element " +
055                "in FSImage file");
056
057    ImageElement element = tagQ.pop();
058    write("</" + element.toString() + ">\n");
059  }
060
061  @Override
062  void start() throws IOException {
063    write("<?xml version=\"1.0\" ?>\n");
064  }
065
066  @Override
067  void visit(ImageElement element, String value) throws IOException {
068    writeTag(element.toString(), value);
069  }
070
071  @Override
072  void visitEnclosingElement(ImageElement element) throws IOException {
073    write("<" + element.toString() + ">\n");
074    tagQ.push(element);
075  }
076
077  @Override
078  void visitEnclosingElement(ImageElement element,
079      ImageElement key, String value)
080       throws IOException {
081    write("<" + element.toString() + " " + key + "=\"" + value +"\">\n");
082    tagQ.push(element);
083  }
084
085  private void writeTag(String tag, String value) throws IOException {
086    write("<" + tag + ">" + value + "</" + tag + ">\n");
087  }
088}