Sascha's Wiki

Verwendung von Lookup in Netbeans

Allgemein

Was ist ein Lookup? Der Lookup dient bei Netbeans dazu auf Objekte und Methoden die andere Pakete oder Klassen bereitstellen zuzugreifen, ohne dass man diese der Gewünschten Methode übergeben muss.

Imports

Um Lookup zu verwenden muss man dem Netbeans Modul die Lookup API als Dependency hinzufügen (Rechtsklick auf Modul → Properties → Libraries → Add Dependency).

Klassenimports:

import org.openide.util.Lookup;
import org.openide.util.lookup.ServiceProvider;

Aufbau

Lookups werden über eine Klasse, ein Interface und einen ServiceProvider erzeugt:

Beispielcode ohne Zusammenhang:

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;

import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import java.io.IOException;
import java.util.ArrayList;
import org.openide.util.lookup.ServiceProvider;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

@ServiceProvider(service = Interface.class)
public class Class implements Interface {

    
    @Override
    public void writeXMLFile(ArrayList<String[]> input) throws ParserConfigurationException, 
                                                               FileNotFoundException, 
                                                               IOException, 
                                                               SAXException {
        createFile();
        //DocumentBuilderFactory
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        //DocumentBuilder
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        //Document
        Document xmlDoc = docBuilder.newDocument();
        //Build XML Elements and Text Nodes
        //<time>
        //  <date>2013-08-23</date>
        //  <from>07:00</from>
        //  <to>17:53</to>
        //</time>
        Element rootNode = xmlDoc.createElement("root");
        int i = 0;

        for (String[] strings : xmlList) {

            Element timeElement = xmlDoc.createElement("time");
            timeElement.setAttribute("id", String.valueOf(i));

            Text dateElementText = xmlDoc.createTextNode(strings[0]);
            Element dateElement = xmlDoc.createElement("date");
            dateElement.appendChild(dateElementText);

            Text fromElementText = xmlDoc.createTextNode(strings[1]);
            Element fromElement = xmlDoc.createElement("from");
            fromElement.appendChild(fromElementText);

            Text toElementText = xmlDoc.createTextNode(strings[2]);
            Element toElement = xmlDoc.createElement("to");
            toElement.appendChild(toElementText);

            timeElement.appendChild(dateElement);
            timeElement.appendChild(fromElement);
            timeElement.appendChild(toElement);

            rootNode.appendChild(timeElement);

            i++;
        }

        xmlDoc.appendChild(rootNode);

        //Set OutputFormat
        OutputFormat outFormat = new OutputFormat(xmlDoc);
        outFormat.setIndenting(true);


        //Declare the FileOutputStream
        FileOutputStream outStream = new FileOutputStream(xmlFile);

        //XMLSerializer to serialize the XML data with the specified OutputFormat
        XMLSerializer serializer = new XMLSerializer(outStream, outFormat);
        serializer.serialize(xmlDoc);

    }

    @Override
    public ArrayList<String[]> readXML() throws ParserConfigurationException, IOException, SAXException {
        //DocumentBuilderFactory
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        //DocumentBuilder
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        //Document
        Document xmlDoc = docBuilder.parse(new InputSource(filePath));

        xmlDoc.getDocumentElement().normalize();

        NodeList timeList = xmlDoc.getElementsByTagName("time");

        ArrayList<String[]> tags = new ArrayList<String[]>();

        if (!xmlFile.exists()) {
            return tags;
        }

        for (int temp = 0; temp < timeList.getLength(); temp++) {
            Node nNode = timeList.item(temp);


            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element element = (Element) nNode;
                String[] elements = new String[3];

                /**
                 * Reading "date" value from xml file
                 */
                NodeList dateList = element.getElementsByTagName("date");
                Element dateListElement = (Element) dateList.item(0);
                NodeList textDateList = dateListElement.getChildNodes();
                elements[0] = ((Node) textDateList.item(0)).getNodeValue().trim();

                /**
                 * Reading "from" value from xml file
                 */
                NodeList fromList = element.getElementsByTagName("from");
                Element fromListElement = (Element) fromList.item(0);
                NodeList textFromList = fromListElement.getChildNodes();
                elements[1] = ((Node) textFromList.item(0)).getNodeValue().trim();

                /**
                 * Reading "to" value from xml File
                 */
                NodeList toList = element.getElementsByTagName("to");
                Element toListElement = (Element) toList.item(0);
                NodeList textToList = toListElement.getChildNodes();
                elements[2] = ((Node) textToList.item(0)).getNodeValue().trim();

                tags.add(elements);
            }
        }

        return tags;
        
    }
    
    private void createFile() {
        xmlFile.delete();
        xmlFile = new File(filePath);
    }

}

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;

public interface IWrite {
          
    public void writeXMLFile(ArrayList<String[]> input) throws ParserConfigurationException, 
    FileNotFoundException, IOException, SAXException;
    
    public ArrayList<String[]> readXML() throws ParserConfigurationException, IOException, SAXException;
    
}

Zugriff

Über

Lookup.getDefault().lookup("Interface.class").Methode
kann man auf die Methoden zugreifen.

Beispiel für obige Klasse:

ArrayList<String[]> data = new ArrayList<String[]>();

data = Lookup.getDefault().lookup("Interface.class").readXML;

Lookup.getDefault().lookup("Interface.class").writeXMLFile(data);

java/netbeans/lookup.txt · Zuletzt geändert: 2016/04/21 09:15 (Externe Bearbeitung)