Java tutorial
/* * Copyright (C) 2014 Arnaud PETIT <arnaud.petit@minesdedouai.fr> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301 USA */ package edu.jdomengine.core.attr; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import org.apache.commons.lang.StringEscapeUtils; import edu.jdomengine.core.JDomBaseNode; import edu.jdomengine.core.JDomC; import edu.jdomengine.interfaces.IJDom; import edu.jdomengine.interfaces.IJDomNode; import edu.jdomengine.interfaces.accept.attr.IJDomAcceptDataset; import edu.jdomengine.interfaces.attr.IJDomDataset; /** * Class representing a dataset of attribute values, implementing * {@link IJDomDataset} */ public class JDomDataset extends JDomBaseNode implements IJDomDataset { private static final long serialVersionUID = 1L; /** * Factory method for any {@link JDomDataset} attribute. * @param node {@link IJDomAcceptDataset} the parent node of this one. * @return a new {@link JDomDataset} node. */ public static JDomDataset createAttribute(IJDomAcceptDataset node) { return new JDomDataset(node); } protected JDomDataset(IJDomNode node) { super(node); } private Map<String, String> set = null; protected Map<String, String> getSet() { if (this.set == null) { this.set = new HashMap<>(); } return this.set; } @Override public String getName() { return "<dataset>"; //$NON-NLS-1$ } @Override public String getValue() { StringBuilder sb = new StringBuilder(); renderHtml5(sb); return sb.toString(); } @Override public StringBuilder toHtml5(StringBuilder str) { for (Entry<String, String> e : getSet().entrySet()) { if (e.getKey() != null && e.getKey() != IJDom.EMPTY_STRING) { String arg = StringEscapeUtils.escapeHtml(e.getKey()).replaceAll("[^\\w\\d]", IJDom.EMPTY_STRING); //$NON-NLS-1$ String val = StringEscapeUtils.escapeHtml(e.getValue()); str.append(" data-").append(arg).append("=\"") //$NON-NLS-1$ //$NON-NLS-2$ .append(val).append("\""); //$NON-NLS-1$ } } return str; } @Override public String toString() { return toHtml5(new StringBuilder()).toString(); } @Override public StringBuilder renderHtml5(StringBuilder str) { return toHtml5(str); } @Override public boolean needsRender() { return !getSet().isEmpty(); } /** * Adds a value to target data-key attribute * @param key the anchor to append to value to. * @param value the value to save. */ public void add(String key, String value) { String truekey = sanitize(key); String target = getSet().get(truekey); if (target == null) { getSet().put(truekey, value); } else { getSet().put(truekey, target + IJDom.SPACE + value); } } /** * transform all keys into * @param key the key to make html-proof * @return String the sanitized key */ public String sanitize(String key) { String subkey = key; if (!subkey.matches("[a-z0-9\\-]")) { //$NON-NLS-1$ logWarning(JDomC.LOG_HYPHEN_TRANSFORM, key); subkey = subkey.replaceAll("([A-Z])", "-$1"); //$NON-NLS-1$ //$NON-NLS-2$ } return subkey.toLowerCase(); } /** * replaces the value with target key to another value * @param key the anchor to append to value to. * @param value the value to save. */ public void replace(String key, String value) { String truekey = sanitize(key); getSet().put(truekey, value); } /** * Deletes the value with target key. * @param key the key of the value to delete. */ public void delete(String key) { String truekey = sanitize(key); getSet().remove(truekey); } }