Inspector | Message | Severity | Location |
---|---|---|---|
Java Inspector 048 | Copyrights information should be present in each file. | 1 | |
Java Inspector 089 | Undocumented parameter failOnError | 2 | 50:5 |
Java Inspector 089 | Undocumented parameter url | 2 | 65:5 |
Java Inspector 089 | Undocumented parameter file | 2 | 80:5 |
Java Inspector 089 | Undocumented method | 2 | 96:5 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 98:38 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 105:81 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 109:29 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 114:29 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 119:42 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 123:42 |
Java Inspector 090 | Unnecessary else part in if. The main part terminates control flow (return, break, throw, or continue). | 3 | 103:13 |
Java Inspector 090 | Unnecessary else part in if. The main part terminates control flow (return, break, throw, or continue). | 3 | 112:20 |
1/*
2 * hgcommons 9
3 * Hammurapi Group Common Library
4 * Copyright (C) 2003 Hammurapi Group
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
21 * e-Mail: support@hammurapi.biz
22 */
23package biz.hammurapi.ant;
24
25import java.io.File;
26
27import javax.xml.parsers.DocumentBuilder;
28import javax.xml.parsers.DocumentBuilderFactory;
29
30import org.apache.tools.ant.BuildException;
31import org.apache.tools.ant.Project;
32import org.apache.tools.ant.ProjectComponent;
33import org.w3c.dom.Element;
34
35/**
36 * Base class for elements, which need to read xml files.
37 * @ant.type name="XmlSourceEntry" category="Common" ignore="yes"
38 * @author Pavel Vlasov
39 * @version $Revision: 1.2 $
40 */
41public class XmlSourceEntry extends ProjectComponent {
42
43 /** Holds value of property failOnError. */
44 private boolean failOnError = true;
45
46 /**
47 * Fail build if unable to read source. Default is true.
48 * @ant.not-required
49 */
50 public void setFailOnError(boolean failOnError) {
51 this.failOnError = failOnError;
52 }
53
54 /** Getter for property failOnError.
55 * @return Value of property failOnError.
56 */
57 public boolean isFailOnError() {
58 return this.failOnError;
59 }
60
61 /**
62 * URL to read rules from.
63 * @ant.not-required Yes, unless file is set.
64 */
65 public void setURL(String url) {
66 this.url = url;
67 }
68
69 /** Getter for property URL.
70 * @return Value of property URL.
71 */
72 public String getURL() {
73 return this.url;
74 }
75
76 /**
77 * File to read rules from.
78 * @ant.required Yes, unless URL is set.
79 */
80 public void setFile(File file) {
81 this.file=file;
82 }
83
84 /** Getter for property file.
85 * @return Value of property file.
86 */
87 public File getFile() {
88 return this.file;
89 }
90
91 /** Holds value of property URL. */
92 private String url;
93 /** Holds value of property file. */
94 private File file;
95
96 public Element getDocumentElement() {
97 if (file!=null && url!=null) {
98 throw new BuildException("file and url are mutually exclusive");
99 }
100
101 try {
102 DocumentBuilder builder=DocumentBuilderFactory.newInstance().newDocumentBuilder();
103 if (file!=null) {
104 if (failOnError && !(file.exists() && file.isFile())) {
105 throw new BuildException(file.getAbsolutePath()+" does not exist or is not a file");
106 }
107
108 if (getProject()!=null) {
109 log("Parsing "+file.getAbsolutePath(), Project.MSG_VERBOSE);
110 }
111 return builder.parse(file).getDocumentElement();
112 } else if (url!=null) {
113 if (getProject()!=null) {
114 log("Parsing URL "+url, Project.MSG_VERBOSE);
115 }
116 java.net.URL theURL=new java.net.URL(url);
117 return builder.parse(theURL.openStream()).getDocumentElement();
118 } else {
119 throw new BuildException("file or url must be set");
120 }
121 } catch (Exception e) {
122 if (failOnError) {
123 throw new BuildException("Cannot load xml document", e);
124 }
125
126 return null;
127 }
128 }
129}
130