Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2012 astamuse company,Ltd.
   *
   * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
   *
  * 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 com.astamuse.asta4d.sample.snippet;
 
 import java.util.List;
 
 
 
 
 public class ShowCodeSnippet {
     private static final String JAVA_PACKAGE = "/com/astamuse/asta4d/sample";
     private static final String VM_ARG = "asta4d.sample.source_location";
     private static final String SHOW_MARK = "@ShowCode:";
 
     public Renderer showCode(HttpServletRequest requestString fileString startMarkString endMarkString title) {
         Renderer render = new GoThroughRenderer();
         ServletContext servletContext = request.getSession().getServletContext();
         String contents = readFileByLines(servletContextfile + startMark + endMark);
         render.add("div"makeShowHtml(filetitlecontents));
 
         return render;
     }
 
     private Element makeShowHtml(String fileString titleString contents) {
         // create the pre tag
         Element pre = new Element(Tag.valueOf("pre"), "");
         pre.addClass("prettyprint source");
         pre.attr("style""overflow-x:auto");
         List<NodepreChildren = new ArrayList<>();
         if (contents != null) {
             preChildren.add(new Element(Tag.valueOf("span"), "").appendText(contents));
         }
         ElementUtil.appendNodes(prepreChildren);
 
         // create the article tag
         Element article = new Element(Tag.valueOf("article"), "");
         List<NodearticleChildren = new ArrayList<>();
         if (title == null) {
             if (file != null) {
                 articleChildren.add(new Element(Tag.valueOf("div"), "").appendText(file));
             }
         } else {
             articleChildren.add(new Element(Tag.valueOf("div"), "").appendText(title));
         }
         articleChildren.add(pre);
         ElementUtil.appendNodes(articlearticleChildren);
 
         // create the section tag
         Element section = new Element(Tag.valueOf("section"), "");
         List<NodesectionChildren = new ArrayList<>();
         sectionChildren.add(article);
         ElementUtil.appendNodes(sectionsectionChildren);
 
         return section;
     }
 
     private static String readFileByLines(ServletContext servletContextString fileNameString startMarkString endMark) {
         String filePath = "";
         InputStream inputStream = null;
         BufferedReader reader = null;
         try {
             // read the file
             if (fileName.endsWith(".java")) {
                 String source_location = System.getProperty();
                 if (source_location != null) {
                     filePath = source_location +  + fileName;
                     inputStream = new FileInputStream(filePath);
                 } else {
                     filePath = "/WEB-INF/src" +  + fileName;
                     inputStream = servletContext.getResourceAsStream(filePath);
                }
            } else {
                inputStream = servletContext.getResourceAsStream(fileName);
            }
            if (inputStream == null) {
                return null;
            }
            // find the line that has the mark
            reader = new BufferedReader(new InputStreamReader(inputStream"utf-8"));
            String line = null;
            String contents = "";
            boolean isMark = false;
            while ((line = reader.readLine()) != null) {
                if (line.contains(endMark)) {
                    isMark = false;
                }
                if (isMark && !line.contains()) {
                    contents = contents + line + "\n";
                }
                if (line.contains(startMark)) {
                    isMark = true;
                }
            }
            return contents;
        } catch (IOException e) {
            return null;
            // close
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e1) {
                }
            }
        }
    }
New to GrepCode? Check out our FAQ X