1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.wiki.filters;
16  
17  import org.stringtree.factory.AbstractStringFetcher;
18  import org.stringtree.factory.Container;
19  import org.stringtree.factory.Fetcher;
20  import org.stringtree.juicer.Initialisable;
21  import org.stringtree.regex.Matcher;
22  import org.stringtree.regex.Pattern;
23  
24  /**
25   * <a href="MediaWikiLocalLink.java.html"><b><i>View Source</i></b></a>
26   *
27   * @author Raymond Augé
28   */
29  public class MediaWikiLocalLink
30      extends AbstractStringFetcher implements Initialisable {
31  
32      public MediaWikiLocalLink() {
33          this(null);
34      }
35  
36      public MediaWikiLocalLink(Container pages) {
37          _pages = pages;
38      }
39  
40      public void init(Fetcher context) {
41          _pages = (Container)context.getObject("wiki.pages");
42      }
43  
44      public Object getObject(String key) {
45          String value = key;
46  
47          Matcher match = _pattern.matcher(key);
48  
49          if (match.find()) {
50              key = encodeKey(match.group(3).trim());
51  
52              String title = match.group(6).trim();
53  
54              if (_pages.contains(key)) {
55                  value = "\nview_mw{" + title + "," + key + "}\n";
56              }
57              else{
58                  value = "\nedit_mw{" + title + "," + key + "}\n";
59              }
60          }
61  
62          return value;
63      }
64  
65      protected String encodeKey(String key) {
66          return key.replaceAll("\\W", "_");
67      }
68  
69      private static Pattern _pattern = Pattern.compile(
70          "(^|\\p{Punct}|\\p{Space})(\\[\\s*(((\\p{Lu}\\p{Ll}+)\\s?)+)\\s*\\|\\s*((([\\p{Alpha}\\p{Digit}]+)\\s?)+)\\s*\\])(\\z|\\n|\\p{Punct}|\\p{Space})");
71  
72      private Container _pages;
73  
74  }