View Javadoc

1   
2   /*
3    * Copyright 2011 The Kuali Foundation
4    *
5    * Licensed under the Educational Community License, Version 1.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.opensource.org/licenses/ecl1.php
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.kuali.student.r2.common.util;
18  
19  import org.kuali.student.r2.common.dto.RichTextInfo;
20  
21  /**
22   * Utility to convert text between plain and formatted
23   * @author nwright
24   */
25  public class RichTextHelper {
26  
27      public static RichTextInfo buildRichTextInfo(String plain, String formatted) {
28          if (plain == null) {
29              return null;
30          }
31          RichTextInfo info = new RichTextInfo();
32          info.setFormatted(formatted);
33          info.setPlain(plain);
34          return info;
35      }
36  
37      public RichTextInfo toRichTextInfo(String plain, String formatted) {
38          if (plain == null) {
39              return null;
40          }
41          RichTextInfo info = new RichTextInfo();
42          info.setFormatted(formatted);
43          info.setPlain(plain);
44          return info;
45      }
46  
47      public RichTextInfo fromFormatted(String plain) {
48          return toRichTextInfo(plain, formatted2Plain(plain));
49      }
50  
51      public RichTextInfo fromPlain(String plain) {
52          return toRichTextInfo(plain, plain2Formatted(plain));
53      }
54  
55      public String formatted2Plain(String forematted) {
56          if (forematted == null) {
57              return null;
58          }
59          // TODO: actually implement a real conversion
60          return forematted.replaceAll("<br>", "\n");
61      }
62  
63      public String plain2Formatted(String plain) {
64          if (plain == null) {
65              return null;
66          }
67          // TODO: actually implement a real conversion
68          return plain.replaceAll("\n", "<br>");
69      }
70  }