Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*******************************************************************************
 *  Copyright (c) 2006, 2012 IBM Corporation and others.
 *  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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    private static void processTagExceptionCharacters(HashMap<?, ?> substituteChars, StringBuffer buffer,
            String text) {
        // Get the tag name
        String tagName = getTagName(text);
        // Determine if there is a trailing forward slash
        boolean trailingSlash = text.endsWith("/"); //$NON-NLS-1$
        // Extract the attribute list of the element tag content
        // It may contain trailing spaces or a trailing '/'
        String attributeList = text.substring(tagName.length());
        // If the attribute list is not valid, discard the attribute list
        if ((isValidTagAttributeList(attributeList) == false)) {
            buffer.append('<');
            buffer.append(tagName);
            // Since, this tag has an attribute list and we are discarding it,
            // we have to make sure to replace the trailing slash if it had one
            if (trailingSlash) {
                buffer.append('/');
            }
            buffer.append('>');
            return;
        } else if (attributeList.length() == 0) {
            // If the tag has no attribute list then just return the tag
            // as is (trailing slash is already including in the tag name)
            buffer.append('<');
            buffer.append(tagName);
            buffer.append('>');
            return;
        }
        boolean inQuote = false;
        // Append the opening element bracket
        buffer.append('<');
        // Traverse the tag element content character by character
        // Translate any substitution characters required only inside attribute
        // value double-quotes
        for (int i = 0; i < text.length(); i++) {
            boolean processed = false;
            char currentChar = text.charAt(i);
            boolean onQuote = (currentChar == '"');
            // Determine whether we are currently processing the quote character
            if (onQuote) {
                if (inQuote) {
                    // Quote encountered is an end quote
                    inQuote = false;
                } else {
                    // Quote encountered is a begin quote
                    inQuote = true;
                }
            }
            // If we are currently within an attribute value double-quotes and
            // not on a quote character, translate this character if necessary
            if (inQuote && !onQuote) {
                processed = processSubstituteChars(currentChar, substituteChars, buffer);
            }
            // If the character did not require translation, just append it 
            // as-is
            if (processed == false) {
                buffer.append(currentChar);
            }
        }
        // Append the closing element bracket
        buffer.append('>');
    }

    private static String getTagName(String buffer) {
        // Sample buffer format:
        // NO '<'
        // tagName att1="value" att2="value"
        // NO '>'      
        StringBuffer tagName = new StringBuffer();
        // The tag name is every non-whitespace character in the buffer until
        // a whitespace character is encountered
        for (int i = 0; i < buffer.length(); i++) {
            char character = buffer.charAt(i);
            if (Character.isWhitespace(character)) {
                break;
            }
            tagName.append(character);
        }
        return tagName.toString();
    }

    private static boolean isValidTagAttributeList(String text) {
        // Determine whether the given attribute list is formatted in the
        // valid name="value" XML format
        // Sample formats:
        // " att1="value1" att2="value2"
        // " att1="value1" att2="value2 /"
        // " att1="value1"

        //                       space  attributeName      space = space  "attributeValue" space /
        String patternString = "^([\\s]+[A-Za-z0-9_:\\-\\.]+[\\s]?=[\\s]?\".+?\")*[\\s]*[/]?$"; //$NON-NLS-1$
        Pattern pattern = Pattern.compile(patternString);
        Matcher matcher = pattern.matcher(text);
        // Determine whether the given attribute list matches the pattern
        return matcher.find();
    }

    private static boolean processSubstituteChars(char currentChar, HashMap<?, ?> substituteChars,
            StringBuffer buffer) {
        Character character = new Character(currentChar);
        if (substituteChars.containsKey(character)) {
            String value = (String) substituteChars.get(character);
            if (isDefined(value)) {
                // Append the value if defined
                buffer.append(value);
            }
            // If the value was not defined, then we will strip the character
            return true;
        }
        return false;
    }

    public static boolean isDefined(String text) {
        if ((text == null) || (text.length() == 0)) {
            return false;
        }
        return true;
    }
}