title: How to Determine Indentation: Converting Java Source FIle to HTML File
body: <p>I'm trying to programatically convert a Java source file into an HTML file using <code>PrintWriter</code> to write to a seperate .html file</p>
  
  <p>Example source file may look like this. <br>
  <b>HelloWorld.java:</b></p>
  
  <pre><code>public class HelloWorld {
      public static void main(String[] args) {
          System.out.println("Hello World!");
  
          while (true) {
              System.out.println("Hello World!"); 
              // Disregard this ridiculous example
          }
      }
  }
  </code></pre>
  
  <p>All My printing works fine except I have a problem with indentation.  Everyting is aligned left.</p>
  
  <p><b>HelloWorld.html</b> (as seen in browser):</p>
  
  <pre><code>public class HelloWorld {
  public static void main(String[] args) {
  System.out.println("Hello World!");
  
  while (true) {
  System.out.println("Hello World!"); 
  // Disregard this ridiculous example
  }
  }
  }
  </code></pre>
  
  <p><b>Program Source Code Snippet: I want this program to determine for me, which line in the java source code should get indentation when it is being converted to HTML. I don't want to do it manually, because then I'd have to write a different program for every source file</b></p>
  
  <pre><code>    PrintWriter output = new PrintWriter(newFile);
  
      output.print("&lt;!DOCTYPE html&gt;&lt;html&gt;&lt;head&gt;"
              + "&lt;style&gt;"
              + ".keyword { font-weight: bold; color: blue}"
              + "&lt;/style&gt;&lt;/head&gt;&lt;body&gt;");
  
      while (input.hasNextLine()) {
          String line = input.nextLine();
          String[] tokens = line.split(" ");
  
          for (int i = 0; i &lt; tokens.length; i++) {
              if (keywordSet.contains(tokens[i])) {
                  // Gives Java keyword bold blue font
                  output.print("&lt;span class=\"keyword\"&gt;");
                  output.print(tokens[i] + " ");
                  output.print("&lt;/span&gt;");
              } else {
                  output.print(tokens[i] + " ");
              }
          }
          output.print("&lt;br/&gt;");
      }
  
      output.print("&lt;/body&gt;&lt;html&gt;");
      output.close();
  </code></pre>
  
  <p><b>Note: The reason I <code>split()</code> each line is because certain keywords that may be in that line, are doing to be highlighted in the html file, which I do with a <code>&lt;span&gt;</code>, as noted in my code</b></p>
  
  <p>In the program source code, I obviously don't have any indenting implementation, so I know why I don't have indentation in the html file.  I really don't know how to go about implementing this.  </p>
  
  <p>How do I determine which line gets indentation, and how much indentation?</p>
  
  <p><b>EDIT:</b>
  <b>My Guess: Determine how much whitespace is in the line before splitting it, save the into a variable, then print those spaces in for form of &amp;nbsp's before I print anything else in the line.  But how do I determine how much whitespace is at the beginning of the line?</b></p>