All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.cxf.tools.common.Tag Maven / Gradle / Ivy

There is a newer version: 2.7.18
Show newest version
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you 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.
 */

package org.apache.cxf.tools.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.namespace.QName;

public class Tag {
    QName name;
    Map attributes;
    String text;

    List tags;
    Tag parent;

    List ignoreAttr;


    public List getIgnoreAttr() {
        if (ignoreAttr == null) {
            ignoreAttr = new ArrayList();
        }
        return ignoreAttr;
    }

    public Tag getParent() {
        return this.parent;
    }

    public void setParent(Tag nTag) {
        this.parent = nTag;
    }

    public List getTags() {
        if (tags == null) {
            tags = new ArrayList();
        }
        return tags;
    }

    public String getText() {
        return text;
    }

    public void setText(String nText) {
        this.text = nText;
    }

    public QName getName() {
        return name;
    }

    public void setName(QName nName) {
        this.name = nName;
    }

    public Map getAttributes() {
        if (attributes == null) {
            attributes = new HashMap();
        }
        return attributes;
    }
    
    private String createIndent(int size) {
        String indent = "    ";
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < size; i++) {
            sb.append(indent);
        }
        return sb.toString();
    }

    private String formatAttribute(final Tag tag) {
        StringBuilder sb = new StringBuilder();
        sb.append(tag.getName().getLocalPart());
        sb.append(" ");
        for (Map.Entry attr : tag.getAttributes().entrySet()) {
            sb.append(attr.getKey());
            sb.append("=\"");
            sb.append(attr.getValue());
            sb.append("\" ");
        }
        return sb.toString().trim();
    }

    private String formatTag(Tag tag, int indent) {
        StringBuilder sb = new StringBuilder();
        sb.append(createIndent(indent));
        sb.append(indent);
        sb.append("<");
        sb.append(formatAttribute(tag));
        sb.append(">");
        if (tag.getParent() != null) {
            sb.append(" (" + tag.getParent().getName().getLocalPart() + ")");
        }
        if (text != null) {
            sb.append(text); 
        }
        sb.append("\n");

        if (tag.getTags().size() > 0) {
            indent++;
            for (Tag subTag : tag.getTags()) {
                sb.append(formatTag(subTag, indent));
            }
        }
        return sb.toString();
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(formatTag(this, 0));
        return sb.toString();
    }

    public int hashCode() {
        return getName().hashCode() + getAttributes().hashCode();
    }

    public boolean equals(Object object) {
        if (object == null) {
            return false;
        }
        if (!(object instanceof Tag)) {
            return false;
        }

        if (object == this) {
            return true;
        }
        Tag tag = (Tag) object;
        if (!getName().equals(tag.getName())) {
            return false;
        }
        for (QName attr : getAttributes().keySet()) {
            if (getIgnoreAttr().contains(attr.getLocalPart())
                || getIgnoreAttr().contains(getName().getLocalPart() + "@" + attr.getLocalPart())) {
                continue;
            }
            if (!tag.getAttributes().containsKey(attr)) {
                return false;
            }
            if (!tag.getAttributes().get(attr).equals(getAttributes().get(attr))) {
                return false;
            }
        }
        return true;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy