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 java.net.URL;
 import java.util.List;
 import java.util.Map;

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

   
.
 
    private final LanguageSyntax languageSyntax;

   
.
 
    private final Integer indent;

   
.
 
    private final String content;

   
.
 
    private final WikletContext context;

   
.
 
    private final boolean highlightCode;
 
    public ProgramListingElement(
       WikletContext context,
       LanguageSyntax languageSyntax,
       Integer indent,
       String content,
       boolean highlightCode)
    {
       this. = context;
       this. = languageSyntax;
       this. = indent;
       this. = content;
       this. = highlightCode;
    }
 
    @Override
   public void writeTo(XMLEmitter xml)
   {
      ElementEmitter programListingCoXML = xml.element("programlistingco");
      //
      ElementEmitter areaspecXML = programListingCoXML.element("areaspec").withAttribute("units""linecolumn");
      //
      ElementEmitter programListingXML = programListingCoXML.element("programlisting");
      {
         programListingXML.withAttribute("language".name().toLowerCase());
      }
      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";
               }
            }
            programListingXML.content(biltotrue);
            break;
         case :
            programListingXML.content(biltotrue);
            break;
         case :
            CodeContextImpl ctx = new CodeContextImpl(areaspecXMLprogramListingXML);
            //
            new CodeProcessor().parse(biltoctx);
            //
            if (ctx.callouts.size() > 0)
            {
               ElementEmitter calloutListXML = programListingCoXML.element("calloutlist");
               for (Map.Entry<StringCalloutcallout : ctx.callouts.entrySet())
               {
                  if (callout.getValue(). != null)
                  {
                     StringBuffer sb = new StringBuffer();
                     for (String coId : callout.getValue().)
                     {
                        if (sb.length() > 0)
                        {
                           sb.append(" ");
                        }
                        sb.append(coId).append("-co");
                     }
                     calloutListXML.element("callout").
                        withAttribute("arearefs"sb.toString()).
                        element("para").content(callout.getValue().true);
                  }
               }
            }
            //
            break;
      }
   }
   private class CodeContextImpl implements CodeContext
   {

      
.
      private final StringBuilder sb = new StringBuilder();

      
.
      private XMLEmitter areaspecXML;

      
.
      private XMLEmitter programlistingXML;

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

      
.
      private final Random random = new Random();
      private CodeContextImpl(XMLEmitter areaspecXMLXMLEmitter programlistingXML)
      {
         this. = programlistingXML;
         this. = areaspecXML;
      }
      public void writeContent(String content)
      {
         .content(contenttrue);
         .append(content);
      }
      public void writeCallout(String id)
      {
         String coId = "" + Math.abs(.nextLong());
         //
         TextArea ta = new TextArea(.toString());
         Position pos = ta.position(.length());
         //
         .element("area").
            withAttribute("id"coId + "-co").
            withAttribute("linkends"coId).
            withAttribute("coords",(pos.getLine() + 1) + " " + (pos.getColumn() + 1));
         //
         Callout callout = .get(id);
         if (callout == null)
         {
            callout = new Callout();
            .put(idcallout);
         }
         //
         callout.ids.addLast(coId);
      }
      public void setCallout(String idString text)
      {
         Callout callout = .get(id);
         if (callout == null)
         {
            callout = new Callout();
            .put(idcallout);
         }
         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 LinkedList<Stringids = new LinkedList<String>();
   }
New to GrepCode? Check out our FAQ X