使用Java插入段落和子段落的级别

使用Java插入段落和子段落的级别,java,Java,我想有一种方法来实现将级别标记为段落、子段落和子句,以下是文本的输入和输出格式 输入文本: (A) Preparations (1) Required ingredients to prepare... (a) 100ml of coconut oil (b) 2 table spoon of lemon mixed.. (B) Process steps (i) Mix well the ... (ii) Apply in all parts of ... (iii) Gen

我想有一种方法来实现将级别标记为段落、子段落和子句,以下是文本的输入和输出格式

输入文本:

 (A) Preparations 
 (1) Required ingredients to prepare...
 (a) 100ml of coconut oil
 (b) 2 table spoon of lemon mixed..
 (B) Process steps
 (i) Mix well the ...
 (ii) Apply in all parts of ...
 (iii) Gently give a massage..
输出文本:

 <Level type="Para" id="(A)">
      (A) Preparations 
      <Level type="Sub-Para" id="(A)(1)">
           (1) Required ingredients to prepare...
           <Level type="Clause" id="(A)(1)(a)">
                (a) 100ml of coconut oil
           <Level type="Clause" id="(A)(1)(b)">
                (b) 2 table spoon of lemon mixed..
 <Level type="Para" id="(B)">
      (B) Process steps
      <Level type="Sub-Para" id="(B)(i)">
           (i) Mix well the ...
      <Level type="Sub-Para" id="(B)(ii)">
           (ii) Apply in all parts of ...
      <Level type="Sub-Para" id="(B)(iii)">
           (iii) Gently give a massage..

(A) 准备工作
(1) 准备所需的配料。。。
(a) 100毫升椰子油
(b) 2茶匙柠檬汁。。
(B) 工艺步骤
(i) 混合好。。。
(ii)适用于……的所有部分。。。
(iii)轻轻地进行按摩。。
请协助如何使用Java实现这一点。我将自己试一试

显示文本对齐方式只是为了更好地理解级别

如果我在解释问题时不清楚,请让我知道,我将修改问题

谢谢。

公共类App2{
public class App2 {


private static final String PARA_STRING = "<Level type=\"Para\" id=\"%s\">\n\t%s";
private static final String SUBPARA_STRING = "\t<Level type=\"Sub-Para\" id=\"%s%s\">\n\t\t%s";
private static final String CLAUSE_STRING = "\t\t<Level type=\"Clause\" id=\"%s%s%s\">\n\t\t\t%s";

public static void main(String[] args) throws Exception
{
    StringBuilder input = new StringBuilder();
    input.append("(A) Preparations");
    input.append("\n");
    input.append("(1) Required ingredients to prepare...");
    input.append("\n");
    input.append("(a) 100ml of coconut oil");
    input.append("\n");
    input.append("(b) 2 table spoon of lemon mixed..");
    input.append("\n");
    input.append("(B) Process steps");
    input.append("\n");
    input.append("(i) Mix well the ...");
    input.append("\n");
    input.append("(ii) Apply in all parts of ...");
    input.append("\n");
    input.append("(iii) Gently give a massage..");
    input.append("\n");

    buildParagraphs(input.toString());
}

/**
 * 
 * @throws Exception
 */
public static void buildParagraphs(String input) throws Exception
{
    StringBuilder result = new StringBuilder();

    String[] inputArray = input.split("\n");
    String paraPrefix = null;
    String subParaPrefix = null;


    for (int i = 0; i < inputArray.length; i++)
    {
        String item = inputArray[i].split(" ")[0];
        if(isPara(item))
        {
            result.append(String.format(PARA_STRING, item, inputArray[i]));
            result.append("\t");
            result.append("\n");
            paraPrefix = item;
        }
        else if(isSubPara(item))
        {
            result.append(String.format(SUBPARA_STRING, paraPrefix, item, inputArray[i]));
            result.append("\t");
            result.append("\n");
            subParaPrefix = item;
        }
        else if(isClause(item))
        {
            result.append(String.format(CLAUSE_STRING, paraPrefix, subParaPrefix, item, inputArray[i]));
            result.append("\t\t");
            result.append("\n");
        }
    }

    System.out.println(result);
}

public static boolean isPara(String source)
{
    Pattern p = Pattern.compile("\\([A-Z]\\)");
    Matcher m = p.matcher(source);
    boolean b = m.matches();

    return b;
}

public static boolean isSubPara(String source)
{
    Pattern p = Pattern.compile("^\\([[0-9]|i]+\\)$");
    Matcher m = p.matcher(source);
    boolean b = m.matches();

    return b;
}

public static boolean isClause(String source)
{
    Pattern p = Pattern.compile("\\([a-z]\\)");
    Matcher m = p.matcher(source);
    boolean b = m.matches();

    return b;
}}
私有静态最终字符串PARA_String=“\n\t%s”; 私有静态最终字符串子参数\u String=“\t\n\t\t%s”; 私有静态最终字符串子句\u String=“\t\t\n\t\t\t%s”; 公共静态void main(字符串[]args)引发异常 { StringBuilder输入=新建StringBuilder(); 输入。追加(“(A)准备工作”); input.append(“\n”); 输入。追加(“(1)准备所需的配料…”); input.append(“\n”); 输入。添加(“(a)100ml椰子油”); input.append(“\n”); 输入。追加((b)2汤匙柠檬混合汁); input.append(“\n”); 输入。追加(“(B)过程步骤”); input.append(“\n”); 输入。追加(“(i)充分混合…”); input.append(“\n”); 输入。追加(“(ii)适用于……的所有部分); input.append(“\n”); 输入。追加(“(iii)轻轻按摩…”; input.append(“\n”); 构建段落(input.toString()); } /** * *@抛出异常 */ 公共静态void构建段落(字符串输入)引发异常 { StringBuilder结果=新建StringBuilder(); 字符串[]inputArray=input.split(“\n”); 字符串paraPrefix=null; 字符串subParaPrefix=null; for(int i=0;i