View Javadoc
1   /*
2    * #%L
3    * JRst :: Api
4    * %%
5    * Copyright (C) 2004 - 2010 CodeLutin
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 3 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20   * #L%
21   */
22  
23  package org.nuiton.jrst.legacy.directive;
24  
25  import org.nuiton.jrst.JRSTDirective;
26  import org.nuiton.jrst.legacy.JRSTLexer;
27  import static org.nuiton.jrst.legacy.ReStructuredText.IMAGE;
28  import static org.nuiton.jrst.legacy.ReStructuredText.SUBSTITUTION_DEFINITION;
29  import org.dom4j.DocumentHelper;
30  import org.dom4j.Element;
31  import org.dom4j.Node;
32  
33  import java.util.regex.Matcher;
34  import java.util.regex.Pattern;
35  
36  /**
37   * .. image:: picture.jpeg :height: 100 :width: 200 :scale: 50 :alt: alternate
38   * text :align: right
39   * 
40   * Created: 4 nov. 06 12:52:02
41   *
42   * @author poussin
43   * @version $Revision$
44   *
45   * Last update: $Date$
46   * by : $Author$
47   */
48  public class ImageDirective implements JRSTDirective {
49  
50      protected static final String SCALE = "scale";
51      protected static final String WIDTH = "width";
52      protected static final String HEIGHT = "height";
53  
54      /*
55       * (non-Javadoc)
56       * 
57       * @see org.nuiton.jrst.JRSTDirective#parse(org.dom4j.Element)
58       */
59      @Override
60      public Node parse(Element e) {
61          Element result = DocumentHelper.createElement(IMAGE);
62  
63          if (e.getParent() != null
64                  && SUBSTITUTION_DEFINITION.equals(e.getParent().getName())) {
65              String ref = e.getParent().attributeValue("name");
66              result.addAttribute("alt", ref);
67          }
68          result.addAttribute("uri", e.attributeValue(JRSTLexer.DIRECTIVE_VALUE));
69  
70          Pattern arg = Pattern.compile(":([^:]+):\\s*(.*)");
71          String[] lines = e.getText().split("\n");
72          for (String l : lines) {
73              Matcher matcher = arg.matcher(l.trim());
74              if (matcher.matches()) {
75                  String name = matcher.group(1);
76                  String value = matcher.group(2);
77                  if (SCALE.equalsIgnoreCase(name)) {
78                      if (!result.asXML().matches(".*" + WIDTH + "=\".*\".*")) {
79                          result.addAttribute(WIDTH, value + (value.matches(".*%") ? "" : "%"));
80                      }
81                      if (!result.asXML().matches(".*" + HEIGHT + "=\".*\".*")) {
82                          result.addAttribute(HEIGHT, value + (value.matches(".*%") ? "" : "%"));
83                      }
84                  }
85                  result.addAttribute(name, value);
86              }
87          }
88          return result;
89      }
90  
91  }