001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 * http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014package org.atteo.evo.config;
015
016import javax.validation.constraints.Pattern;
017import javax.xml.bind.annotation.XmlAccessType;
018import javax.xml.bind.annotation.XmlAccessorType;
019import javax.xml.bind.annotation.XmlAttribute;
020import javax.xml.bind.annotation.XmlID;
021
022import org.atteo.evo.classindex.IndexSubclasses;
023import org.atteo.evo.xmlmerge.CombineChildren;
024import org.atteo.evo.xmlmerge.CombineSelf;
025
026/**
027 * Any configurable element from the configuration file.
028 *
029 * <p>
030 * Configurable contains an 'id' attribute which can be referenced
031 * from other places using standard ID/IDREF functionality of the XML schema.
032 * </p>
033 */
034@IndexSubclasses
035@XmlAccessorType(XmlAccessType.NONE)
036public abstract class Configurable {
037    /**
038     * Id by which this element can be referenced.
039     */
040    @XmlID
041    @XmlAttribute
042    @Pattern(regexp = "\\w[\\w\\d]*")
043    private String id;
044
045    /**
046     * Describes the behavior of node combining when merging XML trees.
047     */
048    @XmlAttribute(name = CombineSelf.ATTRIBUTE_NAME)
049    private CombineSelf combineSelf = CombineSelf.MERGE;
050
051    /**
052     * Describes the behavior of children combining when merging XML trees.
053     */
054    @XmlAttribute(name = CombineChildren.ATTRIBUTE_NAME)
055    private CombineChildren combineChildren = CombineChildren.MERGE;
056
057    /**
058     * Returns an ID for this element associated in the XML file.
059     * @return an ID or null, if ID not assigned
060     */
061    public String getId() {
062        return id;
063    }
064
065    /**
066     * Returns the behavior of node combining when merging XML trees.
067     * @return node behavior
068     */
069    public CombineSelf getCombineSelf() {
070        return combineSelf;
071    }
072
073    /**
074     * Returns the behavior of children combining when merging XML trees.
075     * @return node behavior
076     */
077    public CombineChildren getCombineChildren() {
078        return combineChildren;
079    }
080
081    @Override
082    public String toString() {
083        StringBuilder builder = new StringBuilder();
084        builder.append(getClass().getSimpleName());
085        if (id != null) {
086            builder.append("[id=\"");
087            builder.append(id);
088            builder.append("\"]");
089        }
090        return builder.toString();
091    }
092}