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.xwiki;
 
 import  org.xwiki.component.embed.EmbeddableComponentManager;
 import  org.xwiki.component.manager.ComponentLookupException;
 import  org.xwiki.rendering.block.Block;
 import  org.xwiki.rendering.block.HeaderBlock;
 import  org.xwiki.rendering.block.MacroBlock;
 import  org.xwiki.rendering.block.SectionBlock;
 import  org.xwiki.rendering.block.VerbatimBlock;
 import  org.xwiki.rendering.block.WordBlock;
 import  org.xwiki.rendering.block.XDOM;
 import  org.xwiki.rendering.listener.HeaderLevel;
 import  org.xwiki.rendering.parser.ParseException;
 import  org.xwiki.rendering.parser.Parser;
 import  org.xwiki.rendering.renderer.BlockRenderer;
 import  org.xwiki.rendering.renderer.printer.DefaultWikiPrinter;
 import  org.xwiki.rendering.renderer.printer.WikiPrinter;
 import  org.xwiki.rendering.syntax.Syntax;
 
 import java.io.Reader;
 import java.net.URL;
 import java.util.List;

Author(s):
Julien Viet
Version:
$Revision$
 
 class WikiLoader
 {

   
.
 
    private final WikletContext context;

   
.
 
    private final EmbeddableComponentManager ecm;
 
    public WikiLoader(WikletContext contextthrows WikbookException
    {
       EmbeddableComponentManager ecm = new EmbeddableComponentManager();
       ecm.initialize(Thread.currentThread().getContextClassLoader());
 
       //
       this. = context;
       this. = ecm;
    }
 
    public Block load(String idString syntaxIdthrows WikbookException
    {
       return load(idsyntaxId, 0);
    }
 
    public Block load(Reader readerString syntaxIdthrows WikbookException
    {
       return load(readersyntaxId, 0);
    }
 
    private Block load(String idString syntaxIdint baseLevelthrows WikbookException
    {
       try
       {
          Reader reader = _load(id);
          return load(readersyntaxIdbaseLevel);
       }
       catch (IOException e)
       {
          throw new WikbookException(e);
       }
    }
 
    private Block load(Reader readerString syntaxIdint baseLevelthrows WikbookException
    {
       try
      {
         if (syntaxId == null)
         {
            syntaxId = Syntax.XWIKI_2_0.toIdString();
         }
         //
         XDOM xdom;
         if ("verbatim".equals(syntaxId))
         {
            xdom = new XDOM(new ArrayList<Block>());
            xdom.addChild(new VerbatimBlock(Utils.read(reader), false));
         }
         else
         {
            // Get parser
            Parser parser = .lookup(Parser.classsyntaxId);
            // Parse
            xdom = parser.parse(reader);
            // Replace all headers
            if (baseLevel > 0)
            {
               for (HeaderBlock header : xdom.getChildrenByType(HeaderBlock.classtrue))
               {
                  int l = baseLevel + header.getLevel().getAsInt();
                  HeaderBlock nheader = new HeaderBlock(
                     header.getChildren(),
                     HeaderLevel.parseInt(l),
                     header.getParameters(),
                     header.getId()
                  );
                  header.getParent().replaceChild(nheaderheader);
               }
            }
            //
            List<Substitutionsubstitutions = visit(xdomsyntaxId);
            //
            if (substitutions.size() > 0)
            {
               for (Substitution substitution : substitutions)
               {
                  substitution.src.getParent().replaceChild(substitution.dstsubstitution.src);
               }
               WikiPrinter printer = new DefaultWikiPrinter();
               BlockRenderer substitutionRenderer = .lookup(BlockRenderer.class, Syntax.XWIKI_2_0.toIdString());
               substitutionRenderer.render(xdomprinter);
               Parser substitutionParser = .lookup(Parser.class, Syntax.XWIKI_2_0.toIdString());
               xdom = substitutionParser.parse(new StringReader(printer.toString()));
            }
         }
         // Returns include block instead of XDOM
         return new IncludeBlock(syntaxIdxdom.getChildren());
      }
      catch (IOException e)
      {
         throw new WikbookException(e);
      }
      catch (ComponentLookupException e)
      {
         throw new WikbookException(e);
      }
      catch (ParseException e)
      {
         throw new WikbookException(e);
      }
   }

   
Load the document with the specified id.

Parameters:
id the document id
Returns:
a reader for the document
Throws:
IOException any io exception
   private Reader _load(String idthrows IOException
   {
      URL main = .resolveResource(.id);
      if (main == null)
      {
         throw new IOException("Could not load wiki document: " + id);
      }
      return Utils.read(main);
   }

   
Visit a block and returns the list of substitutions that must be performed in the visited block. The returned list can be safely modified.

Parameters:
block the block to visit
Returns:
the substitution list
   private List<Substitutionvisit(Block blockString currentSyntaxId)
   {
      return visit(block, 0, currentSyntaxId);
   }
   private List<Substitutionvisit(Block blockint levelString currentSyntaxId)
   {
      if (block instanceof MacroBlock)
      {
         MacroBlock macro = (MacroBlock)block;
         String id = macro.getId();
         if ("include".equals(id))
         {
            String includedId = macro.getParameter("document");
            String syntaxId = macro.getParameter("syntax");
            if (syntaxId == null)
            {
               syntaxId = currentSyntaxId;
            }
            LinkedList<Substitutionlist = new LinkedList<Substitution>();
            list.add(new Substitution(blockload(includedIdsyntaxIdlevel)));
            return list;
         }
         else if (id.startsWith("property."))
         {
            String name = id.substring("property.".length());
            if (name != null)
            {
               String value = .getProperty(name);
               if (value != null)
               {
                  LinkedList<Substitutionlist = new LinkedList<Substitution>();
                  list.add(new Substitution(blocknew WordBlock(value)));
                  return list;
               }
            }
         }
      }
      else if (block instanceof SectionBlock)
      {
         level++;
      }
      //
      List<Substitutionsubstitutions = new LinkedList<Substitution>();
      for (Block child : block.getChildren())
      {
         List<SubstitutionchildSubstitutions = visit(childlevelcurrentSyntaxId);
         if (childSubstitutions.size() > 0)
         {
            substitutions.addAll(childSubstitutions);
         }
      }
      //
      return substitutions;
   }
New to GrepCode? Check out our FAQ X