X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.message%2Fsrc%2Forg%2Fsimantics%2Fmessage%2Futil%2FTagUtil.java;h=12f57c879e942ec6dbdb6cbb34c3ba970b1c68d6;hb=refs%2Fchanges%2F38%2F238%2F2;hp=f805e2cd7a1acc7c2cdab833f74b1ae9bc2ca80a;hpb=24e2b34260f219f0d1644ca7a138894980e25b14;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.message/src/org/simantics/message/util/TagUtil.java b/bundles/org.simantics.message/src/org/simantics/message/util/TagUtil.java index f805e2cd7..12f57c879 100644 --- a/bundles/org.simantics.message/src/org/simantics/message/util/TagUtil.java +++ b/bundles/org.simantics.message/src/org/simantics/message/util/TagUtil.java @@ -1,98 +1,98 @@ -/******************************************************************************* - * Copyright (c) 2007, 2010 Association for Decentralized Information Management - * in Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.message.util; - -/** - * @author Tuukka Lehtonen - */ -public final class TagUtil { - - /** - * No operation, does nothing. Just a convenience for replacing a tag with - * no tag in code. - * - * @param text the input - * @return the specified input text - */ - public static String nop(String text) { - return text; - } - - public static String tag(String s, String tag) { - if (tag == null) - return s; - - StringBuilder b = new StringBuilder(s.length() + tag.length()*2 + 6); - b.append('<'); - b.append(tag); - b.append('>'); - b.append(s); - b.append("'); - return b.toString(); - } - - /** - * @param s - * @param tag - * @param attrs attribute+value pairs - * @return - */ - public static String tag(String s, String tag, String...attrs) { - assert (attrs.length % 2 == 0); - StringBuilder b = new StringBuilder(s.length() + tag.length()*2 + 30); - b.append('<'); - b.append(tag); - for (int i = 0; i < attrs.length; i+=2) { - b.append(' '); - b.append(attrs[i]); - b.append("=\""); - b.append(attrs[i+1]); - b.append('"'); - } - b.append('>'); - b.append(s); - b.append("'); - return b.toString(); - } - - // NEVER MIND THESE - - private static boolean isEnclosed(String s, String tag) { - String trim = s.trim(); - int tagLen = tag.length(); - int len = trim.length(); - boolean startTag = (len >= tag.length() + 2) && (trim.charAt(0) == '<') && trim.startsWith(tag, 1) && (trim.charAt(1+tagLen) == '>'); - boolean endTag = (len >= tag.length()*2 + 5) && trim.startsWith("'); - return startTag && endTag; - } - - public static void main(String[] args) { - assertTrue(isEnclosed("form", "form") == false); - assertTrue(isEnclosed("
", "form") == true); - assertTrue(isEnclosed("
", "form") == true); - assertTrue(isEnclosed("
", "form") == true); - assertTrue(isEnclosed("
", "form") == true); - assertTrue(isEnclosed("
", "form") == true); - assertTrue(isEnclosed("
FOO
", "form") == true); - assertTrue(isEnclosed("
FOO
", "form") == true); - } - - private static void assertTrue(boolean b) { - if (b == false) - throw new AssertionError(false); - } - -} +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.message.util; + +/** + * @author Tuukka Lehtonen + */ +public final class TagUtil { + + /** + * No operation, does nothing. Just a convenience for replacing a tag with + * no tag in code. + * + * @param text the input + * @return the specified input text + */ + public static String nop(String text) { + return text; + } + + public static String tag(String s, String tag) { + if (tag == null) + return s; + + StringBuilder b = new StringBuilder(s.length() + tag.length()*2 + 6); + b.append('<'); + b.append(tag); + b.append('>'); + b.append(s); + b.append("'); + return b.toString(); + } + + /** + * @param s + * @param tag + * @param attrs attribute+value pairs + * @return + */ + public static String tag(String s, String tag, String...attrs) { + assert (attrs.length % 2 == 0); + StringBuilder b = new StringBuilder(s.length() + tag.length()*2 + 30); + b.append('<'); + b.append(tag); + for (int i = 0; i < attrs.length; i+=2) { + b.append(' '); + b.append(attrs[i]); + b.append("=\""); + b.append(attrs[i+1]); + b.append('"'); + } + b.append('>'); + b.append(s); + b.append("'); + return b.toString(); + } + + // NEVER MIND THESE + + private static boolean isEnclosed(String s, String tag) { + String trim = s.trim(); + int tagLen = tag.length(); + int len = trim.length(); + boolean startTag = (len >= tag.length() + 2) && (trim.charAt(0) == '<') && trim.startsWith(tag, 1) && (trim.charAt(1+tagLen) == '>'); + boolean endTag = (len >= tag.length()*2 + 5) && trim.startsWith("'); + return startTag && endTag; + } + + public static void main(String[] args) { + assertTrue(isEnclosed("form", "form") == false); + assertTrue(isEnclosed("
", "form") == true); + assertTrue(isEnclosed("
", "form") == true); + assertTrue(isEnclosed("
", "form") == true); + assertTrue(isEnclosed("
", "form") == true); + assertTrue(isEnclosed("
", "form") == true); + assertTrue(isEnclosed("
FOO
", "form") == true); + assertTrue(isEnclosed("
FOO
", "form") == true); + } + + private static void assertTrue(boolean b) { + if (b == false) + throw new AssertionError(false); + } + +}