Java tutorial
//package com.java2s; /** * Copyright (C) [2013] [The FURTHeR Project] * * 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. */ import java.util.Map; public class Main { public static final String ELEMENT_EMPTY_OPEN = "<"; public static final String ELEMENT_EMPTY_CLOSE = "/>"; /** * Attribute syntax within an element start: <code>(ATTRIBUTE_DELIMITER + * attribute_name + ATTRIBUTE_EQUALS + ATTRIBUTE_VALUE_ESCAPE_START + * attribute_value + ATTRIBUTE_VALUE_ESCAPE_END)</code>. */ public static final String ATTRIBUTE_DELIMITER = " "; public static final String ATTRIBUTE_EQUALS = "="; public static final String ATTRIBUTE_VALUE_ESCAPE_START = "\""; public static final String ATTRIBUTE_VALUE_ESCAPE_END = "\""; /** * Print an empty XML tag with attributes. * * @param tag * tag name * @return opening tag */ public static StringBuilder emptyTag(final String tag) { final StringBuilder s = new StringBuilder(ELEMENT_EMPTY_OPEN); s.append(tag); s.append(ELEMENT_EMPTY_CLOSE); return s; } /** * Print an empty XML tag. * * @param tag * tag name * @param attributes * list of attributes * @return opening tag */ public static StringBuilder emptyTag(final String tag, final Map<String, String> attributes) { final StringBuilder s = new StringBuilder(ELEMENT_EMPTY_OPEN).append(tag); for (final Map.Entry<String, String> attribute : attributes.entrySet()) { s.append(ATTRIBUTE_DELIMITER); s.append(attribute.getKey()); s.append(ATTRIBUTE_EQUALS); s.append(ATTRIBUTE_VALUE_ESCAPE_START); s.append(attribute.getValue()); s.append(ATTRIBUTE_VALUE_ESCAPE_END); } s.append(ELEMENT_EMPTY_CLOSE); return s; } }