alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.xml.text.completion;

import org.w3c.dom.*;
import org.netbeans.modules.xml.spi.model.*;
import org.netbeans.modules.xml.text.syntax.*;
import org.netbeans.modules.xml.text.syntax.dom.*;
import java.io.StringWriter;
import java.io.PrintWriter;
import java.util.Enumeration;

import junit.framework.*;

/**
 * Just remember completion request and return empty results.
 * Probe passes context for consistency.
 *
 * @author  Petr Kuzel
 */
class UnitTestQuery implements GrammarQuery {

    private String op = ".";
    private String ctx = "initial context";
    private Exception trace;
    private TestCase test;
    
    /** Creates new TestQuery */
    public UnitTestQuery(TestCase test) {
        this.test = test;
    }

    /**
     * Allow to get names of parsed general entities.
     * @return list of GrammarResults (ENTITY_REFERENCE_NODEs)
     */
    public Enumeration queryEntities(String prefix) {
        op = "R";
        ctx = "'" + prefix + "'";
        fillTrace();        
        return org.openide.util.Enumerations.empty();
    }    
    
    /**
     * @stereotype query
     * @output list of results that can be queried on name, and attributes
     * @time Performs fast up to 300 ms.
     * @param ctx represents virtual attribute Node to be replaced. Its parent is a element node.
     * @return list of GrammarResults (ATTRIBUTE_NODEs) that can be queried on name, and attributes.
     *        Every list member represents one possibility.
     */
    public Enumeration queryAttributes(HintContext ctx) {
        op = "A";
        this.ctx = ctx.toString();
        fillTrace();
        return EmptyEnumeration.EMPTY;        
    }
    
    /**
     * @semantics Navigates through read-only Node tree to determine context and provide right results.
     * @postconditions Let ctx unchanged
     * @time Performs fast up to 300 ms.
     * @stereotype query
     * @param ctx represents virtual element Node that has to be replaced, its own attributes does not name sense, it can be used just as the navigation start point.
     * @return list of GrammarResults (ELEMENT_NODEs) that can be queried on name, and attributes
     *        Every list member represents one possibility.
     */
    public Enumeration queryElements(HintContext ctx) {
        op = "E";
        this.ctx = ctx.toString();
        fillTrace();        
        
        // we need raw parent name, raw children names, my position in children
        
        Node parent = ctx.getParentNode();
        if (parent != null) {
            
//            System.out.println("Context:" + ctx);
//            System.out.print("parentnode?");
            String name = parent.getNodeName();
            
//            System.out.print("siblings?");
            
            // index of place we are inserted to
            
            int index = 0;
            
            Node sibling = null;
            
            // find previous ELEMENT sibling
            
            sibling = ctx;
            do {
                sibling = sibling.getPreviousSibling();
                if (sibling == null) break;
                if (sibling.getNodeType() == Node.ELEMENT_NODE) break;
            } while (true);
                
            while (sibling != null) {

                index++;
                
                // find previous ELEMENT sibling
                
                do {
                    sibling = sibling.getPreviousSibling();
                    if (sibling == null) break;
                    if (sibling.getNodeType() == Node.ELEMENT_NODE) break;
                } while (true);
                
            }

            // all children
            
//           System.out.print("kids?"); 
           String names = "";
           NodeList kids = parent.getChildNodes();
           
           if (kids == null) {
               op = "X missing kids";
               return EmptyEnumeration.EMPTY;
           }
           for (int i = 0; i < kids.getLength(); i++) {
               Node next = kids.item(i);
               if (next.getNodeType() == Node.ELEMENT_NODE) {
                    names += next.getNodeName()  + ", ";
               }
           }

           // this is context needed by Sandeep's Liasion
           this.ctx = "queryElements(): parent: " + name + " p'kids: " + names + " index: " + index;
            
        } else {
            op = "X missing parent";
        }
        
        return EmptyEnumeration.EMPTY;                
    }
    
    /**
     * Allow to get names of declared notations.
     * @return list of GrammarResults (NOTATION_NODEs)
     */
    public Enumeration queryNotations(String prefix) {
        op = "N";
        ctx = "'" + prefix + "'";
        fillTrace();        
        return EmptyEnumeration.EMPTY;        
    }
    
    /**
     * @semantics Navigates through read-only Node tree to determine context and provide right results.
     * @postconditions Let ctx unchanged
     * @time Performs fast up to 300 ms.
     * @stereotype query
     * @input ctx represents virtual Node that has to be replaced (parent can be either Attr or Element), its own attributes does not name sense, it can be used just as the navigation start point.
     * @return list of GrammarResults (TEXT_NODEs) that can be queried on name, and attributes.
     *        Every list member represents one possibility.
     */
    public Enumeration queryValues(HintContext ctx) {
        op = "V";
        this.ctx = ctx.toString();     
        fillTrace();
        return EmptyEnumeration.EMPTY;
    }
    

    void reset() {
        op = ".";
        ctx = "no context passed (no query)";
        trace = null;
    }
    
    String operation() {
        return op;
    }
    
    String context() {
        return ctx;
    }
    
    String stackTrace() {
        if (trace != null) {
            StringWriter swr = new StringWriter();
            PrintWriter out = new PrintWriter(swr);            
            trace.printStackTrace(out);
            out.flush();
            return swr.getBuffer().toString();       
        } else {
            return "";
        }
    }
    
    private void fillTrace() {
        trace = new RuntimeException();
    }
}

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.