Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2010 eXo Platform SAS.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
   * the License, or (at your option) any later version.
   *
   * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 
 package org.wikbook.core.model.content.block;
 
 import  org.xwiki.rendering.block.XDOM;
 
 import java.net.URL;
 import java.util.List;
 import java.util.Map;

Author(s):
Julien Viet
Version:
$Revision$
 
 public class ProgramListingElement extends BlockElement
 {

   
.
 
    private final ElementContainer<CalloutElementcallouts;

   
.
 
    private final LanguageSyntax languageSyntax;

   
.
 
    private final Integer indent;

   
.
 
    private final String content;

   
.
 
    private final WikletContext context;

   
.
 
    private final boolean highlightCode;

   
.
 
    private String listing;
 
    public ProgramListingElement(
       WikletContext context,
       LanguageSyntax languageSyntax,
       Integer indent,
       String content,
       boolean highlightCode)
    {
       this. = context;
       this. = languageSyntax;
       this. = indent;
       this. = content;
       this. = highlightCode;
       this. = new ElementContainer<CalloutElement>(CalloutElement.class);
    }
 
    public void process(XDOMTransformer _transformer)
   {
      String bilto;
      switch ()
      {
         case :
            if ( != null)
            {
               try
               {
                  // Output buffer
                  StringWriter writer = new StringWriter();
                  // Create transformer handler
                  Transformer transformer = XML.createTransformer(new OutputFormat(true));
                  // Surround with an element to take care of special case
                  String data = "<root>" +  + "</root>";
                  //
                  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                  dbf.setIgnoringElementContentWhitespace(true);
                  dbf.setCoalescing(true);
                  DocumentBuilder db = dbf.newDocumentBuilder();
                  Document doc = db.parse(new InputSource(new StringReader(data)));
                  // Remove white spaces
                  XML.removeWhiteSpace(doc.getDocumentElement());
                  //
                  transformer.transform(new DOMSource(doc), new StreamResult(writer));
                  // Get the original data
                  data = writer.toString();
                  int from = data.indexOf("<root>\n") + "<root>\n".length();
                  int to  = data.lastIndexOf("\n</root>");
                  data = data.substring(fromto);
                  //
                  TextArea ta = new TextArea(data);
                  ta.trimLeft();
                  //
                  bilto = ta.getText();
               }
               catch (Exception e)
               {
                  e.printStackTrace();
                  bilto = "Exception occured, see logs";
               }
            }
            else
            {
               bilto = ;
            }
            break;
         case :
            CodeContextImpl ctx = new CodeContextImpl();
            //
            new CodeProcessor().parse(ctx);
            // Create the resulting callouts
            for (Map.Entry<StringCalloutcallout : ctx.callouts.entrySet()) {
               if (callout.getValue(). != null)
               {
                  CalloutElement calloutElt = new CalloutElement(callout.getValue().callout.getValue().);
                  push(calloutElt);
                  WikiLoader loader = new WikiLoader();
                  XDOM dom = loader.load(new StringReader(callout.getValue().), null);
                  dom.traverse(_transformer);
                  
                  merge();
               }
            }
            //
            bilto = ctx.sb.toString();
            //
            break;
         default:
            bilto = ;
            break;
      }
      //
      this. = bilto;
   }
   public boolean append(DocbookElement elt)
   {
      return .append(elt);
   }
   public void writeTo(XMLEmitter xml)
   {
      ElementEmitter programListingCoXML = xml.element("programlistingco");
      //
      ElementEmitter areaspecXML = programListingCoXML.element("areaspec").withAttribute("units""linecolumn");
      //
      for (CalloutElement calloutElt : )
      {
         for (Map.Entry<StringPositiontarget : calloutElt.getIds().entrySet())
         {
            areaspecXML.element("area").
               withAttribute("id"target.getKey() + "-co").
               withAttribute("linkends"target.getKey()).
               withAttribute("coords",(target.getValue().getLine() + 1) + " " + (target.getValue().getColumn() + 1));
         }
      }
      //
      ElementEmitter programListingXML = programListingCoXML.element("programlisting");
      {
         programListingXML.withAttribute("language".name().toLowerCase());
      }
      //
      if (.isNotEmpty())
      {
         .writeTo(programListingCoXML.element("calloutlist"));
      }
      //
      programListingXML.content(true);
   }
   private class CodeContextImpl implements CodeContext
   {

      
.
      private final StringBuilder sb = new StringBuilder();

      
.
      private final TreeMap<StringCalloutcallouts = new TreeMap<StringCallout>();

      
.
      private final Random random = new Random();
      private CodeContextImpl()
      {
      }
      public void writeContent(String content)
      {
         .append(content);
      }
      public void writeCallout(String index)
      {
         String coId = "" + Math.abs(.nextLong());
         //
         TextArea ta = new TextArea(.toString());
         Position pos = ta.position(.length());
         //
         Callout callout = .get(index);
         if (callout == null)
         {
            callout = new Callout();
            .put(indexcallout);
         }
         // Write a white space so we are sure that the position for the callout exist in the text
         writeContent(" ");
         //
         callout.ids.put(coIdpos);
      }
      public void setCallout(String indexString text)
      {
         Callout callout = .get(index);
         if (callout == null)
         {
            callout = new Callout();
            .put(indexcallout);
         }
         callout.text = text;
      }
      public InputStream resolveResources(String idthrows IOException
      {
         List<URLlist = .resolveResources(.id);
         if (list.size() > 0)
         {
            return list.get(0).openStream();
         }
         return null;
      }
   }
   private static class Callout
   {

      
.
      private String text;

      
.
      private final LinkedHashMap<StringPositionids = new LinkedHashMap<StringPosition>();
   }
New to GrepCode? Check out our FAQ X