001    /*
002    @license.text@
003     */
004    package biz.hammurapi.ant;
005    
006    import java.io.File;
007    
008    import javax.xml.parsers.DocumentBuilder;
009    import javax.xml.parsers.DocumentBuilderFactory;
010    
011    import org.apache.tools.ant.BuildException;
012    import org.apache.tools.ant.Project;
013    import org.apache.tools.ant.ProjectComponent;
014    import org.w3c.dom.Element;
015    
016    /**
017     * Base class for elements, which need to read xml files.
018     * @ant.type name="XmlSourceEntry" category="Common" ignore="yes"
019     * @author Pavel Vlasov
020     * @version $Revision: 1.2 $
021     */
022    public class XmlSourceEntry extends ProjectComponent {
023    
024        /** Holds value of property failOnError. */
025        private boolean failOnError = true;
026    
027        /**
028         * Fail build if unable to read source. Default is true.
029         * @ant.not-required
030         */
031        public void setFailOnError(boolean failOnError) {
032            this.failOnError = failOnError;
033        }
034    
035        /** Getter for property failOnError.
036         * @return Value of property failOnError.
037         */
038        public boolean isFailOnError() {
039            return this.failOnError;
040        }
041    
042        /**
043         * URL to read rules from. 
044         * @ant.not-required Yes, unless file is set.
045         */
046        public void setURL(String url) {
047            this.url = url;
048        }
049    
050        /** Getter for property URL.
051         * @return Value of property URL.
052         */
053        public String getURL() {
054            return this.url;
055        }
056    
057        /**
058         * File to read rules from.
059         * @ant.required Yes, unless URL is set.
060         */
061        public void setFile(File file) {
062            this.file=file;
063        }
064    
065        /** Getter for property file.
066         * @return Value of property file.
067         */
068        public File getFile() {
069            return this.file;
070        }
071    
072        /** Holds value of property URL. */
073        private String url;
074        /** Holds value of property file. */
075        private File file;
076    
077        public Element getDocumentElement() {
078            if (file!=null && url!=null) {
079                throw new BuildException("file and url are mutually exclusive");
080            }
081            
082            try {
083                DocumentBuilder builder=DocumentBuilderFactory.newInstance().newDocumentBuilder();
084                if (file!=null) {
085                            if (failOnError && !(file.exists() && file.isFile())) {
086                                    throw new BuildException(file.getAbsolutePath()+" does not exist or is not a file");
087                            }
088    
089                    if (getProject()!=null) {
090                            log("Parsing "+file.getAbsolutePath(), Project.MSG_VERBOSE);
091                    }
092                    return builder.parse(file).getDocumentElement(); 
093                } else if (url!=null) {
094                    if (getProject()!=null) {
095                            log("Parsing URL "+url, Project.MSG_VERBOSE);
096                    }
097                    java.net.URL theURL=new java.net.URL(url);
098                    return builder.parse(theURL.openStream()).getDocumentElement();
099                } else {
100                    throw new BuildException("file or url must be set");
101                }
102            } catch (Exception e) {
103                if (failOnError) {
104                    throw new BuildException("Cannot load xml document", e);
105                }
106                
107                            return null;
108            }        
109        }    
110    }