1 /*
2  * JasperReports - Free Java Reporting Library.
3  * Copyright (C) 2001 - 2019 TIBCO Software Inc. All rights reserved.
4  * http://www.jaspersoft.com
5  *
6  * Unless you have purchased a commercial license agreement from Jaspersoft,
7  * the following license terms apply:
8  *
9  * This program is part of JasperReports.
10  *
11  * JasperReports is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * JasperReports is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
23  */

24 package net.sf.jasperreports.engine.util;
25
26 import java.text.AttributedCharacterIterator.Attribute;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
30
31 import javax.swing.JEditorPane;
32 import javax.swing.text.BadLocationException;
33 import javax.swing.text.Document;
34 import javax.swing.text.Element;
35 import javax.swing.text.AbstractDocument.LeafElement;
36
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39
40
41 /**
42  * @author Teodor Danciu (teodord@users.sourceforge.net)
43  */

44 public class JEditorPaneRtfMarkupProcessor extends JEditorPaneMarkupProcessor
45 {
46     private static final Log log = LogFactory.getLog(JEditorPaneRtfMarkupProcessor.class);
47
48     private static JEditorPaneRtfMarkupProcessor instance;  
49     
50     /**
51      * 
52      */

53     public static JEditorPaneRtfMarkupProcessor getInstance()
54     {
55         if (instance == null)
56         {
57             instance = new JEditorPaneRtfMarkupProcessor();
58         }
59         return instance;
60     }
61     
62     @Override
63     public String convert(String srcText)
64     {
65         JEditorPane editorPane = new JEditorPane("text/rtf", srcText);
66         editorPane.setEditable(false);
67
68         List<Element> elements = new ArrayList<Element>();
69
70         Document document = editorPane.getDocument();
71
72         Element root = document.getDefaultRootElement();
73         if (root != null)
74         {
75             addElements(elements, root);
76         }
77
78         String chunk = null;
79         Element element = null;
80         int startOffset = 0;
81         int endOffset = 0;
82         
83         JRStyledText styledText = new JRStyledText();
84         styledText.setGlobalAttributes(new HashMap<Attribute,Object>());
85         for(int i = 0; i < elements.size(); i++)
86         {
87             if (chunk != null)
88             {
89                 styledText.append(chunk);
90                 styledText.addRun(new JRStyledText.Run(getAttributes(element.getAttributes()), startOffset, endOffset));
91             }
92
93             chunk = null;
94             element = elements.get(i);
95             startOffset = element.getStartOffset();
96             endOffset = element.getEndOffset();
97
98             try
99             {
100                 chunk = document.getText(startOffset, endOffset - startOffset);
101             }
102             catch(BadLocationException e)
103             {
104                 if (log.isDebugEnabled())
105                 {
106                     log.debug("Error converting markup.", e);
107                 }
108             }
109
110         }
111
112         if (chunk != null && !"\n".equals(chunk))
113         {
114             styledText.append(chunk);
115             styledText.addRun(new JRStyledText.Run(getAttributes(element.getAttributes()), startOffset, endOffset));
116         }
117
118         return JRStyledTextParser.getInstance().write(styledText);
119     }
120     
121     /**
122      * 
123      */

124     protected void addElements(List<Element> elements, Element element) 
125     {
126         if(element instanceof LeafElement)
127         {
128             elements.add(element);
129         }
130         for(int i = 0; i < element.getElementCount(); i++)
131         {
132             Element child = element.getElement(i);
133             addElements(elements, child);
134         }
135     }
136     
137 }
138