001: package org.geotools.coverage.grid.io.imageio;
002:
003: import javax.imageio.metadata.IIOMetadata;
004: import javax.imageio.metadata.IIOMetadataFormat;
005: import javax.imageio.metadata.IIOMetadataNode;
006:
007: import org.w3c.dom.NamedNodeMap;
008: import org.w3c.dom.Node;
009:
010: /**
011: * Utility calss that can be used to dump a DOM tree in a formatted way.
012: * <p>
013: * It is useful for inspecting the metadata inside GeoTiff files.
014: *
015: * @author Simone Giannecchini
016: * @since 2.3.x
017: */
018: public final class IIOMetadataDumper {
019:
020: /** Root of the XML tree to display. */
021: private Node root;
022:
023: /**
024: * The {@link IIOMetadataFormat} name to print out the {@link IIOMetadata}
025: * for.
026: */
027: private volatile String formatName = "";
028:
029: /** The encoded {@link IIOMetadata} as a simple {@link String}. */
030: private String metadata;
031:
032: /**
033: * Constructor for a {@link IIOMetadataDumper} accepting and
034: * {@link IIOMetadata} and a {@link String} for the format name of the XML
035: * metadata to use.
036: *
037: * @param metadata
038: * The metadta to display.
039: * @param name
040: * The format of the metaata to display.
041: */
042: public IIOMetadataDumper(IIOMetadata metadata, String name) {
043: this .root = metadata.getAsTree(name);
044: StringBuffer buff = new StringBuffer();
045: parseMetadata(buff, root, 0);
046: this .formatName = name;
047: this .metadata = buff.toString();
048:
049: }
050:
051: /**
052: * Constructor for a {@link IIOMetadataDumper} accepting an
053: * {@link IIOMetadataNode}. It has no way to choose the format of the
054: * metadata to parse since this choice has been alreadu done previously.
055: *
056: * @param rootNode
057: */
058: public IIOMetadataDumper(IIOMetadataNode rootNode) {
059: this .root = rootNode;
060: StringBuffer buff = new StringBuffer();
061: parseMetadata(buff, root, 0);
062: this .metadata = buff.toString();
063: }
064:
065: /**
066: * Adds indentation to the tree while we build it
067: *
068: * @param buff
069: * @param level
070: */
071: private void indent(StringBuffer buff, int level) {
072: for (int i = 0; i < level; i++) {
073: buff.append(" ");
074: }
075: }
076:
077: /**
078: * Builds a graphical representation of a certain XML tree.
079: *
080: * @param buff
081: * @param node
082: * @param level
083: */
084: private void parseMetadata(StringBuffer buff, Node node, int level) {
085: indent(buff, level); // emit open tag
086: buff.append("<").append(node.getNodeName());
087: NamedNodeMap map = node.getAttributes();
088: if (map != null) { // print attribute values
089: int length = map.getLength();
090: for (int i = 0; i < length; i++) {
091: Node attr = map.item(i);
092: buff.append(" ").append(attr.getNodeName()).append(
093: "=\"").append(attr.getNodeValue()).append("\"");
094: }
095: }
096:
097: Node child = node.getFirstChild();
098: if (child != null) {
099: buff.append(">\n"); // close current tag
100: while (child != null) { // emit child tags recursively
101: parseMetadata(buff, child, level + 1);
102: child = child.getNextSibling();
103: }
104: indent(buff, level); // emit close tag
105: buff.append("</").append(node.getNodeName()).append(">\n");
106: } else {
107: buff.append("/>\n");
108: }
109: }
110:
111: /**
112: * Allows me to get the generated XML representation for the underlying
113: * tree;
114: *
115: * @return A formatted XML string.
116: */
117: public synchronized String getMetadata() {
118: return metadata;
119: }
120:
121: /**
122: * Retrieves the name of the format we want to get the XML representation
123: * for.
124: *
125: * @return The name of the format we want to get the XML representation for.
126: */
127: public String getFormatName() {
128: return formatName;
129: }
130:
131: /**
132: * Sets the name of the format we want to get the XML representation for.
133: * <p>
134: * This method causes a new generation of the string representation if the
135: * format is different from the one stored.
136: *
137: * @param formatName
138: * The name of the format we want to get the XML representation
139: * for.
140: */
141: public synchronized void setFormatName(String formatName) {
142: if (this .formatName.equalsIgnoreCase(formatName))
143: return;
144: StringBuffer buff = new StringBuffer();
145: parseMetadata(buff, root, 0);
146: this.formatName = formatName;
147: this.metadata = buff.toString();
148:
149: }
150:
151: }
|