com.love320.templateparser.templateprocess.impl.SeparationImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.love320.templateparser.templateprocess.impl.SeparationImpl.java

Source

/**
 * Copyright (c) 2010-2012 love320.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * 
 * Founder admin@love320.com
 */
package com.love320.templateparser.templateprocess.impl;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.love320.templateparser.templateprocess.Separation;

/**
 * @ClassName: SeparationImpl
 * @Description: TODO
 * @author love320.com
 * @date 2012-5-3 ?09:42:49
 * 
 */
public class SeparationImpl implements Separation {

    private String tagNamehead = "love320_";
    private String tagNamefoot = "/";

    public void setTagNamehead(String tagNamehead) {
        this.tagNamehead = tagNamehead;
    }

    public void setTagNamefoot(String tagNamefoot) {
        this.tagNamefoot = tagNamefoot;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.love320.templateparser.templateprocess.Separation#getXML(java.lang
     * .String)
     */
    @Override
    public String getXML(String str) {
        Document document = DocumentHelper.createDocument();//?xml
        Element rootElement = document.addElement("root");// root
        rootElement.addElement("site").setText("www.love320.com");//? 
        Element contentElement = rootElement.addElement("content");//??
        for (String singStr : str.split("\r\n")) {//?????
            tagStrRow(singStr, contentElement);//???
            addContent("/r/n", 3, contentElement);//?  -- /r/n
        }
        return document.asXML();//xml
    }

    /**
     * ??? 
     * */
    private void tagStrRow(String str, Element contentElement) {
        String[] strS = str.split("\\{" + tagNamehead);// tagNamehead 
        if (strS.length > 1) { //?
            for (String singStr : strS) {
                if (singStr.indexOf(tagNamefoot + "}") != -1) {//?????
                    String[] singStrS = singStr.split(tagNamefoot + "}");
                    if (singStrS.length == 2) {
                        addContent(singStrS[0], 2, contentElement);
                        addContent(singStrS[1], 1, contentElement);
                    } else {
                        addContent(singStrS[0], 2, contentElement);
                    }
                } else {
                    addContent(singStr, 1, contentElement);
                }
            }
        } else {
            addContent(str, 1, contentElement);
        }

    }

    //xml?
    private void addContent(String str, int type, Element contentElement) {
        switch (type) {
        case 1:
            Element singStringElement = contentElement.addElement("item");
            singStringElement.addAttribute("type", "string");
            singStringElement.addCDATA(str);
            break;
        case 2:
            Element singLalbelElement = contentElement.addElement("item");
            singLalbelElement.addAttribute("type", "label");
            singLalbelElement.setText(str);
            break;
        case 3:
            Element singSymbolsElement = contentElement.addElement("item");
            singSymbolsElement.addAttribute("type", "symbols");
            singSymbolsElement.setText(str);
            break;
        default:
            break;
        }
    }

}