Java XML Document Format formatPython(int start, Document doc, String s)

Here you can find the source of formatPython(int start, Document doc, String s)

Description

format Python

License

Open Source License

Declaration

private static String formatPython(int start, Document doc, String s) throws BadLocationException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.NoSuchElementException;
import java.util.Scanner;
import javax.swing.text.BadLocationException;

import javax.swing.text.Document;

public class Main {
    private static String formatPython(int start, Document doc, String s) throws BadLocationException {
        int pos = 0;
        int prev;
        boolean check = false;

        String spaces = "";
        String formattedLine = "";
        String scannedLine = "";

        Scanner src = new Scanner(doc.getText(0, doc.getLength()));

        try {//from   w w  w .j a v  a 2  s  . c  om
            while (!check) {
                String nextLine = src.nextLine();
                prev = pos;
                pos += nextLine.length() + 1;
                if (pos > start) {
                    check = true;
                    pos -= prev;
                }
            }
        } catch (NoSuchElementException e) {
            return s;
        }

        for (int j = 0; j < pos - 1; j++) {
            spaces += " ";
        }

        Scanner lineCheck = new Scanner(s);

        int count = 0;
        while (lineCheck.hasNext()) {
            scannedLine = lineCheck.nextLine();
            if (count == 0) {
                formattedLine += scannedLine;
                count++;
            } else {
                formattedLine += '\n' + spaces + scannedLine;
            }
        }

        return formattedLine;
    }
}

Related

  1. format(Document document)
  2. format(Source xslSource, Document xml, URIResolver resolver)
  3. formatElementEnd( Document document, Element element, String formatString)
  4. formatElementStart( Document document, Element element, String formatString)
  5. formatNode(Document doc, Node parent, Node node)
  6. formatXML(Document document, org.w3c.dom.Element root, String tab)
  7. formatXML(Document xml, Document xsl)
  8. formatXML(Document xml, Document xsl, URIResolver resolver, Writer output)
  9. pretty(Document document, OutputStream outputStream, int indent)