All Downloads are FREE. Search and download functionalities are using the official Maven repository.

se.somath.publisher.builder.SourceCodeBuilder Maven / Gradle / Ivy

There is a newer version: 1.0.5
Show newest version
package se.somath.publisher.builder;

import se.somath.publisher.formatter.HtmlEncoder;
import se.somath.publisher.includer.SourceCodeReader;
import se.somath.publisher.parser.IncludeSourceCodeParser;

import java.util.List;

public class SourceCodeBuilder {
    private SourceCodeReader sourceCodeReader = new SourceCodeReader();

    public List getFormattedSourceCode(List result, IncludeSourceCodeParser includeSourceCodeParser) {
        if (includeSourceCodeParser.shouldDisplayFileName()) {
            addFileName(result, includeSourceCodeParser);
        }
        addStartPreTag(result);
        addEncodedSourceCode(result, includeSourceCodeParser);
        addEndPreTag(result);

        return result;
    }

    private void addStartPreTag(List result) {
        result.add("
");
        result.add("
"); } private void addEncodedSourceCode(List result, IncludeSourceCodeParser includeSourceCodeParser) { List unFormattedSourceCode = readSourceFile(includeSourceCodeParser); HtmlEncoder htmlEncoder = new HtmlEncoder(); List formattedSourceCode = htmlEncoder.encode(unFormattedSourceCode); result.addAll(formattedSourceCode); } private void addEndPreTag(List result) { result.add("
"); result.add("
"); } private void addFileName(List result, IncludeSourceCodeParser includeSourceCodeParser) { String fileName = includeSourceCodeParser.getFileName(); String fileDisplayName = includeSourceCodeParser.getFileDisplayName(); if (fileDisplayName.length() > 0) { result.add("

" + fileDisplayName + "

"); } else { result.add("

" + fileName + "

"); } } private List readSourceFile(IncludeSourceCodeParser includeSourceCodeParser) { List sourceCode; String root = includeSourceCodeParser.getRoot(); String fileName = includeSourceCodeParser.getFileName(); sourceCode = sourceCodeReader.readFile(root, fileName); return sourceCode; } public void setSourceCodeReader(SourceCodeReader sourceCodeReader) { this.sourceCodeReader = sourceCodeReader; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy