KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > text > TextUtil


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.editor.text;
12
13 public abstract class TextUtil {
14
15     public static String JavaDoc createMultiLine(String JavaDoc text, int limit) {
16         return createMultiLine(text, limit, false);
17     }
18
19     public static String JavaDoc createMultiLine(String JavaDoc text, int limit, boolean ignoreNewLine) {
20         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
21         int counter = 0;
22         boolean preformatted = false;
23
24         for (int i = 0; i < text.length(); i++) {
25             char c = text.charAt(i);
26             counter++;
27             if (c == '<') {
28                 if (isPreStart(text, i)) {
29                     preformatted = true;
30                 } else if (isPreEnd(text, i)) {
31                     preformatted = false;
32                 } else if (isParagraph(text, i)) {
33                     buffer.append("\n<p>\n"); //$NON-NLS-1$
34
counter = 0;
35                     i += 2;
36                     continue;
37                 }
38             }
39             if (preformatted) {
40                 if (c == '\n')
41                     counter = 0;
42                 buffer.append(c);
43                 continue;
44             }
45             if (Character.isWhitespace(c)) {
46                 if (counter == 1) {
47                     counter = 0;
48                     continue; // skip
49
} else if (counter > limit) {
50                     buffer.append('\n');
51                     counter = 0;
52                     i--;
53                     continue;
54                 }
55             }
56             if (c == '\n') {
57                 if (ignoreNewLine)
58                     c = ' ';
59                 else
60                     counter = 0;
61             }
62             buffer.append(c);
63         }
64         return buffer.toString();
65     }
66
67     private static boolean isParagraph(String JavaDoc text, int loc) {
68         if (text.charAt(loc) != '<')
69             return false;
70         if (loc + 2 >= text.length())
71             return false;
72         if (text.charAt(loc + 1) != 'p')
73             return false;
74         if (text.charAt(loc + 2) != '>')
75             return false;
76         return true;
77     }
78
79     private static boolean isPreEnd(String JavaDoc text, int loc) {
80         if (text.charAt(loc) != '<')
81             return false;
82         if (loc + 5 >= text.length())
83             return false;
84         if (text.charAt(loc + 1) != '/')
85             return false;
86         if (text.charAt(loc + 2) != 'p')
87             return false;
88         if (text.charAt(loc + 3) != 'r')
89             return false;
90         if (text.charAt(loc + 4) != 'e')
91             return false;
92         if (text.charAt(loc + 5) != '>')
93             return false;
94         return true;
95     }
96
97     private static boolean isPreStart(String JavaDoc text, int loc) {
98         if (text.charAt(loc) != '<')
99             return false;
100         if (loc + 4 >= text.length())
101             return false;
102         if (text.charAt(loc + 1) != 'p')
103             return false;
104         if (text.charAt(loc + 2) != 'r')
105             return false;
106         if (text.charAt(loc + 3) != 'e')
107             return false;
108         if (text.charAt(loc + 4) != '>')
109             return false;
110         return true;
111     }
112 }
113
Popular Tags