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