com.bstek.dorado.config.xml.XmlParseException.java Source code

Java tutorial

Introduction

Here is the source code for com.bstek.dorado.config.xml.XmlParseException.java

Source

/*
 * This file is part of Dorado 7.x (http://dorado7.bsdn.org).
 * 
 * Copyright (c) 2002-2012 BSTEK Corp. All rights reserved.
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * and BSDN commercial (http://www.bsdn.org/licenses) licenses.
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at http://www.bstek.com/contact.
 */

package com.bstek.dorado.config.xml;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import com.bstek.dorado.config.ParseContext;
import com.bstek.dorado.core.io.Resource;

/**
 * XML?
 * <p>
 * ?XML??
 * </p>
 * @author Benny Bao (mailto:benny.bao@bstek.com)
 * @since Feb 17, 2007
 */
public class XmlParseException extends IllegalArgumentException {
    private static final long serialVersionUID = 5085933510960299967L;

    /**
     * @param message ?
     */
    public XmlParseException(String message) {
        super(populateErrorMessage(message, null, null));
    }

    /**
     * @param message ?
     * @param context ?
     */
    public XmlParseException(String message, ParseContext context) {
        super(populateErrorMessage(message, null, (context != null) ? context.getResource() : null));
    }

    /**
     * @param message ?
     * @param node XML
     * @param resource ???
     */
    public XmlParseException(String message, Node node, Resource resource) {
        super(populateErrorMessage(message, node, resource));
    }

    /**
     * @param message ?
     * @param node XML
     * @param context ?
     */
    public XmlParseException(String message, Node node, ParseContext context) {
        super(populateErrorMessage(message, node, (context != null) ? context.getResource() : null));
    }

    private static String populateErrorMessage(String message, Node node, Resource resource) {
        StringBuffer sb = new StringBuffer();
        if (message != null)
            sb.append(message);

        if (resource != null) {
            sb.append(" - ").append(resource);
        }

        if (node != null) {
            Element element;
            if (node instanceof Element) {
                element = (Element) node;
            } else {
                element = (Element) node.getParentNode();
            }

            if (element != null) {
                sb.append(" - ").append("<" + element.getTagName() + " ");
                NamedNodeMap names = element.getAttributes();
                for (int i = 0; i < 3 && i < names.getLength(); i++) {
                    sb.append(populateXmlAttribute(element, names.item(i).getNodeName())).append(" ");
                }
                sb.append("... ");
            }
        }
        return sb.toString();
    }

    private static String populateXmlAttribute(Element element, String attribute) {
        String value = element.getAttribute(attribute);
        if (StringUtils.isNotEmpty(value)) {
            return attribute + "=\"" + StringEscapeUtils.escapeXml(value) + "\"";
        } else {
            return "";
        }
    }
}