/* LanguageTool, a natural language style checker
 * Copyright (C) 2022 Daniel Naber (http://www.danielnaber.de)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */
package org.languagetool.dev;

import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

/**
 * A lightweight XML indentation helper for grammar.xml files. Unlike other XML
 * indenters, this will really only change the indentation (spaces at start of each line)
 * and *not* insert or remove line breaks.
 */
public class XmlIndenter {

  private static final int INDENT = 2;

  public static void main(String[] args) throws IOException {
    if (args.length != 1) {
      System.out.println("Usage: " + XmlIndenter.class.getSimpleName() + " <xmlFile>");
      System.exit(1);
    }
    List<String> lines = Files.readAllLines(Paths.get(args[0]));
    boolean inCategory = false;
    boolean inRuleGroup = false;
    boolean inRule = false;
    boolean inAntiPattern = false;
    boolean inPattern = false;
    boolean inMarker = false;
    boolean inToken = false;
    for (String line : lines) {
      String origLine = line;
      line = line.trim();
      if (line.contains("</marker>") && (inPattern || inAntiPattern)) { inMarker = false; }
      if (line.startsWith("</antipattern>")) { inAntiPattern = false; }
      if (line.startsWith("</pattern>")) { inPattern = false; }
      if (line.startsWith("</rule>")) { inRule = false; }
      if (line.startsWith("</rulegroup")) { inRuleGroup = false; }
      if (line.startsWith("</category")) { inCategory = false; }
      int level = INDENT + (inCategory ? INDENT : 0) + (inRuleGroup ? INDENT : 0) + (inRule ? INDENT : 0) +
        (inPattern ? INDENT : 0) + (inAntiPattern ? INDENT : 0) + (inMarker ? INDENT : 0) + (inToken ? INDENT : 0);
      if (line.startsWith("<category") || line.startsWith("</category")) {
        level = INDENT;
      }
      if (line.equals("</token>")) {
        level -= INDENT;
      }
      String indentSpaces = StringUtils.repeat(' ', level);
      if (!line.isEmpty() && (inCategory || line.startsWith("<category") || line.startsWith("</category"))) {
        System.out.println(indentSpaces + line);
      } else {
        System.out.println(origLine);
      }
      if (line.startsWith("<category")) { inCategory = true; }
      if (line.startsWith("<rulegroup")) { inRuleGroup = true; }
      if (line.startsWith("<rule ") || line.startsWith("<rule>")) { inRule = true; }
      if (line.startsWith("<pattern")) { inPattern = true; }
      if (line.startsWith("<antipattern")) { inAntiPattern = true; }
      if (line.contains("<marker>") && !line.contains("</marker>") && (inPattern || inAntiPattern)) { inMarker = true; }
      if (line.contains("</token>") || (line.contains("<token") && line.contains("/>")) && (inPattern || inAntiPattern)) { inToken = false; }
      if (line.contains("<token") && !line.contains("/>") && !line.contains("</token>") && (inPattern || inAntiPattern)) {
        inToken = true;
      }
    }
  }
}

@@ -1778,32 +1778,123 @@
 Marker = false;%0A
+    boolean inAnd = false;%0A    boolean inUnify = false;%0A    boolean inUnifyIgnore = false;%0A
     boolean inTo
@@ -2408,24 +2408,212 @@
  = false; %7D%0A
+      if (line.startsWith(%22%3C/and%22)) %7B inAnd = false; %7D%0A      if (line.startsWith(%22%3C/unify%3E%22)) %7B inUnify = false; %7D%0A      if (line.startsWith(%22%3C/unify-ignore%3E%22)) %7B inUnifyIgnore = false; %7D%0A
       int le
@@ -2819,16 +2819,103 @@
 INDENT :
+ 0) +%0A        (inAnd ? INDENT : 0) + (inUnify ? INDENT : 0) + (inUnifyIgnore ? INDENT :
  0);%0A   
@@ -3824,32 +3824,237 @@
 arker = true; %7D%0A
+      if (line.contains(%22%3Cand%3E%22)) %7B inAnd = true; %7D%0A      if (line.contains(%22%3Cunify%3E%22) %7C%7C line.contains(%22%3Cunify %22)) %7B inUnify = true; %7D%0A      if (line.contains(%22%3Cunify-ignore%3E%22)) %7B inUnifyIgnore = true; %7D%0A
       if (line.c

support indentation for more elements