在java中修改birt报表模板的数据源

 // 修改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;

  }
 }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值