Source Code Cross Referenced for Command.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » beans » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » org package » org.apache.harmony.beans 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */
017:
018:        package org.apache.harmony.beans;
019:
020:        import java.beans.BeanInfo;
021:        import java.beans.Expression;
022:        import java.beans.IndexedPropertyDescriptor;
023:        import java.beans.IntrospectionException;
024:        import java.beans.Introspector;
025:        import java.beans.PropertyDescriptor;
026:        import java.beans.Statement;
027:        import java.beans.XMLDecoder;
028:        import java.lang.reflect.Array;
029:        import java.lang.reflect.Method;
030:        import java.util.HashMap;
031:        import java.util.Iterator;
032:        import java.util.Map;
033:        import java.util.Vector;
034:
035:        import org.apache.harmony.beans.internal.nls.Messages;
036:        import org.xml.sax.Attributes;
037:
038:        public class Command {
039:
040:            private static final int INITIALIZED = 0;
041:
042:            private static final int CHILDREN_FILTERED = 1;
043:
044:            private static final int COMMAND_EXECUTED = 2;
045:
046:            private static final int CHILDREN_PROCESSED = 3;
047:
048:            private String tagName; // tag name
049:
050:            private Map<String, String> attrs; // set of attrs
051:
052:            private String data; // string data
053:
054:            // inner commands
055:            private Vector<Command> commands = new Vector<Command>();
056:
057:            // arguments
058:            private Vector<Command> arguments = new Vector<Command>();
059:
060:            // operations
061:            private Vector<Command> operations = new Vector<Command>();
062:
063:            // additional arguments placed before others
064:            private Vector<Argument> auxArguments = new Vector<Argument>();
065:
066:            private Argument result; // result argument
067:
068:            private Object target; // target to execute a command on
069:
070:            private String methodName; // method name
071:
072:            private Command ctx; // context for command
073:
074:            private int status; // commands
075:
076:            private XMLDecoder decoder;
077:
078:            // private int tabCount = 0;
079:
080:            public Command(String tagName, Map<String, String> attrs) {
081:                this .tagName = tagName;
082:                this .attrs = attrs;
083:                this .status = initializeStatus(tagName);
084:            }
085:
086:            public Command(XMLDecoder decoder, String tagName,
087:                    Map<String, String> attrs) {
088:                this .decoder = decoder;
089:                this .tagName = tagName;
090:                this .attrs = attrs;
091:                this .status = initializeStatus(tagName);
092:            }
093:
094:            // set data for command
095:            public void setData(String data) {
096:                this .data = data;
097:            }
098:
099:            // set tab count to display log messages
100:            // public void setTabCount(int tabCount) {
101:            // this.tabCount = tabCount;
102:            // }
103:
104:            // set context - upper level command
105:            public void setContext(Command ctx) {
106:                this .ctx = ctx;
107:            }
108:
109:            // add child command
110:            public void addChild(Command cmd) {
111:                if (cmd != null) {
112:                    cmd.setContext(this );
113:                    commands.add(cmd);
114:                }
115:            }
116:
117:            // remove child command
118:            public void removeChild(Command cmd) {
119:                if ((cmd != null) && commands.remove(cmd)) {
120:                    cmd.setContext(null);
121:                }
122:            }
123:
124:            // command status
125:            public int getStatus() {
126:                return status;
127:            }
128:
129:            // check if one of arguments or operations is unresolved
130:            private boolean isResolved() {
131:                if (getStatus() < Command.CHILDREN_PROCESSED) {
132:                    return false;
133:                }
134:                for (int i = 0; i < arguments.size(); ++i) {
135:                    Command arg = arguments.elementAt(i);
136:
137:                    if (!arg.isResolved()) {
138:                        return false;
139:                    }
140:                }
141:                for (int j = 0; j < operations.size(); ++j) {
142:                    Command opr = operations.elementAt(j);
143:
144:                    if (!opr.isResolved()) {
145:                        return false;
146:                    }
147:                }
148:                return true;
149:            }
150:
151:            // execute command and return execution flags
152:            public int exec(Map<String, Command> references) throws Exception {
153:                // System.out.println("in exec() status = " + translateStatus(status) +
154:                // "...");
155:                if (status < Command.CHILDREN_PROCESSED) {
156:                    if (status < Command.COMMAND_EXECUTED) {
157:                        if (status < Command.CHILDREN_FILTERED) {
158:                            status = doBeforeRun(references);
159:                            // System.out.println("after doBeforeRun() status = " +
160:                            // translateStatus(status));
161:                        }
162:                        if (status == Command.CHILDREN_FILTERED) {
163:                            status = doRun(references);
164:                            // System.out.println("after doRun() status = " +
165:                            // translateStatus(status));
166:                        }
167:                    }
168:                    if (status == Command.COMMAND_EXECUTED) {
169:                        status = doAfterRun(references);
170:                        // System.out.println("after doAfterRun() status = " +
171:                        // translateStatus(status));
172:                    }
173:                }
174:                // System.out.println("...out of exec() status = " +
175:                // translateStatus(status));
176:                return status;
177:            }
178:
179:            // execute commands in backtrack order
180:            public boolean backtrack(Map<String, Command> references)
181:                    throws Exception {
182:                for (int i = 0; i < arguments.size(); ++i) {
183:                    Command arg = arguments.elementAt(i);
184:                    arg.backtrack(references);
185:                }
186:                for (int i = 0; i < operations.size(); ++i) {
187:                    Command opr = operations.elementAt(i);
188:                    opr.backtrack(references);
189:                }
190:                if (status == Command.CHILDREN_FILTERED) {
191:                    status = doRun(references);
192:                }
193:                if (status == Command.COMMAND_EXECUTED) {
194:                    status = doAfterRun(references);
195:                    return (getStatus() == Command.CHILDREN_PROCESSED);
196:                }
197:                return false;
198:            }
199:
200:            // put command in one of two collections - arguments or operations
201:            private int doBeforeRun(Map<String, Command> references)
202:                    throws Exception {
203:                if (status == Command.INITIALIZED) {
204:                    for (int i = 0; i < commands.size(); ++i) {
205:                        Command cmd = commands.elementAt(i);
206:
207:                        // XXX is this correct?
208:                        if (cmd.isExecutable()) {
209:                            arguments.add(cmd);
210:                        } else {
211:                            operations.add(cmd);
212:                        }
213:                    }
214:                    return Command.CHILDREN_FILTERED;
215:                }
216:                return status;
217:            }
218:
219:            // run command
220:            private int doRun(Map<String, Command> references) throws Exception {
221:                if (status == Command.CHILDREN_FILTERED) {
222:                    if (isRoot()) {
223:                        result = new Argument(decoder);
224:                        // System.out.println("doRun(): result is decoder...");
225:                        return Command.COMMAND_EXECUTED;
226:                    }
227:
228:                    if (isNull()) {
229:                        result = new Argument(null);
230:                        // System.out.println("doRun(): result is null...");
231:                        return Command.COMMAND_EXECUTED;
232:                    }
233:
234:                    if (ctx != null && ctx.isArray()
235:                            && (ctx.getResultValue() == null)
236:                            && !isExecutable()) {
237:                        // System.out.println("doRun(): context is array...");
238:                        return status;
239:                    }
240:
241:                    Object target = getTarget(references);
242:                    if (target == null) {
243:                        // System.out.println("doRun(): target is null...");
244:                        return status;
245:                    }
246:                    // if (target instanceof Class) {
247:                    // System.out.println("doRun(): target = " +
248:                    // ((Class)target).getName());
249:                    // } else {
250:                    // System.out.println("doRun(): target = " +
251:                    // target.getClass().getName());
252:                    // }
253:                    if (isReference()) {
254:                        result = getReferencedArgument(references);
255:                        // System.out.println("doRun(): reference - result is " +
256:                        // result.getType());
257:                    } else {
258:                        String methodName = getMethodName(references);
259:                        // System.out.println("doRun(): methodName = " +
260:                        // methodName);
261:                        Argument[] arguments = getArguments();
262:                        if (arguments == null) {
263:                            return status;
264:                        }
265:                        // for (Argument element : arguments) {
266:                        // if (element != null) {
267:                        // System.out.println("doRun(): arg [" + i + "] = "
268:                        // + arguments[i].getType());
269:                        // } else {
270:                        // System.out.println("doRun(): arg [" + i + "] =
271:                        // null");
272:                        // }
273:                        // }
274:
275:                        Expression expr = new Expression(target, methodName,
276:                                getArgumentsValues());
277:                        result = new Argument(expr.getValue());
278:
279:                        if (isPrimitiveClassName(getTagName())) {
280:                            result.setType(getPrimitiveClass(tagName));
281:                        }
282:
283:                        // System.out.println("doRun(): method call - result is " +
284:                        // result.getType());
285:                    }
286:                    return Command.COMMAND_EXECUTED;
287:                }
288:                return status;
289:            }
290:
291:            // run child commands
292:            private int doAfterRun(Map<String, Command> references)
293:                    throws Exception {
294:                if (status == Command.COMMAND_EXECUTED) {
295:                    // System.out.println("doAfterRun(): command " + getResultType() + "
296:                    // processed...");
297:                    Vector<Command> toBeRemoved = new Vector<Command>();
298:                    try {
299:                        Statement[] statements = null;
300:
301:                        for (int i = 0; i < operations.size(); ++i) {
302:                            Command cmd = operations.elementAt(i);
303:
304:                            if (cmd.isArrayElement()) {
305:
306:                                if (cmd.isResolved()) {
307:                                    if (statements == null) {
308:                                        statements = new Statement[operations
309:                                                .size()];
310:                                    }
311:                                    statements[i] = new Statement(
312:                                            getResultValue(),
313:                                            "set", new Object[] { new Integer(i), //$NON-NLS-1$
314:                                                    cmd.getResultValue() });
315:                                    if ((i + 1) == operations.size()) {
316:                                        for (int j = 0; j < operations.size(); ++j) {
317:                                            statements[j].execute();
318:                                        }
319:                                        toBeRemoved.addAll(operations);
320:                                    }
321:                                } else {
322:                                    break;
323:                                }
324:
325:                            } else {
326:                                // since the call is Array.set()
327:                                if (!isArray()) {
328:                                    cmd.setTarget(getResultValue());
329:                                }
330:                                cmd.exec(references);
331:
332:                                if (cmd.isResolved()) {
333:                                    // System.out.println("doAfterRun(): cmd = " +
334:                                    // cmd.methodName + " is resolved");
335:                                    toBeRemoved.add(cmd);
336:                                } else {
337:                                    // System.out.println("doAfterRun(): cmd = " +
338:                                    // cmd.methodName + " is unresolved");
339:                                    break;
340:                                }
341:
342:                            }
343:
344:                        }
345:                    } catch (Exception e) {
346:                        throw new Exception(e);
347:                    } finally {
348:                        operations.removeAll(toBeRemoved);
349:                    }
350:
351:                    // if (operations.size() == 0) {
352:                    // System.out.println("doAfterRun(): command " + getResultType()
353:                    // + " completely processed.");
354:                    // } else {
355:                    // System.out.println("doAfterRun(): command " + getResultType()
356:                    // + " contains incomplete " +
357:                    // operations.size() + " commands.");
358:                    // }
359:                    return (operations.size() == 0) ? Command.CHILDREN_PROCESSED
360:                            : status;
361:                }
362:                return status;
363:            }
364:
365:            // Result accessors
366:
367:            // Return result - Argument class
368:            public Argument getResult() {
369:                return result;
370:            }
371:
372:            // Returns result value
373:            public Object getResultValue() {
374:                return (result != null) ? result.getValue() : null;
375:            }
376:
377:            // Returns result type
378:            public Class<?> getResultType() throws ClassNotFoundException {
379:                return (result != null) ? result.getType() : null;
380:            }
381:
382:            // accessors to XML tags and attrs
383:            public boolean hasAttr(String name) {
384:                return attrs.get(name) != null;
385:            }
386:
387:            public String getAttr(String name) {
388:                return attrs.get(name);
389:            }
390:
391:            public boolean isTag(String name) {
392:                return tagName.equals(name);
393:            }
394:
395:            public boolean hasAttr(String name, String value) {
396:                return value.equals(attrs.get(name));
397:            }
398:
399:            public String getTagName() {
400:                return tagName;
401:            }
402:
403:            // Checks if the object is primitive - int, float, etc...
404:            private boolean isPrimitive() {
405:                return isPrimitiveClassName(tagName);
406:            }
407:
408:            // Checks if the object is constructor
409:            private boolean isConstructor() {
410:                return isPrimitive() || !isStaticMethod() && !isMethod()
411:                        && !isProperty() && !isField() && !isArray()
412:                        && !isReference();
413:            }
414:
415:            // Checks if the command is static method
416:            private boolean isStaticMethod() {
417:                return isTag("object") && hasAttr("method") || isTag("class"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
418:            }
419:
420:            // Checks if the command is public method
421:            private boolean isMethod() {
422:                return isTag("void") && (hasAttr("method") || hasAttr("index")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
423:            }
424:
425:            // Check if the command relates to property - getter ot setter depends on
426:            // number of args
427:            private boolean isProperty() {
428:                return isTag("void") && hasAttr("property"); //$NON-NLS-1$ //$NON-NLS-2$
429:            }
430:
431:            // Check if the command is field accessor
432:            private boolean isField() {
433:                return isTag("object") && hasAttr("field"); //$NON-NLS-1$ //$NON-NLS-2$
434:            }
435:
436:            // Check if the command is array
437:            private boolean isArray() {
438:                return isTag("array"); //$NON-NLS-1$
439:            }
440:
441:            // Check if the command is array element
442:            private boolean isArrayElement() {
443:                return (ctx != null) && (ctx.isArray()) && isExecutable();
444:            }
445:
446:            private boolean isReference() {
447:                return hasAttr("idref"); //$NON-NLS-1$
448:            }
449:
450:            // Check if the command is root object
451:            private boolean isRoot() {
452:                return isTag("java"); //$NON-NLS-1$
453:            }
454:
455:            // Check if the command is null
456:            private boolean isNull() {
457:                return isTag("null"); //$NON-NLS-1$
458:            }
459:
460:            // Checks if the command could generate object
461:            public boolean isExecutable() {
462:                boolean result = isTag("object") || isTag("void") && hasAttr("class") //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
463:                        && hasAttr("method") || isTag("array") || isPrimitive() //$NON-NLS-1$ //$NON-NLS-2$
464:                        || isTag("class") || isTag("null"); //$NON-NLS-1$ //$NON-NLS-2$
465:                return result;
466:            }
467:
468:            private Argument getReferencedArgument(
469:                    Map<String, Command> references) {
470:                return references.get(getAttr("idref")).getResult(); //$NON-NLS-1$
471:            }
472:
473:            // get a target through tag and attrs analysis
474:            private Object getTarget(Map<String, Command> references)
475:                    throws Exception {
476:                if (target == null) {
477:                    if (isReference()) {
478:                        Command cmd = references.get(getAttr("idref")); //$NON-NLS-1$
479:                        target = (cmd != null) ? cmd.getResultValue() : null;
480:                    } else if (isExecutable()) {
481:                        String className = null;
482:
483:                        if (isPrimitive()) {
484:                            className = getPrimitiveClassName(tagName);
485:                        } else if (isTag("class")) { //$NON-NLS-1$
486:                            className = getPrimitiveClassName(tagName);
487:                        } else if (isConstructor() || isStaticMethod()
488:                                || isField()) {
489:                            className = getAttr("class"); //$NON-NLS-1$
490:                        } else if (isArray()) {
491:                            className = getAttr("class"); //$NON-NLS-1$
492:                            Class<?> componentType = isPrimitiveClassName(className) ? getPrimitiveClass(className)
493:                                    : Class.forName(className, true, Thread
494:                                            .currentThread()
495:                                            .getContextClassLoader());
496:                            className = Array.newInstance(componentType, 0)
497:                                    .getClass().getName();
498:                        }
499:
500:                        if (className != null) {
501:                            try {
502:                                target = Class.forName(className, true, Thread
503:                                        .currentThread()
504:                                        .getContextClassLoader());
505:                            } catch (ClassNotFoundException e) {
506:                                target = Class.forName(className);
507:                            }
508:
509:                            if (isField()) {
510:                                String fieldName = getAttr("field"); //$NON-NLS-1$
511:                                target = ((Class) target).getField(fieldName);
512:                            }
513:                        } else {
514:                            throw new Exception(Messages.getString(
515:                                    "beans.42", className)); //$NON-NLS-1$
516:                        }
517:                    } else if (ctx.isArray()) {
518:                        // target = ctx.getTarget(references);
519:                        target = Class.forName("java.lang.reflect.Array"); //$NON-NLS-1$
520:                    }
521:                }
522:                return target;
523:            }
524:
525:            // set target to execute command on
526:            private void setTarget(Object target) {
527:                this .target = target;
528:            }
529:
530:            // Return a method name of command
531:            private String getMethodName(Map<String, Command> references)
532:                    throws NoSuchMethodException, IntrospectionException,
533:                    Exception {
534:                if (methodName == null) {
535:                    String methodValue = null;
536:                    if (isTag("class")) { //$NON-NLS-1$
537:                        addArgument(new Argument(String.class, data), 0);
538:                        methodValue = "forName"; //$NON-NLS-1$
539:                    } else if (isPrimitive()) {
540:                        if (isTag("char")) { //$NON-NLS-1$
541:                            if (data.length() != 1) {
542:                                throw new IntrospectionException(Messages
543:                                        .getString("beans.43", //$NON-NLS-1$
544:                                                data));
545:                            }
546:                            addArgument(new Argument(char.class, new Character(
547:                                    data.charAt(0))), 0);
548:                        } else {
549:                            addArgument(new Argument(String.class, data), 0);
550:                        }
551:                        methodValue = "new"; //$NON-NLS-1$
552:                    } else if (isConstructor() || hasAttr("method", "new")) { //$NON-NLS-1$ //$NON-NLS-2$
553:                        methodValue = "new"; //$NON-NLS-1$
554:                    } else if (isArray()) {
555:                        methodValue = "new"; //$NON-NLS-1$
556:                        int length = hasAttr("length") ? Integer //$NON-NLS-1$
557:                                .parseInt(getAttr("length")) : getArgumentsNumber(); //$NON-NLS-1$
558:                        copyArgumentsToCommands();
559:                        addArgument(
560:                                new Argument(int.class, new Integer(length)), 0);
561:                    } else if (hasAttr("property")) { //$NON-NLS-1$
562:                        String propertyValue = getAttr("property"); //$NON-NLS-1$
563:                        if (hasAttr("index")) { //$NON-NLS-1$
564:                            addArgument(new Argument(int.class, new Integer(
565:                                    getAttr("index"))), 0); //$NON-NLS-1$
566:                        }
567:
568:                        BeanInfo beanInfo = Introspector.getBeanInfo(getTarget(
569:                                references).getClass());
570:                        PropertyDescriptor[] pds = beanInfo
571:                                .getPropertyDescriptors();
572:
573:                        boolean methodFound = false;
574:                        Method method = null;
575:                        for (PropertyDescriptor pd : pds) {
576:                            if (propertyValue.equals(pd.getName())) {
577:                                int argsNum = getArgumentsNumber();
578:                                if (hasAttr("index")) { //$NON-NLS-1$
579:                                    IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
580:                                    if (argsNum == 1) {
581:                                        method = ipd.getIndexedReadMethod();
582:                                    } else if (argsNum == 0) {
583:                                        method = ipd.getReadMethod();
584:                                    }
585:                                } else {
586:                                    method = pd.getReadMethod();
587:                                }
588:
589:                                if (method != null) {
590:                                    methodFound = matchMethodParams(method,
591:                                            references);
592:                                }
593:
594:                                if (methodFound == false) {
595:                                    if (hasAttr("index")) { //$NON-NLS-1$
596:                                        IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
597:                                        if (argsNum == 2) {
598:                                            method = ipd
599:                                                    .getIndexedWriteMethod();
600:                                        } else if (argsNum == 1) {
601:                                            method = ipd.getWriteMethod();
602:                                        }
603:                                    } else {
604:                                        method = pd.getWriteMethod();
605:                                    }
606:                                }
607:
608:                                if (method != null) {
609:                                    methodFound = matchMethodParams(method,
610:                                            references);
611:                                }
612:                            }
613:                        }
614:
615:                        if (method == null) {
616:                            throw new NoSuchMethodException(Messages.getString(
617:                                    "beans.44", //$NON-NLS-1$
618:                                    propertyValue));
619:                        }
620:                        methodValue = method.getName();
621:                    } else if (hasAttr("method")) { //$NON-NLS-1$
622:                        if (hasAttr("index")) { //$NON-NLS-1$
623:                            addArgument(new Argument(int.class, Integer
624:                                    .valueOf(getAttr("index"))), 0); //$NON-NLS-1$
625:                        }
626:                        methodValue = getAttr("method"); //$NON-NLS-1$
627:                    } else if (hasAttr("index")) { //$NON-NLS-1$
628:                        addArgument(new Argument(int.class, Integer
629:                                .valueOf(getAttr("index"))), 0); //$NON-NLS-1$
630:                        methodValue = getArgumentsNumber() > 1 ? "set" : "get"; //$NON-NLS-1$ //$NON-NLS-2$
631:                        if (ctx.isArray()) {
632:                            addArgument(ctx.getResult(), 0);
633:                        }
634:                    } else if (hasAttr("field")) { //$NON-NLS-1$
635:                        addArgument(
636:                                new Argument(Class.forName(
637:                                        getAttr("class"), true, //$NON-NLS-1$
638:                                        Thread.currentThread()
639:                                                .getContextClassLoader())), 0);
640:
641:                        methodValue = "get"; //$NON-NLS-1$
642:                    } else {
643:                        throw new Exception(Messages.getString("beans.45")); //$NON-NLS-1$
644:                    }
645:                    methodName = methodValue;
646:                }
647:                return methodName;
648:            }
649:
650:            // return a list of arguments as of Argument type
651:            private Argument[] getArguments() {
652:                Argument[] args = new Argument[auxArguments.size()
653:                        + arguments.size()];
654:
655:                for (int i = 0; i < auxArguments.size(); ++i) {
656:                    args[i] = auxArguments.elementAt(i);
657:                }
658:                for (int j = 0; j < arguments.size(); ++j) {
659:                    Command cmd = arguments.elementAt(j);
660:
661:                    if (cmd.getStatus() >= Command.COMMAND_EXECUTED) {
662:                        args[auxArguments.size() + j] = cmd.getResult();
663:                    } else {
664:                        // System.out.println("arg: " + cmd.getResultValue());
665:                        args = null;
666:                        break;
667:                    }
668:                }
669:                return args;
670:            }
671:
672:            // return argument values
673:            private Object[] getArgumentsValues() {
674:                Argument[] args = getArguments();
675:                Object[] result = new Object[args.length];
676:                for (int i = 0; i < args.length; ++i) {
677:                    result[i] = args[i].getValue();
678:                }
679:                return result;
680:            }
681:
682:            // copy arguments to treat as commands
683:            private void copyArgumentsToCommands() {
684:                Iterator<Command> i = arguments.iterator();
685:                while (i.hasNext()) {
686:                    Command cmd = i.next();
687:                    cmd.status = Command.CHILDREN_FILTERED;
688:                    operations.add(cmd);
689:                }
690:                arguments.clear();
691:            }
692:
693:            // return number of arguments
694:            private int getArgumentsNumber() {
695:                return auxArguments.size() + arguments.size();
696:            }
697:
698:            // return number of commands
699:            // private int getOperationsNumber() {
700:            // return operations.size();
701:            // }
702:
703:            // add argument to the beginning of arguments
704:            private void addArgument(Argument argument, int idx) {
705:                auxArguments.insertElementAt(argument, idx);
706:            }
707:
708:            // Check if the name of class is primitive
709:            public static boolean isPrimitiveClassName(String className) {
710:                return className.equalsIgnoreCase("boolean") //$NON-NLS-1$
711:                        || className.equalsIgnoreCase("byte") //$NON-NLS-1$
712:                        || className.equalsIgnoreCase("char") //$NON-NLS-1$
713:                        || className.equalsIgnoreCase("short") //$NON-NLS-1$
714:                        || className.equalsIgnoreCase("int") //$NON-NLS-1$
715:                        || className.equalsIgnoreCase("long") //$NON-NLS-1$
716:                        || className.equalsIgnoreCase("float") //$NON-NLS-1$
717:                        || className.equalsIgnoreCase("double") //$NON-NLS-1$
718:                        || className.equalsIgnoreCase("string"); //$NON-NLS-1$
719:            }
720:
721:            // Transforms a primitive class name
722:            private String getPrimitiveClassName(String data) {
723:                String shortClassName = null;
724:                if (data.equals("int")) { //$NON-NLS-1$
725:                    shortClassName = "Integer"; //$NON-NLS-1$
726:                } else if (data.equals("char")) { //$NON-NLS-1$
727:                    shortClassName = "Character"; //$NON-NLS-1$
728:                } else {
729:                    shortClassName = data.substring(0, 1).toUpperCase()
730:                            + data.substring(1, data.length());
731:                }
732:                return "java.lang." + shortClassName; //$NON-NLS-1$
733:            }
734:
735:            public static Class<?> getPrimitiveClass(String className) {
736:                Class<?> result = null;
737:                if (className.equals("boolean")) { //$NON-NLS-1$
738:                    result = boolean.class;
739:                } else if (className.equals("byte")) { //$NON-NLS-1$
740:                    result = byte.class;
741:                } else if (className.equals("char")) { //$NON-NLS-1$
742:                    result = char.class;
743:                } else if (className.equals("short")) { //$NON-NLS-1$
744:                    result = short.class;
745:                } else if (className.equals("int")) { //$NON-NLS-1$
746:                    result = int.class;
747:                } else if (className.equals("long")) { //$NON-NLS-1$
748:                    result = long.class;
749:                } else if (className.equals("float")) { //$NON-NLS-1$
750:                    result = float.class;
751:                } else if (className.equals("double")) { //$NON-NLS-1$
752:                    result = double.class;
753:                } else if (className.equals("string")) { //$NON-NLS-1$
754:                    result = String.class;
755:                }
756:                return result;
757:            }
758:
759:            private boolean matchMethodParams(Method method,
760:                    Map<String, Command> references) {
761:                Class<?>[] paramTypes = method.getParameterTypes();
762:                Argument[] args = getArguments();
763:                if (args == null) {
764:                    return false;
765:                }
766:                boolean result = true;
767:                if (paramTypes.length == args.length) {
768:                    for (int j = 0; j < paramTypes.length; ++j) {
769:                        // System.out.println("paramTypes[j] = " + paramTypes[j]);
770:                        // System.out.println("args[j] = " + args[j].getType());
771:
772:                        boolean isAssignable = (args[j].getType() == null) ? !paramTypes[j]
773:                                .isPrimitive()
774:                                : paramTypes[j].isAssignableFrom(args[j]
775:                                        .getType());
776:
777:                        // System.out.println("args[j] = " + args[j].getType());
778:
779:                        if (!isAssignable) {
780:                            result = false;
781:                            break;
782:                        }
783:                    }
784:                } else {
785:                    result = false;
786:                }
787:                return result;
788:            }
789:
790:            public static Map<String, String> parseAttrs(String tagName,
791:                    Attributes attrs) {
792:                Map<String, String> result = new HashMap<String, String>();
793:                if (tagName.equals("object")) { //$NON-NLS-1$
794:                    for (String name : objectAttrNames) {
795:                        String value = attrs.getValue(name);
796:                        if (value != null) {
797:                            result.put(name, value);
798:                        }
799:                    }
800:                } else if (tagName.equals("void")) { //$NON-NLS-1$
801:                    for (String name : voidAttrNames) {
802:                        String value = attrs.getValue(name);
803:                        if (value != null) {
804:                            result.put(name, value);
805:                        }
806:                    }
807:                } else if (tagName.equals("array")) { //$NON-NLS-1$
808:                    for (String name : arrayAttrNames) {
809:                        String value = attrs.getValue(name);
810:                        if (value != null) {
811:                            result.put(name, value);
812:                        }
813:                    }
814:                } else if (tagName.equals("java")) { //$NON-NLS-1$
815:                    for (String name : javaAttrNames) {
816:                        String value = attrs.getValue(name);
817:                        if (value != null) {
818:                            result.put(name, value);
819:                        }
820:                    }
821:                }
822:                return result;
823:            }
824:
825:            // Auxiliary logging with tabs functions
826:            public static void pr(String msg) {
827:                // System.out.print(msg);
828:            }
829:
830:            public static void pr(int tabCount, String msg) {
831:                String result = ""; //$NON-NLS-1$
832:                for (int i = 0; i < tabCount; ++i) {
833:                    result += '\t';
834:                }
835:                result += msg;
836:                // System.out.print(result);
837:            }
838:
839:            public static void prn(String msg) {
840:                // System.out.println(msg);
841:            }
842:
843:            public static void prn(int tabCount, String msg) {
844:                String result = ""; //$NON-NLS-1$
845:                for (int i = 0; i < tabCount; ++i) {
846:                    result += '\t';
847:                }
848:                result += msg;
849:                // System.out.println(result);
850:            }
851:
852:            public static void printAttrs(int tabCount, String tagName,
853:                    Attributes attrs) {
854:                pr(tabCount, tabCount + ">in <" + tagName); //$NON-NLS-1$
855:                for (int i = 0; i < attrs.getLength(); ++i) {
856:                    String attrName = attrs.getQName(i);
857:                    String attrValue = attrs.getValue(i);
858:                    pr(" " + attrName + "=" + attrValue); //$NON-NLS-1$ //$NON-NLS-2$
859:                }
860:                prn(">"); //$NON-NLS-1$
861:            }
862:
863:            private static int initializeStatus(String tagName) {
864:                // return tagName.equals("java") ? Command.COMMAND_EXECUTED :
865:                // Command.INITIALIZED;
866:                return Command.INITIALIZED;
867:            }
868:
869:            // private static String translateStatus(int status) {
870:            // String result = "unknown";
871:            // if(status == Command.INITIALIZED) {
872:            // result = "initialized";
873:            // } else if(status == Command.CHILDREN_FILTERED) {
874:            // result = "children filtered";
875:            // } else if(status == Command.COMMAND_EXECUTED) {
876:            // result = "executed";
877:            // } else if(status == Command.CHILDREN_PROCESSED) {
878:            // result = "children processed";
879:            // }
880:            // return result;
881:            // }
882:
883:            private static final String[] objectAttrNames = {
884:                    "id", "idref", "class", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
885:                    "field", "method", "property", "index" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
886:
887:            private static final String[] voidAttrNames = {
888:                    "id", "class", "method", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
889:                    "property", "index" }; //$NON-NLS-1$ //$NON-NLS-2$
890:
891:            private static final String[] arrayAttrNames = {
892:                    "id", "class", "length" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
893:
894:            private static final String[] javaAttrNames = { "version", "class" }; //$NON-NLS-1$ //$NON-NLS-2$
895:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.