1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.message.util;
15 * @author Tuukka Lehtonen
17 public final class TagUtil {
20 * No operation, does nothing. Just a convenience for replacing a tag with
23 * @param text the input
24 * @return the specified input text
26 public static String nop(String text) {
30 public static String tag(String s, String tag) {
34 StringBuilder b = new StringBuilder(s.length() + tag.length()*2 + 6);
48 * @param attrs attribute+value pairs
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);
56 for (int i = 0; i < attrs.length; i+=2) {
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;
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);
93 private static void assertTrue(boolean b) {
95 throw new AssertionError(false);