- Timestamp:
- 12/10/10 22:05:19 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
MDService2/trunk/MDService2/src/eu/clarin/cmdi/mdservice/action/Cache.java
r890 r980 15 15 */ 16 16 import java.util.Date; 17 import java.util.HashMap; 17 18 import java.text.DateFormat; 18 19 import java.text.SimpleDateFormat; … … 171 172 } 172 173 173 DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd :hh-mm-ss");174 DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-ddThh:mm:ss"); 174 175 e.setAttribute("date", dateFormat.format(new Date()).toString()); 175 176 cacheindex_doc.getFirstChild().appendChild(e); … … 235 236 return transformed; 236 237 } 238 237 239 238 240 //TODO: sanitize-key … … 506 508 */ 507 509 } 510 511 public InputStream clear(HashMap<String,String> clear_params) throws ParserConfigurationException, TransformerConfigurationException, TransformerException, TransformerFactoryConfigurationError { 512 String xpath_expr = ""; 513 Integer removed = 0; 514 InputStream is = null; 515 516 if (clear_params.size() > 0 ) { 517 if (clear_params.containsKey("repository")){ 518 xpath_expr = xpath_expr + " and @repository='" + clear_params.get("repository") + "'"; 519 } 520 if (clear_params.containsKey("type")){ 521 xpath_expr = xpath_expr + " and @type='" + clear_params.get("type") + "'"; 522 } 523 if (clear_params.containsKey("date")){ 524 xpath_expr = xpath_expr + " and @date='" + clear_params.get("date") + "'"; 525 } 526 if (clear_params.containsKey("datefrom")){ 527 xpath_expr = xpath_expr + " and @date >= '" + clear_params.get("datefrom") + "'"; 528 } 529 if (clear_params.containsKey("dateto")){ 530 xpath_expr = xpath_expr + " and @date <= '" + clear_params.get("dateto") + "'"; 531 } 532 if (xpath_expr.length()>0){ 533 xpath_expr = xpath_expr.substring(5); 534 } 535 xpath_expr = "//index/f[" + xpath_expr + "]"; 536 } else { 537 xpath_expr = "//index/f"; 538 } 539 540 ////////// remove index_lines + files 541 String id,type,path; 542 File f; 543 //creating an XPathFactory: 544 XPathFactory factory = XPathFactory.newInstance(); 545 //using this factory to create an XPath object: 546 XPath xpath = factory.newXPath(); 547 //XPath object created compiles the XPath expression: 548 XPathExpression expr; 549 try { 550 expr = xpath.compile(xpath_expr); 551 //expression is evaluated with respect to a certain context node which is doc. 552 Object result = expr.evaluate(cacheindex_doc, XPathConstants.NODESET); 553 NodeList list = (NodeList) result; 554 555 //create deleted_doc 556 DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); 557 DocumentBuilder docBuilder; 558 docBuilder = docFactory.newDocumentBuilder(); 559 Document deleted_doc = docBuilder.newDocument(); 560 Element root = (Element) deleted_doc.createElement("index"); 561 root.setAttribute("count_deleted", String.valueOf(list.getLength()) ); 562 deleted_doc.appendChild(root); 563 564 // delete items 565 for(int i = 0;i< list.getLength();i++){ 566 567 id = list.item(i).getAttributes().getNamedItem("id").getNodeValue(); 568 type = list.item(i).getAttributes().getNamedItem("type").getNodeValue(); 569 path = cachepath + PREFIX + type + "_"+id + ".xml"; 570 f = new File (path); 571 572 f.delete(); 573 cacheindex_doc.getFirstChild().removeChild(list.item(i)); 574 575 Element e = deleted_doc.createElement("f"); 576 e.setAttribute("filename", path); 577 e.setAttribute("id", id); 578 deleted_doc.getFirstChild().appendChild(e); 579 } 580 581 //create resultstream 582 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 583 Source xmlSource = new DOMSource(deleted_doc); 584 Result outputTarget = new StreamResult(outputStream); 585 TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget); 586 is = new ByteArrayInputStream(outputStream.toByteArray()); 587 588 removed = list.getLength(); 589 590 } catch (XPathExpressionException e) { 591 // TODO Auto-generated catch block 592 e.printStackTrace(); 593 } 594 595 //////// set the index value 596 NodeList nl = cacheindex_doc.getElementsByTagName("f"); 597 if (nl.getLength() < 1){ 598 cachecounter = 1; 599 }else { 600 cachecounter = Integer.parseInt(nl.item(nl.getLength()-1).getAttributes().getNamedItem("id").getNodeValue()); 601 } 602 this.updateCachecounter(); 603 return is; 604 } 605 508 606 }
Note: See TracChangeset
for help on using the changeset viewer.