001: /*
002: * hgcommons 7
003: * Hammurapi Group Common Library
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.ant;
024:
025: import java.io.File;
026:
027: import javax.xml.parsers.DocumentBuilder;
028: import javax.xml.parsers.DocumentBuilderFactory;
029:
030: import org.apache.tools.ant.BuildException;
031: import org.apache.tools.ant.Project;
032: import org.apache.tools.ant.Task;
033: import org.w3c.dom.Element;
034:
035: /**
036: * Base class for elements, which need to read xml files.
037: * @author Pavel Vlasov
038: * @version $Revision: 1.2 $
039: */
040: public class XmlSourceEntry extends Task {
041:
042: /** Holds value of property failOnError. */
043: private boolean failOnError = true;
044:
045: /**
046: * Fail build if unable to read source. Default is true.
047: * @ant.non-required
048: */
049: public void setFailOnError(boolean failOnError) {
050: this .failOnError = failOnError;
051: }
052:
053: /** Getter for property failOnError.
054: * @return Value of property failOnError.
055: */
056: public boolean isFailOnError() {
057: return this .failOnError;
058: }
059:
060: /**
061: * URL to read rules from.
062: * @ant.non-required Yes, unless file is set.
063: */
064: public void setURL(String url) {
065: this .url = url;
066: }
067:
068: /** Getter for property URL.
069: * @return Value of property URL.
070: */
071: public String getURL() {
072: return this .url;
073: }
074:
075: /**
076: * File to read rules from.
077: * @ant.required Yes, unless URL is set.
078: */
079: public void setFile(File file) {
080: this .file = file;
081: }
082:
083: /** Getter for property file.
084: * @return Value of property file.
085: */
086: public File getFile() {
087: return this .file;
088: }
089:
090: /** Holds value of property URL. */
091: private String url;
092: /** Holds value of property file. */
093: private File file;
094:
095: public Element getDocumentElement() {
096: if (file != null && url != null) {
097: throw new BuildException(
098: "file and url are mutually exclusive");
099: }
100:
101: try {
102: DocumentBuilder builder = DocumentBuilderFactory
103: .newInstance().newDocumentBuilder();
104: if (file != null) {
105: if (failOnError && !(file.exists() && file.isFile())) {
106: throw new BuildException(file.getAbsolutePath()
107: + " does not exist or is not a file");
108: }
109:
110: if (getProject() != null) {
111: log("Parsing " + file.getAbsolutePath(),
112: Project.MSG_VERBOSE);
113: }
114: return builder.parse(file).getDocumentElement();
115: } else if (url != null) {
116: if (getProject() != null) {
117: log("Parsing URL " + url, Project.MSG_VERBOSE);
118: }
119: java.net.URL theURL = new java.net.URL(url);
120: return builder.parse(theURL.openStream())
121: .getDocumentElement();
122: } else {
123: throw new BuildException("file or url must be set");
124: }
125: } catch (Exception e) {
126: if (failOnError) {
127: throw new BuildException("Cannot load xml document", e);
128: }
129:
130: return null;
131: }
132: }
133: }
|