Simple Example

To start developing a new service can be very simple, for a service which is associated with all XML Documents and that displays "Hello World [arguments]" in the console when opened, just follow the next steps:

This example including sources can be downloaded from: sources/simple-service.zip.

Step 1 - Create Service

Create the file SimpleDocumentService.java and copy the next code in there:

import java.net.URI;
import java.util.List;
import java.util.Map;

import javax.swing.ImageIcon;

import org.xngr.DocumentMarker;
import org.xngr.DocumentService;

public class SimpleDocumentService implements DocumentService {
  public boolean close() {
    return false;
  }
  
  public String getIdentifier() {
    return "any string would work here";
  }
  
  public String getDescription() {
    return "Simple XML Document Service";
  }
  
  public ImageIcon getIcon() {
    return null;
  }
  
  public String getName() {
    return "Simple Service";
  }
  
  public void open(URI uri, Map<String, String> arguments) {
    System.out.println("Hello World ["+uri+"]");
  }
  
  public List<DocumentMarker> getMarkers() {
    // return null to associate the services with all XML Documents 
    return null;
  }
}

Step 2 - Create Bundle Activator

Create the file SimpleDocumentActivator.java and copy the next code in there:

import java.util.Hashtable;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

import org.xngr.Service;

public class SimpleDocumentActivator implements BundleActivator {
  public void start(BundleContext context) throws Exception {
    context.registerService(Service.class.getName(), new SimpleDocumentService(), 
    new Hashtable<Object, Object>());
  }
  
  public void stop(BundleContext arg0) throws Exception {}
}

Step 3 - Compile

Compile the java files, make sure that org.xngr-2.0-beta-1.jar file is included in the classpath.

Step 4 - Create OSGI manifest

Create a file that is called manifest.mf and contains the following elements:

Bundle-Name: Simple Document Service
Bundle-Description: A bundle that registers a Simple Document Service.
Bundle-Vendor: Anybody
Bundle-Version: 1.0.0
Bundle-Activator: SimpleDocumentActivator
Import-Package: org.osgi.framework,org.xngr,javax.swing

Note: for a complete description of how to create an OSGI bundle please see the Felix tutorial pages: http://felix.apache.org/site/apache-felix-osgi-tutorial.html

Step 5 - Package as Jar

Jar everything correctly together in a .jar file.
Use the command-line: 'jar cvfm simple.jar manifest.mf .'

The directory structure for the simple.jar file should look like this:
/meta-inf/Manifest.mf
SimpleDocumentService.class
SimpleDocumentActivator.class
...

Step 6 - Install bundle

Start the application, using either the xngr.bat, xngr.sh scripts or by invoking java -jar bin/felix.jar from the command-line.

In the console type the following:
start file:/c:/simple/simple.jar

Note: for a complete description of how to install, uninstall and start and stop OSGI bundles using Felix please see the Felix Usage Documentation: http://felix.apache.org/site/apache-felix-usage-documentation.html

Node Marker Example

To create a marker which 'marks' nodes, just follow the next steps:

This example including sources can be downloaded from: sources/simple-marker.zip.

Step 1 - Create Marker

Create the file SimpleDocBookNodeMarker.java and copy the next code in there:

import java.util.Arrays;
import java.util.List;

import javax.swing.ImageIcon;
import javax.xml.namespace.QName;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xngr.NodeMarker;

public class SimpleDocBookNodeMarker implements NodeMarker {
  ImageIcon icon = new ImageIcon(getClass().getResource("stackframe.gif"));
  
  public String getIdentifier() {
    return "SimpleDocBookNodeMarker";
  }
  
  public XPathExpression getXPathExpression() {
    XPathExpression expression = null;
    
    XPath path = XPathFactory.newInstance().newXPath();
    
    try {
      expression = path.compile("//*[title]");
    } catch (XPathExpressionException e) {
      e.printStackTrace();
    }
    
    return expression;
  }
  
  public List<QName> getRootElementNames() {
    return Arrays.asList(new QName[]{new QName("chapter")});
  }
  
  public String getDescription(Node node) {
    return "Title viewer";
  }
  
  public String getName(Node node) {
    if (node instanceof Element) {
      NodeList list = ((Element)node).getElementsByTagName("title");
      if (list.getLength() > 0) {
        return ((Element)list.item(0)).getTextContent();
      }
    }
    
    return null;
  }
  
  public ImageIcon getIcon(Node node) {
    return icon;
  }
}

Step 2 - Create Bundle Activator

Create the file SimpleDocBookNodeActivator.java and copy the next code in there:

import java.util.Hashtable;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

import org.xngr.Marker;

public class SimpleDocBookNodeActivator implements BundleActivator {
  public void start(BundleContext context) throws Exception {
    context.registerService(Marker.class.getName(), new SimpleDocBookNodeMarker(), 
    new Hashtable<Object, Object>());
  }
  
  public void stop(BundleContext arg0) throws Exception {}
}

Step 3 - Compile

Compile the java files, make sure that org.xngr-2.0-beta-1.jar file is included in the classpath.

Step 4 - Create OSGI manifest

Create a file that is called manifest.mf and contains the following elements:

Bundle-Name: Simple DocBook Node Marker
Bundle-Description: A bundle that registers a Simple DocBook Node Marker.
Bundle-Vendor: Anybody
Bundle-Version: 1.0.0
Bundle-Activator: SimpleDocBookNodeActivator
Import-Package: org.osgi.framework,org.xngr,javax.swing,javax.xml.namespace,javax.xml.xpath,
 org.w3c.dom

Note: for a complete description of how to create an OSGI bundle please see the Felix tutorial pages: http://felix.apache.org/site/apache-felix-osgi-tutorial.html

Step 5 - Package as Jar

Jar everything correctly together in a .jar file.
Use the command-line: 'jar cvf simple-marker.jar manifest.mf .'

The directory structure for the simple-marker.jar file should look like this:
/meta-inf/Manifest.mf
SimpleDocBookNodeMarker.class
SimpleDocBookNodeActivator.class
stackframe.gif
...

Step 6 - Install bundle

Start the application, using either the xngr.bat, xngr.sh scripts or by invoking java -jar bin/felix.jar from the command-line.

In the console type the following:
start file:/c:/simple/simple-marker.jar

Note: for a complete description of how to install, uninstall and start and stop OSGI bundles using Felix please see the Felix Usage Documentation: http://felix.apache.org/site/apache-felix-usage-documentation.html