Tuesday, May 15, 2012

Copy nodes subtree from one DOM document to another

package com.javacodegeeks.snippets.core;

import java.io.File;
import java.io.FileInputStream;
import java.io.StringWriter;
import java.io.Writer;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class CopyNodesSubtreeFromOneDOMDocumentToAnother {
 
 public static void main(String[] args) throws Exception {
  
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  dbf.setValidating(false);
  DocumentBuilder db = dbf.newDocumentBuilder();
  
  Document doc1 = db.parse(new FileInputStream(new File("in1.xml")));
  Document doc2 = db.parse(new FileInputStream(new File("in2.xml")));
  
  System.out.println("Before Copy...");
  prettyPrint(doc2);
  
  NodeList list = doc1.getElementsByTagName("channel");
  Element element = (Element) list.item(0);

  // Imports a node from another document to this document, without altering 
     // or removing the source node from the original document
  Node copiedNode = doc2.importNode(element, true);

  // Adds the node to the end of the list of children of this node
  doc2.getDocumentElement().appendChild(copiedNode);
  
  System.out.println("After Copy...");
  prettyPrint(doc2);
  
 }
 
 public static final void prettyPrint(Document xml) throws Exception {
  Transformer tf = TransformerFactory.newInstance().newTransformer();
  tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
  tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
  tf.setOutputProperty(OutputKeys.INDENT, "yes");
  Writer out = new StringWriter();
  tf.transform(new DOMSource(xml), new StreamResult(out));
  System.out.println(out.toString());
 }

}
input:
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
 <channel>
  <title>Java Tutorials and Examples 1</title>
  <language>en-us</language>
  <item>
   <title><![CDATA[Java Tutorials 1]]></title>
   <link>http://www.javacodegeeks.com/</link>
  </item>
  <item>
   <title><![CDATA[Java Examples 1]]></title>
   <link>http://examples.javacodegeeks.com/</link>
  </item>
 </channel>
</rss>


Thursday, May 10, 2012

writing SWRL rules on protege 4.1 and running on pellet reasoner

1. install the

Pellet Reasoner Plug-in for Protégé 4

  1. Select “File | Preferences”.
  2. Select “Plugins” tab.
  3. Click the “Check for downloads now” button under the “Plugin registry” heading.
  4. Scroll down the “Downloads” list to find “Pellet Reasoner Plug-in” and select the checkbox next to it (See the screen shot below).
  5. If you agree with the licensing terms click “Install”.
  6. When you see the “Updates will take effect next time Protege starts” message, restart Protégé.
2. go to the "window"->views->ontology views->rules
then you can define you SWRL rules in the rule tab.