001/* 002 * Copyright 2013 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.core.util.dom; 017 018import java.util.Collections; 019import java.util.HashMap; 020import java.util.Map; 021 022import org.w3c.dom.Element; 023import org.w3c.dom.Node; 024 025/** 026 * DOM Layer over a tag name, attributes and String value. 027 */ 028public class StringElement extends AbstractAmetysElement 029{ 030 private String _data; 031 private Map<String, String> _attributes; 032 033 /** 034 * Create a string element 035 * @param tagName The tag name 036 * @param data The data value. 037 */ 038 public StringElement(String tagName, String data) 039 { 040 this(tagName, (Map<String, String>) null, data, null); 041 } 042 043 /** 044 * Create a string element 045 * @param tagName The tag name 046 * @param attributeName The attribute name. 047 * @param attributeValue The attribute value. 048 */ 049 public StringElement(String tagName, String attributeName, String attributeValue) 050 { 051 this(tagName, Collections.singletonMap(attributeName, attributeValue), null, null); 052 } 053 054 /** 055 * Create a string element 056 * @param tagName The tag name 057 * @param attributeName The attribute with the value. 058 * @param attributeValue The attribute value. 059 * @param data The data value. 060 */ 061 public StringElement(String tagName, String attributeName, String attributeValue, String data) 062 { 063 this(tagName, Collections.singletonMap(attributeName, attributeValue), data, null); 064 } 065 066 /** 067 * Create a string element 068 * @param attributes The attributes names and values. 069 * @param tagName The tag name. 070 */ 071 public StringElement(String tagName, Map<String, String> attributes) 072 { 073 this(tagName, attributes, null, null); 074 } 075 076 /** 077 * Create a string element 078 * @param tagName The tag name 079 * @param attributes The attributes names and values. 080 * @param data The data value. 081 */ 082 public StringElement(String tagName, Map<String, String> attributes, String data) 083 { 084 this(tagName, attributes, data, null); 085 } 086 087 088 /** 089 * Create a string element 090 * @param tagName The tag name 091 * @param data The data value. 092 * @param parent the parent {@link Element}. 093 */ 094 public StringElement(String tagName, String data, Element parent) 095 { 096 this(tagName, (Map<String, String>) null, data, parent); 097 } 098 099 /** 100 * Create a string element 101 * @param tagName The tag name 102 * @param attributeName The attribute name. 103 * @param attributeValue The attribute value. 104 * @param parent the parent {@link Element}. 105 */ 106 public StringElement(String tagName, String attributeName, String attributeValue, Element parent) 107 { 108 this(tagName, Collections.singletonMap(attributeName, attributeValue), null, parent); 109 } 110 111 /** 112 * Create a string element 113 * @param tagName The tag name 114 * @param attributeName The attribute with the value. 115 * @param attributeValue The attribute value. 116 * @param data The data value. 117 * @param parent the parent {@link Element}. 118 */ 119 public StringElement(String tagName, String attributeName, String attributeValue, String data, Element parent) 120 { 121 this(tagName, Collections.singletonMap(attributeName, attributeValue), data, parent); 122 } 123 124 /** 125 * Create a string element 126 * @param attributes The attributes names and values. 127 * @param tagName The tag name. 128 * @param parent the parent {@link Element}. 129 */ 130 public StringElement(String tagName, Map<String, String> attributes, Element parent) 131 { 132 this(tagName, attributes, null, parent); 133 } 134 135 /** 136 * Create a string element 137 * @param tagName The tag name 138 * @param attributes The attributes names and values. 139 * @param data The data value. 140 * @param parent the parent {@link Element}. 141 */ 142 public StringElement(String tagName, Map<String, String> attributes, String data, Element parent) 143 { 144 super(tagName, parent); 145 _data = data; 146 _attributes = attributes; 147 } 148 149 @Override 150 protected Map<String, AmetysAttribute> _lookupAttributes() 151 { 152 Map<String, AmetysAttribute> attrs = new HashMap<>(); 153 154 if (_attributes != null) 155 { 156 for (String name : _attributes.keySet()) 157 { 158 attrs.put(name, new AmetysAttribute(name, name, null, _attributes.get(name), this)); 159 } 160 } 161 162 return attrs; 163 } 164 165 @Override 166 public Node getFirstChild() 167 { 168 return _data != null ? new AmetysText(_data, this) : null; 169 } 170}