1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.message.util;
\r
15 * @author Tuukka Lehtonen
\r
17 public final class TagUtil {
\r
20 * No operation, does nothing. Just a convenience for replacing a tag with
\r
23 * @param text the input
\r
24 * @return the specified input text
\r
26 public static String nop(String text) {
\r
30 public static String tag(String s, String tag) {
\r
34 StringBuilder b = new StringBuilder(s.length() + tag.length()*2 + 6);
\r
42 return b.toString();
\r
48 * @param attrs attribute+value pairs
\r
51 public static String tag(String s, String tag, String...attrs) {
\r
52 assert (attrs.length % 2 == 0);
\r
53 StringBuilder b = new StringBuilder(s.length() + tag.length()*2 + 30);
\r
56 for (int i = 0; i < attrs.length; i+=2) {
\r
60 b.append(attrs[i+1]);
\r
68 return b.toString();
\r
73 private static boolean isEnclosed(String s, String tag) {
\r
74 String trim = s.trim();
\r
75 int tagLen = tag.length();
\r
76 int len = trim.length();
\r
77 boolean startTag = (len >= tag.length() + 2) && (trim.charAt(0) == '<') && trim.startsWith(tag, 1) && (trim.charAt(1+tagLen) == '>');
\r
78 boolean endTag = (len >= tag.length()*2 + 5) && trim.startsWith("</", len - 1 - tagLen - 2) && trim.startsWith(tag, 1) && (trim.charAt(len - 1) == '>');
\r
79 return startTag && endTag;
\r
82 public static void main(String[] args) {
\r
83 assertTrue(isEnclosed("form", "form") == false);
\r
84 assertTrue(isEnclosed("<form></form>", "form") == true);
\r
85 assertTrue(isEnclosed("<form> </form>", "form") == true);
\r
86 assertTrue(isEnclosed(" <form></form>", "form") == true);
\r
87 assertTrue(isEnclosed("<form></form> ", "form") == true);
\r
88 assertTrue(isEnclosed(" <form></form> ", "form") == true);
\r
89 assertTrue(isEnclosed("<form>FOO</form>", "form") == true);
\r
90 assertTrue(isEnclosed("<form> FOO </form>", "form") == true);
\r
93 private static void assertTrue(boolean b) {
\r
95 throw new AssertionError(false);
\r