001/*
002 * Copyright c 2018 Rusi Popov, MDA Tools.net All rights reserved.
003 *
004 * This program and the accompanying materials are made available under the terms of the
005 * Eclipse Public License v2.0 which accompanies this distribution, and is available at
006 * http://www.eclipse.org/legal/epl-v20.html
007 */
008package net.mdatools.modelant.core.operation.format;
009
010import net.mdatools.modelant.core.api.Operation;
011
012/**
013 * Replace the < start-of-tag and > end-of-tag characters by corresponding HTML entities.
014 * @author Rusi Popov (popovr@mdatools.net)
015 */
016public class EscapeHtmlTags implements Operation<String> {
017
018  /**
019   * @see net.mdatools.modelant.core.api.Function#execute(java.lang.Object)
020   */
021  public String execute(String toEscape) throws RuntimeException, IllegalArgumentException {
022    StringBuilder result;
023    char character;
024
025    result = new StringBuilder();
026
027    for (int i=0; i< toEscape.length(); i++) {
028      character = toEscape.charAt(i);
029
030      if ( character == '<' ) {
031        result.append( "&lt;" );
032
033      } else if ( character == '>' ) {
034        result.append( "&gt;" );
035
036      } else { // the char is not a special one add it to the result as is
037        result.append( character );
038      }
039    }
040    return result.toString();
041  }
042}