]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.message/src/org/simantics/message/util/TagUtil.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.message / src / org / simantics / message / util / TagUtil.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.message.util;
13
14 /**
15  * @author Tuukka Lehtonen
16  */
17 public final class TagUtil {
18
19     /**
20      * No operation, does nothing. Just a convenience for replacing a tag with
21      * no tag in code.
22      * 
23      * @param text the input
24      * @return the specified input text
25      */
26     public static String nop(String text) {
27         return text;
28     }
29
30     public static String tag(String s, String tag) {
31         if (tag == null)
32             return s;
33
34         StringBuilder b = new StringBuilder(s.length() + tag.length()*2 + 6);
35         b.append('<');
36         b.append(tag);
37         b.append('>');
38         b.append(s);
39         b.append("</");
40         b.append(tag);
41         b.append('>');
42         return b.toString();
43     }
44     
45     /**
46      * @param s
47      * @param tag
48      * @param attrs attribute+value pairs 
49      * @return
50      */
51     public static String tag(String s, String tag, String...attrs) {
52         assert (attrs.length % 2 == 0);
53         StringBuilder b = new StringBuilder(s.length() + tag.length()*2 + 30);
54         b.append('<');
55         b.append(tag);
56         for (int i = 0; i < attrs.length; i+=2) {
57             b.append(' ');
58             b.append(attrs[i]);
59             b.append("=\"");
60             b.append(attrs[i+1]);
61             b.append('"');
62         }
63         b.append('>');
64         b.append(s);
65         b.append("</");
66         b.append(tag);
67         b.append('>');
68         return b.toString();
69     }
70
71     // NEVER MIND THESE
72
73     private static boolean isEnclosed(String s, String tag) {
74         String trim = s.trim();
75         int tagLen = tag.length();
76         int len = trim.length();
77         boolean startTag = (len >= tag.length() + 2) && (trim.charAt(0) == '<') && trim.startsWith(tag, 1) && (trim.charAt(1+tagLen) == '>');
78         boolean endTag = (len >= tag.length()*2 + 5) && trim.startsWith("</", len - 1 - tagLen - 2) && trim.startsWith(tag, 1) && (trim.charAt(len - 1) == '>');
79         return startTag && endTag;
80     }
81
82     public static void main(String[] args) {
83         assertTrue(isEnclosed("form", "form") == false);
84         assertTrue(isEnclosed("<form></form>", "form") == true);
85         assertTrue(isEnclosed("<form>    </form>", "form") == true);
86         assertTrue(isEnclosed("   <form></form>", "form") == true);
87         assertTrue(isEnclosed("<form></form>   ", "form") == true);
88         assertTrue(isEnclosed("   <form></form>   ", "form") == true);
89         assertTrue(isEnclosed("<form>FOO</form>", "form") == true);
90         assertTrue(isEnclosed("<form>   FOO </form>", "form") == true);
91     }
92
93     private static void assertTrue(boolean b) {
94         if (b == false)
95             throw new AssertionError(false);
96     }
97
98 }