Source Code Cross Referenced for MultiVariableExpander.java in  » XML » digester » org » apache » commons » digester » substitution » 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 » XML » digester » org.apache.commons.digester.substitution 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* $Id: MultiVariableExpander.java 471661 2006-11-06 08:09:25Z skitching $
002:         *
003:         * Licensed to the Apache Software Foundation (ASF) under one or more
004:         * contributor license agreements.  See the NOTICE file distributed with
005:         * this work for additional information regarding copyright ownership.
006:         * The ASF licenses this file to You under the Apache License, Version 2.0
007:         * (the "License"); you may not use this file except in compliance with
008:         * the License.  You may obtain a copy of the License at
009:         * 
010:         *      http://www.apache.org/licenses/LICENSE-2.0
011:         * 
012:         * Unless required by applicable law or agreed to in writing, software
013:         * distributed under the License is distributed on an "AS IS" BASIS,
014:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015:         * See the License for the specific language governing permissions and
016:         * limitations under the License.
017:         */
018:
019:        package org.apache.commons.digester.substitution;
020:
021:        import java.util.Map;
022:        import java.util.ArrayList;
023:
024:        /**
025:         * <p>Expands variable references from multiple sources.</p>
026:         *
027:         * @since 1.6
028:         */
029:
030:        public class MultiVariableExpander implements  VariableExpander {
031:            private int nEntries = 0;
032:            private ArrayList markers = new ArrayList(2);
033:            private ArrayList sources = new ArrayList(2);
034:
035:            public MultiVariableExpander() {
036:            }
037:
038:            public void addSource(String marker, Map source) {
039:                ++nEntries;
040:                markers.add(marker);
041:                sources.add(source);
042:            }
043:
044:            /*    
045:             * Expands any variable declarations using any of the known
046:             * variable marker strings.
047:             * 
048:             * @throws IllegalArgumentException if the input param references
049:             * a variable which is not known to the specified source.
050:             */
051:            public String expand(String param) {
052:                for (int i = 0; i < nEntries; ++i) {
053:                    param = expand(param, (String) markers.get(i),
054:                            (Map) sources.get(i));
055:                }
056:                return param;
057:            }
058:
059:            /**
060:             * Replace any occurrences within the string of the form
061:             * "marker{key}" with the value from source[key].
062:             * <p>
063:             * Commonly, the variable marker is "$", in which case variables
064:             * are indicated by ${key} in the string.
065:             * <p>
066:             * Returns the string after performing all substitutions.
067:             * <p>
068:             * If no substitutions were made, the input string object is
069:             * returned (not a copy).
070:             *
071:             * @throws IllegalArgumentException if the input param references
072:             * a variable which is not known to the specified source.
073:             */
074:            public String expand(String str, String marker, Map source) {
075:                String startMark = marker + "{";
076:                int markLen = startMark.length();
077:
078:                int index = 0;
079:                for (;;) {
080:                    index = str.indexOf(startMark, index);
081:                    if (index == -1) {
082:                        return str;
083:                    }
084:
085:                    int startIndex = index + markLen;
086:                    if (startIndex > str.length()) {
087:                        throw new IllegalArgumentException(
088:                                "var expression starts at end of string");
089:                    }
090:
091:                    int endIndex = str.indexOf("}", index + markLen);
092:                    if (endIndex == -1) {
093:                        throw new IllegalArgumentException(
094:                                "var expression starts but does not end");
095:                    }
096:
097:                    String key = str.substring(index + markLen, endIndex);
098:                    Object value = source.get(key);
099:                    if (value == null) {
100:                        throw new IllegalArgumentException("parameter [" + key
101:                                + "] is not defined.");
102:                    }
103:                    String varValue = value.toString();
104:
105:                    str = str.substring(0, index) + varValue
106:                            + str.substring(endIndex + 1);
107:                    index += varValue.length();
108:                }
109:            }
110:
111:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.