| // Copyright 2008 Google Inc. |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| package com.google.gerrit.client.ui; |
| |
| import com.google.gwt.core.client.GWT; |
| |
| /** Utilities for dealing with the DOM. */ |
| public abstract class DomUtil { |
| private static final Impl INSTANCE; |
| |
| static { |
| if (GWT.isClient()) |
| INSTANCE = new ClientImpl(); |
| else |
| INSTANCE = new JavaImpl(); |
| } |
| |
| /** Escape XML/HTML special characters in the input string. */ |
| public static String escape(final String in) { |
| return INSTANCE.escape(in); |
| } |
| |
| /** Convert bare URLs into <a href> tags. */ |
| public static String linkify(final String in) { |
| return INSTANCE.linkify(in); |
| } |
| |
| private DomUtil() { |
| } |
| |
| private static abstract class Impl { |
| abstract String escape(String in); |
| |
| String linkify(String in) { |
| return in.replaceAll("(https?://[^ \n\r\t]*)", "<a href=\"$1\">$1</a>"); |
| } |
| } |
| |
| private static class ClientImpl extends Impl { |
| @Override |
| native String escape(String src)/*-{ return src.replace(/&/g,'&').replace(/>/g,'>').replace(/</g,'<').replace(/"/g,'"'); }-*/; |
| } |
| |
| private static class JavaImpl extends Impl { |
| @Override |
| String escape(final String in) { |
| final StringBuilder r = new StringBuilder(in.length()); |
| for (int i = 0; i < in.length(); i++) { |
| final char c = in.charAt(i); |
| switch (c) { |
| case '&': |
| r.append("&"); |
| break; |
| case '>': |
| r.append(">"); |
| break; |
| case '<': |
| r.append("<"); |
| break; |
| case '"': |
| r.append("""); |
| break; |
| default: |
| r.append(c); |
| } |
| } |
| return r.toString(); |
| } |
| } |
| } |