Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 1997-2013, www.tinygroup.org (tinygroup@126.com). Licensed under the GPL, Version 3.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.gnu.org/licenses/gpl.html Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.tinygroup.template.parser;
 
 import java.io.Writer;
 import java.util.List;

Created by luoguo on 2014/6/3.
 
 public class CodeBlock {
     private CodeBlock parentCodeBlock;
     private CodeLet headerCodeLet;
     private CodeLet footerCodeLet;
     private List<CodeBlocksubCodeBlocks;
     private int tabIndent = 0;
 
     public CodeBlock getParentCodeBlock() {
         return ;
     }
 
     public int getTabIndent() {
         return ;
     }
 
     public CodeBlock tabIndent(int tabIndent) {
         this. = tabIndent;
         return this;
     }
 
     public CodeBlock setTabIndent(int tabIndent) {
         this. = tabIndent;
         return this;
     }
 
     public CodeBlock setParentCodeBlock(CodeBlock parentCodeBlock) {
         this. = parentCodeBlock;
         return this;
     }
 
     public CodeBlock insertSubCode(String code) {
         CodeBlock newCodeBlock = new CodeBlock().header(code);
         getSubCodeBlocks().add(0, newCodeBlock);
         return this;
     }
 
     public CodeBlock header(CodeLet headerCodeLet) {
         this. = headerCodeLet;
         return this;
     }
 
     public CodeBlock header(String string) {
         this. = new CodeLet(string).endLine();
         return this;
     }
 
     public CodeBlock footer(String string) {
         this. = new CodeLet(string).endLine();
         return this;
     }
 
     public CodeBlock footer(CodeLet footerCodeLet) {
         this. = footerCodeLet;
         return this;
     }
 
     public CodeBlock setSubCodeBlocks(List<CodeBlocksubCodeBlocks) {
         this. = subCodeBlocks;
         for (CodeBlock subCodeBlock : subCodeBlocks) {
             subCodeBlock.setParentCodeBlock(this);
         }
         return this;
     }
 
     public List<CodeBlockgetSubCodeBlocks() {
         return ;
     }
 
     public CodeBlock subCode(String code) {
         if (code != null) {
             subCode(new CodeLet(code).endLine());
         }
        return this;
    }
    public CodeBlock subCode(CodeBlock codeBlock) {
        if (codeBlock != null) {
            if ( == null) {
                 = new ArrayList<CodeBlock>();
            }
            .add(codeBlock);
            codeBlock.setParentCodeBlock(this);
        }
        return this;
    }
    public CodeBlock subCode(CodeLet codeLet) {
        if (codeLet != null) {
            CodeBlock codeBlock = new CodeBlock();
            codeBlock.header(codeLet);
            return subCode(codeBlock);
        }
        return this;
    }
    public CodeLet getHeaderCodeLet() {
        return ;
    }
    public CodeLet getFooterCodeLet() {
        return ;
    }
    public CodeBlock write(Writer writerthrows IOException {
        return write(writer, 0);
    }
    public CodeBlock write(Writer writerint tabthrows IOException {
        int blockIndent = tab + ;
        if ( != null) {
            writePreBlank(writerblockIndent);
            .write(writer);
        }
        if ( != null) {
            for (CodeBlock codeBlock : ) {
                if ( == null &&  == null) {
                    codeBlock.write(writerblockIndent);
                } else {
                    codeBlock.write(writerblockIndent + 1);
                }
            }
        }
        if ( != null) {
            writePreBlank(writerblockIndent);
            .write(writer);
        }
        return this;
    }
    private void writePreBlank(Writer writerint tabthrows IOException {
        if (tab > 0) {
            writer.write(String.format("%" + tab * 4 + "s"""));
        }
    }
    public String toString() {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        Writer writer = new OutputStreamWriter(outputStream);
        try {
            write(writer);
            writer.close();
        } catch (IOException e) {
            //DoNothing
        }
        return outputStream.toString();
    }
New to GrepCode? Check out our FAQ X