Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/trapezium/vrml/node/PROTO.java


1   /*
2    * @(#)PROTO.java
3    *
4    * Copyright (c) 1998 by Trapezium Development LLC.  All Rights Reserved.
5    *
6    * The information in this file is the property of Trapezium Development LLC
7    * and may be used only in accordance with the terms of the license granted
8    * by Trapezium.
9    *
10   */
11  package com.trapezium.vrml.node;
12  
13  import com.trapezium.parse.TokenEnumerator;
14  import com.trapezium.vrml.VrmlElement;
15  import com.trapezium.vrml.fields.Field;
16  import com.trapezium.vrml.FieldId;
17  import com.trapezium.vrml.grammar.Spelling;
18  
19  /**
20   *  Scene graph component for a PROTO declaration.
21   *
22   *  @author          Johannes N. Johannsen
23   *  @version         1.1, 17 Dec 1997
24   *
25   *  @since           1.0
26   */
27  public class PROTO extends PROTObase {
28      // used by humanoid verification, because unused fields so common
29      // maybe shouldn't even do this
30    static public boolean suppressISwarning = false;
31  
32    /** PROTO nodes can be used in place of a particular built in type */
33    String builtInNodeType = null;
34  
35    public PROTO( int tokenOffset ) {
36      super( tokenOffset );
37    }
38  
39      /** template method, overrides Node.isPROTOnode() which returns false. */
40      public boolean isPROTOnode() {
41          return( true );
42      }
43      
44    /**
45     *  Get the built-in type for this PROTO, i.e. the type of the first Node in the PROTO.
46     */
47    public String getBuiltInNodeType() {
48      return( builtInNodeType );
49    }
50  
51    public String getBaseName() {
52      return( "PROTO " + getId() );
53    }
54  
55    /**
56     *  Set the built in type that this PROTO can be substituted for.
57     */
58    public void setBuiltInNodeType( String type ) {
59      builtInNodeType = type;
60      if ( type == null ) {
61          setError( "node missing from PROTO body" );
62      }
63    }
64  
65      /** get the PROTO interface field with the name that best matches input name.
66       *  This is used when a PROTO instance has an unknown field name.  We
67       *  assume this is a typo, and search for a valid field name that matches.
68       *
69       *  @param  fieldName  a field name that is not part of the PROTO interface
70       *  
71       *  @return  a Field that has a name that has the best spelling match with
72       *           the fieldName input parameter, returns null if no close matches
73       *           found.
74       */
75    public Field getClosestInterfaceDeclaration( String fieldName ) {
76      Field returnField = null;
77      int biggestMatch = 0;
78      int childCount = numberChildren();
79      for ( int i = 0; i < childCount; i++ ) {
80        VrmlElement vle = getChildAt( i );
81        if ( vle instanceof Field ) {
82          Field f = (Field)vle;
83          String fName = f.getFieldId();
84          int testDistance = Spelling.getMatchScore( fName, fieldName );
85          if ( testDistance > biggestMatch ) {
86            biggestMatch = testDistance;
87            returnField = f;
88          }
89        }
90      }
91      return( returnField );
92    }
93  
94      /**  check if all the PROTO interface fields are referenced by an IS statement */
95    public void checkInUse() {
96      if ( suppressISwarning ) return;
97      for ( int i = 0; i < numberChildren(); i++ ) {
98        VrmlElement vle = getChildAt( i );
99        if ( vle instanceof Field ) {
100         Field f = (Field)vle;
101         if ( !f.isInUse() ) {
102           f.setError( "Warning, field not referenced by IS" );
103         }
104       }
105     }
106   }
107 }
108