在java中修改birt报表模板的数据源
2008-11-13 11:26
309 查看
// 修改rptdesign内的数据源信息
public void setDataSource(String rptdesignPath) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
InputStream is = null;
String datasource = null;
Document doc = null;
String EncodePsd = null;
try {
db = dbf.newDocumentBuilder();
try {
is = new FileInputStream(rptdesignPath);
try {
doc = db.parse(is);
is.close();
} catch (SAXException e) {
toPrint("SAXException");
return;
} catch (IOException e) {
toPrint("Can't read file [" + rptdesignPath + "]");
return;
}
} catch (FileNotFoundException e) {
toPrint("Can't found file [" + rptdesignPath + "]");
return;
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
toPrint("ParserConfigurationException");
return;
}
// 查找data-sources信息
NodeList nodeList = doc.getElementsByTagName("data-sources");
for (int i = 0; i < nodeList.getLength(); i++) {
Element e = (Element) nodeList.item(i);
NodeList properties = e.getChildNodes();
if (properties != null) {
// 得到oda-data-source部分信息
Node property = properties.item(1);
for (Node node = property.getFirstChild(); node != null; node = node
.getNextSibling()) {
// 得到driver、url、username、password四项的值,并重新设置
if (node.getNodeName().equals("property")
|| node.getNodeName().equals("encrypted-property")) {
String name = node.getAttributes().getNamedItem("name")
.getNodeValue();
if (name.equals("odaDriverClass")) {
node.getFirstChild().setNodeValue(driver);
} else if (name.equals("odaURL")) {
node.getFirstChild().setNodeValue(url);
} else if (name.equals("odaUser")) {
node.getFirstChild().setNodeValue(username);
} else if (name.equals("odaPassword")) {
// 对密码进行base64编码
EncodePsd = (new sun.misc.BASE64Encoder())
.encode(password.getBytes());
// System.out.println("********EncodePsd:
// "+EncodePsd);
node.getFirstChild().setNodeValue(EncodePsd);
}
}
}
}
}
// 写入指定的文件中
writeToXml(doc, rptdesignPath);
}
// write to xml
public void writeToXml(Document doc, String rptdesign) {
try {
OutputStream fileoutputStream = new FileOutputStream(rptdesign);
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(fileoutputStream);
transformer.transform(source, result);
fileoutputStream.close();
} catch (Exception e) {
toPrint("Can't write to file: " + rptdesign);
return;
}
}
public void setDataSource(String rptdesignPath) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
InputStream is = null;
String datasource = null;
Document doc = null;
String EncodePsd = null;
try {
db = dbf.newDocumentBuilder();
try {
is = new FileInputStream(rptdesignPath);
try {
doc = db.parse(is);
is.close();
} catch (SAXException e) {
toPrint("SAXException");
return;
} catch (IOException e) {
toPrint("Can't read file [" + rptdesignPath + "]");
return;
}
} catch (FileNotFoundException e) {
toPrint("Can't found file [" + rptdesignPath + "]");
return;
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
toPrint("ParserConfigurationException");
return;
}
// 查找data-sources信息
NodeList nodeList = doc.getElementsByTagName("data-sources");
for (int i = 0; i < nodeList.getLength(); i++) {
Element e = (Element) nodeList.item(i);
NodeList properties = e.getChildNodes();
if (properties != null) {
// 得到oda-data-source部分信息
Node property = properties.item(1);
for (Node node = property.getFirstChild(); node != null; node = node
.getNextSibling()) {
// 得到driver、url、username、password四项的值,并重新设置
if (node.getNodeName().equals("property")
|| node.getNodeName().equals("encrypted-property")) {
String name = node.getAttributes().getNamedItem("name")
.getNodeValue();
if (name.equals("odaDriverClass")) {
node.getFirstChild().setNodeValue(driver);
} else if (name.equals("odaURL")) {
node.getFirstChild().setNodeValue(url);
} else if (name.equals("odaUser")) {
node.getFirstChild().setNodeValue(username);
} else if (name.equals("odaPassword")) {
// 对密码进行base64编码
EncodePsd = (new sun.misc.BASE64Encoder())
.encode(password.getBytes());
// System.out.println("********EncodePsd:
// "+EncodePsd);
node.getFirstChild().setNodeValue(EncodePsd);
}
}
}
}
}
// 写入指定的文件中
writeToXml(doc, rptdesignPath);
}
// write to xml
public void writeToXml(Document doc, String rptdesign) {
try {
OutputStream fileoutputStream = new FileOutputStream(rptdesign);
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(fileoutputStream);
transformer.transform(source, result);
fileoutputStream.close();
} catch (Exception e) {
toPrint("Can't write to file: " + rptdesign);
return;
}
}
相关文章推荐
- 修改MyEclipse8.6中的Servlet.java模板
- Java修改Access数据库,无需添加数据源。
- 如何修改eclipse下的Java代码注释模板
- 集算报表用Java动态修改报表数据源
- Eclipse java注释模板修改
- (转)JavaWeb学习之Servlet(二)----Servlet的生命周期、继承结构、修改Servlet模板
- eclipse for javaee中修改servlet新建模板
- Eclipse for JavaEE中修改Servlet新建模板
- 润乾集算报表用Java动态修改报表数据源
- 修改MyEclipse8.6中的Servlet.java模板
- Java如何大批量从json数据源中按指定符号隔字符串,并修改、删除数据
- Eclipse Java注释模板的修改(打造自己的专属模板)
- Eclipse4.7基础 修改生成的.java文件模板,添加作者信息与创建时间
- Java中文档注释的模板修改及添加注释的快捷键
- Java设计模式透析之——模板方法
- java修改session默认过期时间
- 修改XCode模板
- 修改Servlet模板
- Eclipse Java文件注释\签名模板
- java swing table 修改