Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/aendvari/common/osm/OsmPrinter.java


1   /*
2    * OsmPrinter.java
3    *
4    * Copyright (c) 2001, 2002 Aendvari, Ltd. All Rights Reserved.
5    *
6    * See the file LICENSE for terms of use.
7    *
8    */
9   
10  package com.aendvari.common.osm;
11  
12  import java.io.Reader;
13  import java.io.Writer;
14  
15  import java.util.ArrayList;
16  import java.util.Collection;
17  import java.util.Iterator;
18  import java.util.Map;
19  import java.util.StringTokenizer;
20  
21  import com.aendvari.common.osm.*;
22  import com.aendvari.common.util.*;
23  
24  
25  /**
26   * <p>This class prints a given {@link Osm} or {@link OsmNode} in a human-readable format.</p>
27   *
28   * @author  Scott Milne
29   *
30   */
31  
32  public class OsmPrinter
33  {
34    /**
35     * Return the {@link OsmNode} as it's string representation.
36     *
37     */
38  
39    public static String nodeToString( OsmNode osmNode )
40    {
41      return examineChildNode( osmNode, "" );
42    }
43  
44    /**
45     * Print the {@link OsmNode} string representation to the supplied <code>Writer</code>.
46     *
47     */
48  
49    public static void writeString( OsmNode osmNode, Writer writer )
50      throws java.io.IOException
51    {
52      String output = nodeToString( osmNode );
53  
54      writer.write(output);
55    }
56  
57    /**
58     * Convert the node into a string representation.
59     *
60     * @param    osmNode            The {@link OsmNode} instance to convert.
61     * @param    indent            The indentation level for the display.
62     *
63     * @return                  The {@link OsmNode} node string representation.
64     *
65     */
66  
67    private static String examineChildNode( OsmNode osmNode, String indent )
68    {
69      String display = "";
70  
71      display += indent + "+ " +osmNode.getNodeName();
72      display += " = " + osmNode.getNodeValue();
73      display += extractAttributes( osmNode );
74      display += "\n";
75  
76      OsmNode childNode = osmNode.getFirstChild();
77  
78      while (childNode != null)
79      {
80        display += examineChildNode( childNode, indent + "  " );
81  
82        childNode = childNode.getNextSibling();
83      }
84  
85      return display;
86    }
87  
88    /**
89     * Extract and return the attributes of the node as a string.
90     *
91     * @param    osmNode            The {@link OsmNode} instance to extract attributes from.
92     *
93     * @return                  The {@link OsmNode} attributes string representation.
94     *
95     */
96  
97    private static String extractAttributes( OsmNode osmNode )
98    {
99      String attrsString = " ";
100 
101     Collection attributes = osmNode.getAttributes().entrySet();
102 
103     Iterator attrsIterator = attributes.iterator();
104 
105     while (attrsIterator.hasNext())
106     {
107       // get the attribute entry
108       Map.Entry entry = (Map.Entry)attrsIterator.next();
109 
110       // get the attribute key/value
111       String key = (String)entry.getKey();
112       String value = "";
113 
114       if (value != null)
115         value = entry.getValue().toString();
116 
117       // add attribute to output
118       attrsString += "{" + key + "=" + value + "}";
119 
120       if (attrsIterator.hasNext())
121       {
122         attrsString += ",";
123       }
124     }
125 
126     return attrsString;
127   }
128 }
129