📄 configsexpression.java
字号:
/* * $RCSfile: ConfigSexpression.java,v $ * * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistribution of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistribution in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * Neither the name of Sun Microsystems, Inc. or the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * This software is provided "AS IS," without a warranty of any * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that this software is not designed, licensed or * intended for use in the design, construction, operation or * maintenance of any nuclear facility. * * $Revision: 1.5 $ * $Date: 2007/02/09 17:20:44 $ * $State: Exp $ */package com.sun.j3d.utils.universe ;import java.awt.event.* ;import java.io.* ;import java.lang.Integer ;import java.lang.Boolean ;import java.util.* ;import javax.vecmath.* ;import javax.media.j3d.* ;class ConfigSexpression { private ArrayList elements = new ArrayList() ; private void syntaxError(StreamTokenizer st, String file, String s) { System.out.println(s + ":\nat line " + st.lineno() + " in " + file) ; print() ; System.out.print("\n\n") ; } private int myNextToken(StreamTokenizer st, String file) { int tok = 0 ; try { tok = st.nextToken() ; } catch (IOException e) { throw new RuntimeException(e + "\nwhile reading " + file) ; } return tok ; } Object parseAndEval(ConfigContainer configContainer, StreamTokenizer st, int level) { int tok ; String s ; String file = configContainer.currentFileName ; // // First tokenize the character stream and add the tokens to the // elements array. // elements.clear() ; // Look for an open paren to start this sexp. while (true) { tok = myNextToken(st, file) ; if (tok == StreamTokenizer.TT_EOF) return Boolean.FALSE ; if (tok == ')') syntaxError(st, file, "Premature closing parenthesis") ; if (tok == '(') break ; } // Add elements until a close paren for this sexp is found. for (int i = 0 ; true ; i++) { tok = myNextToken(st, file) ; if (tok == StreamTokenizer.TT_EOF) { syntaxError(st, file, "Missing closing parenthesis") ; break ; } // An open paren starts a new embedded sexp. Put the paren back, // evaluate the sexp, and add the result to the elements list. if (tok == '(') { st.pushBack() ; ConfigSexpression cs = new ConfigSexpression() ; elements.add(cs.parseAndEval(configContainer, st, level+1)) ; continue ; } // A close paren finishes the scan. if (tok == ')') break ; // Check for too many arguments. if (i >= 20) syntaxError(st, file, "Too many arguments") ; // Check for numeric argument. if (tok == StreamTokenizer.TT_NUMBER) { elements.add(new Double(st.nval)) ; continue ; } // Anything other than a word or a quoted string is an error. if (tok != StreamTokenizer.TT_WORD && tok != '"' && tok != '\'') { String badToken = String.valueOf((char)tok) ; elements.add(badToken) ; // so bad token prints out syntaxError(st, file, "Invalid token \"" + badToken + "\" must be enclosed in quotes") ; continue ; } // Scan the token for Java property substitution syntax ${...}. s = scanJavaProperties(st, file, st.sval) ; if (s == null) continue ; if (s.equalsIgnoreCase("true")) // Replace "true" or "True" with the Boolean equivalent. elements.add(new Boolean(true)) ; else if (s.equalsIgnoreCase("false")) // Replace "false" or "False" with the Boolean equivalent. elements.add(new Boolean(false)) ; else // Add the token as a string element. elements.add(s) ; } // // Now evaluate elements. // if (elements.size() == 0) syntaxError(st, file, "Null command") ; // If the first argument is a string, then this sexp must be // a top-level command or a built-in, and needs to be evaluated. if (elements.get(0) instanceof String) { try { if (level == 0) { configContainer.evaluateCommand(elements, st.lineno()) ; // Continue parsing top-level commands. return Boolean.TRUE ; } else { // Evaluate built-in and return result to next level up. return evaluateBuiltIn (configContainer, elements, st.lineno()) ; } } catch (IllegalArgumentException e) { syntaxError(st, file, e.getMessage()) ; if (level == 0) // Command ignored: continue parsing. return Boolean.TRUE ; else // Function ignored: return sexp to next level up so error // processing can print it out in context of command. return this ; } } // If the first argument isn't a string, and we are at level 0, // this is a syntax error. if (level == 0) syntaxError(st, file, "Malformed top-level command name") ; // If the first argument is a number, then we must have // either a 2D, 3D, or 4D numeric vector. if (elements.get(0) instanceof Double) { if (elements.size() == 1) syntaxError(st, file, "Can't have single-element vector") ; // Point2D if (elements.size() == 2) { if (!(elements.get(1) instanceof Double)) syntaxError(st, file, "Both elements must be numbers") ; return new Point2d(((Double)elements.get(0)).doubleValue(), ((Double)elements.get(1)).doubleValue()) ; } // Point3d if (elements.size() == 3) { if (!(elements.get(1) instanceof Double) || !(elements.get(2) instanceof Double)) syntaxError(st, file, "All elements must be numbers") ; return new Point3d(((Double)elements.get(0)).doubleValue(), ((Double)elements.get(1)).doubleValue(), ((Double)elements.get(2)).doubleValue()) ; } // Point4D if (elements.size() == 4) { if (!(elements.get(1) instanceof Double) || !(elements.get(2) instanceof Double) || !(elements.get(3) instanceof Double)) syntaxError(st, file, "All elements must be numbers") ; return new Point4d(((Double)elements.get(0)).doubleValue(), ((Double)elements.get(1)).doubleValue(), ((Double)elements.get(2)).doubleValue(), ((Double)elements.get(3)).doubleValue()) ; } // Anything else is an error. syntaxError(st, file, "Too many vector elements") ; } // If the first argument is a Point3d, then we should be a Matrix3d. if (elements.get(0) instanceof Point3d) { if (elements.size() != 3) syntaxError(st, file, "Matrix must have three rows") ; if (!(elements.get(1) instanceof Point3d) || !(elements.get(2) instanceof Point3d)) syntaxError(st, file, "All rows must have three elements") ; return new Matrix3d(((Point3d)elements.get(0)).x, ((Point3d)elements.get(0)).y, ((Point3d)elements.get(0)).z, ((Point3d)elements.get(1)).x, ((Point3d)elements.get(1)).y, ((Point3d)elements.get(1)).z, ((Point3d)elements.get(2)).x, ((Point3d)elements.get(2)).y, ((Point3d)elements.get(2)).z) ; } // If the first argument is a Point4d, then we should be a Matrix4d. if (elements.get(0) instanceof Point4d) { if (elements.size() == 3) { if (!(elements.get(1) instanceof Point4d) || !(elements.get(2) instanceof Point4d)) syntaxError(st, file, "All rows must have four elements") ; return new Matrix4d(((Point4d)elements.get(0)).x, ((Point4d)elements.get(0)).y, ((Point4d)elements.get(0)).z, ((Point4d)elements.get(0)).w, ((Point4d)elements.get(1)).x, ((Point4d)elements.get(1)).y, ((Point4d)elements.get(1)).z, ((Point4d)elements.get(1)).w, ((Point4d)elements.get(2)).x, ((Point4d)elements.get(2)).y, ((Point4d)elements.get(2)).z, ((Point4d)elements.get(2)).w, 0.0, 0.0, 0.0, 1.0) ; } else if (elements.size() != 4) syntaxError(st, file, "Matrix must have three or four rows") ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -