1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
|
/*
Wotonomy: OpenStep design patterns for pure Java applications.
Copyright (C) 2000 Blacksmith, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see http://www.gnu.org
*/
package net.wotonomy.web;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.PushbackInputStream;
import java.io.StringReader;
import java.lang.reflect.InvocationTargetException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import net.wotonomy.control.EOKeyValueCodingSupport;
import net.wotonomy.foundation.NSArray;
import net.wotonomy.foundation.NSDictionary;
import net.wotonomy.foundation.NSMutableDictionary;
import net.wotonomy.foundation.NSSelector;
/**
* Pure java implementation of WOComponent.
*
* @author michael@mpowers.net
* @author $Author: cgruber $
* @version $Revision: 905 $
*/
public class WOComponent extends WOElement implements WOActionResults, net.wotonomy.control.EOKeyValueCodingAdditions,
net.wotonomy.control.EOKeyValueCoding {
WOElement rootElement;
private static final String DIRECTORY_SUFFIX = ".wo";
private static final String TEMPLATE_SUFFIX = ".html";
private static final String DECLARATION_SUFFIX = ".wod";
private static final String OPEN_TAG = "webobject";
private static final String CLOSE_TAG = "/webobject";
private static final String NAME_KEY = "name";
protected transient WOContext context; // don't persist
protected boolean cachingEnabled;
protected WOElement template;
protected WOComponent parent;
/**
* Default constructor. Deprecated in latest spec.
*/
public WOComponent() {
parent = null;
cachingEnabled = true;
template = null;
}
/**
* Constructor specifying a context.
*/
public WOComponent(WOContext aContext) {
this();
context = aContext;
}
/**
* Returns the name of the component, which is usually just the class name.
*/
public String name() {
return justTheClassName();
}
/**
* Returns the system-dependent file path to the current component directory,
* including the ".wo" extension.
*/
public String path() {
throw new RuntimeException("Not implemented yet.");
}
/**
* Returns the URL for this component, relative to the server's document root on
* the server's file system. This is not an http url.
*/
public String baseURL() {
throw new RuntimeException("Not implemented yet.");
}
/**
* Returns the name of the framework that contains this component, or null if
* the component does not belong to a framework. This currently returns the
* package path of the class, or null if it does not belong to a package.
*/
public String frameworkName() {
return justTheResourcePath();
}
/**
* Sets whether templates are cached. If true, templates will only be read once
* per application lifetime. Otherwise, templates will be read each time this
* class is instantiated. Defaults to false.
*/
public void setCachingEnabled(boolean enabled) {
cachingEnabled = enabled;
}
/**
* Returns whether templates are cached. If true, templates are read once per
* application lifetime. Otherwise, templates are read each time this class is
* instantiated.
*/
public boolean isCachingEnabled() {
return cachingEnabled && WOApplication.application().isCachingEnabled();
}
/**
* Returns the root of the tree of elements produced by parsing the templates in
* the component directory for this component.
*/
public WOElement template() {
return template;
}
/**
* Returns the root of the tree of elements produced by parsing the templates in
* the component directory for the named component.
*
* @deprecated Use template() instead.
*/
public WOElement templateWithName(String aComponentName) {
return templateWithName(aComponentName, null);
}
/**
* Returns the root of the tree of elements produced by parsing the templates in
* the component directory for the named component.
*/
WOElement templateWithName(String aComponentName, String aFramework) {
NSArray languages = null;
WOContext context = context();
if (context != null) {
languages = context.request().browserLanguages();
}
String htmlString = readTemplateResource(aComponentName, aFramework, TEMPLATE_SUFFIX, languages);
String declarationString = readTemplateResource(aComponentName, aFramework, DECLARATION_SUFFIX, languages);
WOElement result = templateWithHTMLString(htmlString, declarationString, languages);
if (result == null) {
System.out.println("WOComponent.templateWithName: failed for " + aComponentName);
}
return result;
}
/**
* Returns the root of the tree of elements produced by parsing the specified
* HTML string and bindings declaration string. Note: language list is currently
* ignored.
*/
public static WOElement templateWithHTMLString(String anHTMLString, String aDeclaration, List aLanguageList) {
if (anHTMLString == null)
return null;
WOElement result = null;
try {
NSDictionary<String, WOElementBindings> bindings = processDeclaration(aDeclaration);
List<WOElement> elements = new LinkedList<>();
int index = processTemplate(elements, anHTMLString, 0, bindings, aLanguageList);
if (index == -1) {
if (elements.size() == 1) {
result = (WOElement) elements.get(0);
} else {
result = new WOParentElement(elements);
}
} else // entire template did not process
{
throw new RuntimeException("No closing tag: " + anHTMLString.substring(index));
}
} catch (Exception exc) {
exc.printStackTrace();
}
return result;
}
/**
* Called at the beginning of a request-response cycle. Override to perform any
* necessary initialization. This implementation does nothing.
*/
public void awake() {
}
/**
* Package access only. Called to initialize the component with the proper
* context before the start of the request-response cycle. If the context has a
* current component, that component becomes this component's parent.
*/
void ensureAwakeInContext(WOContext aContext) {
context = aContext;
parent = aContext.parent();
if (template == null) {
template = templateWithName(name(), frameworkName());
}
if (template != null) {
template.ensureAwakeInContext(aContext);
}
awake();
}
public void takeValuesFromRequest(WORequest aRequest, WOContext aContext) {
if (synchronizesVariablesWithBindings()) {
pullValuesFromParent();
if (template != null) {
template.takeValuesFromRequest(aRequest, aContext);
}
pushValuesToParent();
} else if (template != null) {
template.takeValuesFromRequest(aRequest, aContext);
}
}
public WOActionResults invokeAction(WORequest aRequest, WOContext aContext) {
WOActionResults result = null;
if (synchronizesVariablesWithBindings()) {
pullValuesFromParent();
if (template != null) {
result = template.invokeAction(aRequest, aContext);
}
pushValuesToParent();
} else if (template != null) {
result = template.invokeAction(aRequest, aContext);
}
return result;
}
public void appendToResponse(WOResponse aResponse, WOContext aContext) {
if (synchronizesVariablesWithBindings()) {
pullValuesFromParent();
if (template != null) {
template.appendToResponse(aResponse, aContext);
}
pushValuesToParent();
} else if (template != null) {
template.appendToResponse(aResponse, aContext);
}
context = null;
}
/**
* Called at the end of a request-response cycle. Override to perform any
* necessary clean-up. This implementation does nothing.
*/
public void sleep() {
}
/**
* Generates a WOResponse and calls appendToResponse() on it.
*/
public WOResponse generateResponse() {
WOResponse response = new WOResponse();
WOContext context = context();
appendToResponse(response, context); // nulls out context
context.session().savePage(this); // ?is this the right place for this?
return response;
}
/**
* Returns this component's parent component, or null if none.
*/
public WOComponent parent() {
return parent;
}
/**
* Invokes the specified action on this component's parent. Variables will be
* synchronized when this method returns.
*/
public WOActionResults performParentAction(String anAction) {
WOActionResults result = parent().performAction(anAction);
if (synchronizesVariablesWithBindings()) {
pullValuesFromParent();
}
return result;
}
/**
* Invokes the specified action on this component.
*/
WOActionResults performAction(String anAction) {
try {
return (WOActionResults) NSSelector.invoke(anAction, this);
} catch (NoSuchMethodException exc) {
// returns below
} catch (InvocationTargetException exc) {
Throwable t = exc.getTargetException();
exc.printStackTrace();
throw new RuntimeException(t.toString());
} catch (Exception exc) {
exc.printStackTrace();
throw new RuntimeException(exc.toString());
}
return null;
}
/**
* Called before each phase of the request-response cycle, if
* synchronizesVariablesWithBindings is true and the component is not stateless.
*/
public void pullValuesFromParent() {
if (associations == null)
return;
String key;
Enumeration e = associations.keyEnumerator();
while (e.hasMoreElements()) {
key = e.nextElement().toString();
takeValueForKey(valueForBinding(key), key);
}
}
/**
* Called after each phase of the request-response cycle, if
* synchronizesVariablesWithBindings is true and the component is not stateless.
*/
public void pushValuesToParent() {
if (associations == null)
return;
String key;
Enumeration e = associations.keyEnumerator();
while (e.hasMoreElements()) {
key = e.nextElement().toString();
setValueForBinding(valueForKey(key), key);
}
}
/**
* Returns whether this component should be considered stateless. Stateless
* components are shared between sessions to conserve memory. This
* implementation returns false; override to return true.
*/
public boolean isStateless() {
return false;
}
/**
* Called only on stateless components to tell themselves to reset themselves
* for another invocation using a different context. This implementation does
* nothing.
*/
public void reset() {
// does nothing
}
/**
* Returns the application containing this instance of the class.
*/
public WOApplication application() {
return context.application();
}
/**
* Returns whether a session has been created for this user.
*/
public boolean hasSession() {
return context.hasSession();
}
/**
* Returns the current session object, creating it if it doesn't exist.
*/
public WOSession session() {
return context.session();
}
/**
* Returns the current context for this component.
*/
public WOContext context() {
return context;
}
/**
* Returns a new WOComponent with the specified name. If null, returns the
* component named "Main". If the named component doesn't exist, returns null.
*/
public WOComponent pageWithName(String aName) {
return application().pageWithName(aName, context());
}
/**
* Called when exceptions are raised by assigning values to this object. This
* implementation does nothing, but subclasses may override to do something
* useful.
*/
public void validationFailedWithException(Throwable anException, Object aValue, String aPath) {
// does nothing
}
/**
* Called on the component that represents the requested page. Override to
* return logging information specific to your component. This implementation
* returns the component's name.
*/
public String descriptionForResponse(WOResponse aResponse, WOContext aContext) {
return name();
}
/**
* Returns true if this component should get and set values in its parent. This
* implementation returns true. Override to create a component that does not
* automatically synchronize bindings with its parent, useful if you wish to
* handle synchronization manually.
*/
public boolean synchronizesVariablesWithBindings() {
return true;
}
/**
* Returns whether this component has a readable value that maps to the
* specified binding. This implementation calls hasBinding(aBinding).
*/
public boolean canGetValueForBinding(String aBinding) {
return hasBinding(aBinding);
}
/**
* Returns whether this component has a writable value that maps to the
* specified binding.
*/
public boolean canSetValueForBinding(String aBinding) {
WOAssociation assoc = (WOAssociation) associations.objectForKey(aBinding);
if (assoc != null) {
if (assoc.isValueSettable())
return true;
}
return false;
}
/**
* Returns whether this component has the specified binding.
*/
public boolean hasBinding(String aBinding) {
if (associations == null)
return false;
return associations.containsKey(aBinding);
}
/**
* Returns the value for the specified binding for this component. The parent
* component is expected to have set the binding for this component. If no such
* binding exists, the binding is treated as a property is and obtained using
* valueForKey. If the property is not found, this method returns null.
*/
public Object valueForBinding(String aBinding) {
WOComponent parent = parent();
if (associations != null) {
WOAssociation assoc = (WOAssociation) associations.objectForKey(aBinding);
if (assoc != null && parent != null) {
return assoc.valueInComponent(parent);
}
}
if (parent != null) {
return parent.valueForKey(aBinding);
}
return null;
}
/**
* Sets the value for the specified binding for this component. The parent
* component is expected to have set the binding for this component. If no such
* binding exists, the binding is treated as a property and is set using
* takeValueForKey. If the property is not found, this method fails silently.
*/
public void setValueForBinding(Object aValue, String aBinding) {
if (associations == null)
return;
WOComponent parent = parent();
if (associations != null) {
WOAssociation assoc = (WOAssociation) associations.objectForKey(aBinding);
if (assoc != null && parent != null) {
if (assoc.isValueSettable()) {
assoc.setValue(aValue, parent);
return;
}
}
}
if (parent != null) {
parent.takeValueForKey(aValue, aBinding);
}
}
public static void logString(String aString) {
System.out.println(aString);
}
public static void debugString(String aString) {
System.err.println(aString);
}
public Object valueForKeyPath(String aPath) {
// currently key value coding support also handles keypaths
return valueForKey(aPath);
}
public void takeValueForKeyPath(Object aValue, String aPath) {
// currently key value coding support also handles keypaths
takeValueForKey(aValue, aPath);
}
public NSDictionary valuesForKeys(List aKeyList) {
throw new RuntimeException("Not implemented yet.");
}
public void takeValuesFromDictionary(Map aValueMap) {
throw new RuntimeException("Not implemented yet.");
}
public Object valueForKey(String aKey) { // System.out.println( "valueForKey: " + aKey + "->" + this );
// handle "^" property keys
if (aKey.startsWith("^")) {
return valueForBinding(aKey.substring(1));
}
return EOKeyValueCodingSupport.valueForKey(this, aKey);
}
public void takeValueForKey(Object aValue, String aKey) { // System.out.println( "takeValueForKey: " + aKey + " : "
// + aValue + "->" + this );
// handle "^" property keys
if (aKey.startsWith("^")) {
setValueForBinding(aValue, aKey.substring(1));
return;
}
EOKeyValueCodingSupport.takeValueForKey(this, aValue, aKey);
}
public Object storedValueForKey(String aKey) {
return EOKeyValueCodingSupport.storedValueForKey(this, aKey);
}
public void takeStoredValueForKey(Object aValue, String aKey) {
EOKeyValueCodingSupport.takeStoredValueForKey(this, aValue, aKey);
}
public Object handleQueryWithUnboundKey(String aKey) {
return EOKeyValueCodingSupport.handleQueryWithUnboundKey(this, aKey);
}
public void handleTakeValueForUnboundKey(Object aValue, String aKey) {
EOKeyValueCodingSupport.handleTakeValueForUnboundKey(this, aValue, aKey);
}
public void unableToSetNullForKey(String aKey) {
EOKeyValueCodingSupport.unableToSetNullForKey(this, aKey);
}
public Object validateTakeValueForKeyPath(Object aValue, String aKey) {
throw new RuntimeException("Not implemented yet.");
}
// Template Processing
/**
* Takes a template string and a location to begin parsing, looking only for
* interesting tags, and calling itself recursively as necessary. Returns the
* index to resume parsing, or -1 if done.
*/
static private int processTemplate(List<WOElement> elements, String template, int index, Map<String, WOElementBindings> bindings, List aLanguageList)
throws java.io.IOException { // System.out.println( "processTemplate: " + index );
if (template == null)
return -1;
int start = index;
// TODO proper XML/HTML parsing?
while (true) {
// search for start of next tag
start = template.indexOf('<', start);
if (start == -1) {
// if no tags, send output and return
elements.add(new WOStaticElement(template.substring(index)));
return -1;
}
// search for end of opening tag
int end = template.indexOf(">", start + 1);
if (end == -1) {
// if no end to tag
throw new RuntimeException("No end to tag: " + template.substring(start));
}
boolean hasBody = true;
if (template.charAt(end - 1) == '/') {
// tag is standalone - no body
end = end - 1;
hasBody = false;
}
// search for name of tag
int endName = start + 1;
while (endName < end) {
if (Character.isWhitespace(template.charAt(endName)))
break;
endName++;
}
String name = template.substring(start + 1, endName);
if (name.toLowerCase().startsWith(OPEN_TAG)) {
// add the contents before the tag
// System.out.println( index + " : " + start + " : " + hasBody );
elements.add(new WOStaticElement(template.substring(index, start)));
// interesting tag; parse parameters
Map params = new HashMap(5); // arbitrary init length
if (endName < end) {
// delimit by whitespace
StringTokenizer tokens = new StringTokenizer(template.substring(endName + 1, end));
int equals;
String token;
String value;
while (tokens.hasMoreTokens()) {
token = tokens.nextToken();
equals = token.indexOf('=');
if (equals != -1) {
value = token.substring(equals + 1);
if (value.startsWith("\"")) {
// handle spaces within parameter names
while (!value.endsWith("\"")) {
value = value + " " + tokens.nextToken();
}
// strip quotation marks
if (value.endsWith("\"")) {
value = value.substring(1, value.length() - 1);
}
}
// register key with specified value
params.put(token.substring(0, equals).toLowerCase(), value);
} else {
// no value found, register the key name
params.put(token.toLowerCase(), "");
}
}
}
index = end + (hasBody ? 1 : 2);
WOElement body = null;
if (hasBody) {
List<WOElement> childElements = new LinkedList<>();
index = processTemplate(childElements, template, index, bindings, aLanguageList);
start = index;
if (index == -1) {
throw new RuntimeException("No closing tag found: " + template.substring(end));
}
if (childElements.size() == 1) {
body = (WOElement) childElements.get(0);
} else {
body = new WOParentElement(childElements);
}
}
WOElement element = null;
String nameProperty = (String) params.get(NAME_KEY);
WOElementBindings original;
if (!bindings.containsKey(nameProperty)) {
original = null;
System.err.println("No associations for: " + nameProperty);
System.err.println(bindings);
} else {
original = bindings.get(nameProperty);
}
NSDictionary<String, WOAssociation> associations;
if (original == null) {
associations = new NSMutableDictionary<String, WOAssociation>((NSDictionary<String, WOAssociation>)NSDictionary.EmptyDictionary);
} else {
associations = new NSMutableDictionary<String, WOAssociation>(original);
}
String elementClass = original.getElementClass();
WOApplication application = WOApplication.application();
element = application.dynamicElementWithName(elementClass, associations, body, aLanguageList);
if (element == null) {
// unable to create element: show assocs in static element
element = new WOStaticElement(associations.toString());
}
// System.out.println( element );
elements.add(element);
if (!hasBody) {
start = end + 2;
}
} else if (name.toLowerCase().startsWith(CLOSE_TAG)) {
// add any contents before the tag
elements.add(new WOStaticElement(template.substring(index, start)));
// return end + name.length() + 1; // "<" + ">" - 1 = 1
return end + (hasBody ? 1 : 2); // "<" + ">" - 1 = 1
} else {
// tag not interesting: continue
start = end + (hasBody ? 1 : 2);
}
}
}
// Utility Methods
static private void rewriteTag(String tagName, Map properties, String body, StringBuffer context)
throws java.io.IOException {
context.append("<" + tagName);
Iterator it = properties.keySet().iterator();
String key;
while (it.hasNext()) {
key = (String) it.next();
context.append(" " + key + "=\"" + properties.get(key) + "\"");
}
if (body == null) {
context.append("/>");
return;
}
context.append(">" + body + "</" + tagName + ">");
}
private String justTheClassName() {
String className = getClass().getName();
int index = className.lastIndexOf(".");
if (index == -1)
return className;
return className.substring(index + 1);
}
private String justTheResourcePath() {
int last = -1;
char[] src = getClass().getName().toCharArray();
char[] dst = new char[src.length];
for (int i = 0; i < src.length; i++) {
if (src[i] == '.') {
dst[i] = '/';
last = i;
} else {
dst[i] = src[i];
}
}
if (last == -1)
return null;
return new String(dst, 0, last);
}
private String readTemplateResource(String name, String framework, String suffix, NSArray languages) {
if (name == null)
return null;
name = name + DIRECTORY_SUFFIX + '/' + name + suffix;
InputStream is = null;
if (isCachingEnabled()) {
byte[] data = WOApplication.application().resourceManager().bytesForResourceNamed(name, framework,
languages);
if (data != null) {
is = new ByteArrayInputStream(data);
}
} else {
is = WOApplication.application().resourceManager().inputStreamForResourceNamed(name, framework, languages);
}
if (is == null) {
System.err.println("No resources found for: " + name);
return null;
}
// try to autodetect encoding
String encoding = "ISO8859_1";
try {
byte[] header = new byte[4];
is = new PushbackInputStream(is, 4);
is.read(header);
if (header[0] < 33 || header[0] > 126) {
// if any funny characters, presume UTF-16
encoding = "UTF-16";
if (!(header[1] < 33 || header[1] > 126)) {
// if second character is valid, presume UTF-8
encoding = "UTF-8";
}
}
// check byte-order-mark
if (header[0] == 0xef && header[1] == 0xbb && header[2] == 0xbf) // utf-8
{
encoding = "UTF-8";
} else if (header[0] == 0xfe && header[1] == 0xff) // utf-16
{
encoding = "UTF-16";
} else if (header[0] == 0 && header[1] == 0 && header[2] == 0xfe && header[3] == 0xff) // ucs-4
{
encoding = "UCS-4"; // ??
} else if (header[0] == 0xff && header[1] == 0xfe) // ucs-2le, ucs-4le, and
{
encoding = "UCS-16le"; // ??
}
// put back the header
((PushbackInputStream) is).unread(header);
} catch (Throwable t) {
t.printStackTrace();
System.err.println("Error while autodetecting encoding: should never happen");
}
try {
String line;
StringBuffer buf = new StringBuffer();
BufferedReader r = new BufferedReader(new InputStreamReader(is, encoding));
while ((line = r.readLine()) != null) {
buf.append(line);
buf.append('\n');
}
is.close(); // release the resource
String resourceBody = buf.toString();
return resourceBody;
} catch (IOException exc) {
System.err.println("Error while reading: " + name);
exc.printStackTrace();
return null;
}
}
// Declaration Parsing
/**
* Parses the declarations in the specified content and returns a map of element
* names to maps of attribute names to WOAssociations.
*/
private static NSDictionary<String, WOElementBindings> processDeclaration(String content) {
int index;
NSMutableDictionary<String, WOElementBindings> result = new NSMutableDictionary<>();
// strip out comments
StringBuffer stripped = new StringBuffer();
try {
LineNumberReader reader = new LineNumberReader(new StringReader(content));
String line;
while ((line = reader.readLine()) != null) {
index = line.indexOf("//");
while (index > -1) {
// (chochos) This used to truncate lines with quoted URLs
// in them. We have to check that the "//" is not inside quotes.
boolean quoted = false;
if (index > 0) {
for (int _position = 0; _position < index; _position++)
if (line.charAt(_position) == '"')
quoted = !quoted;
}
if (!quoted) {
line = line.substring(0, index);
index = -1;
} else {
// if we didn't truncate the line it's because the //
// were quoted. let's look for more, and check if they're not quoted...
index = line.indexOf("\"", index);
if (index > 0) {
index = line.indexOf("//", index);
}
}
}
stripped.append(line);
}
} catch (IOException exc) {
throw new RuntimeException("Error while stripping comments from declaration: " + stripped);
}
while ((index = stripped.toString().indexOf("/*")) != -1) {
int j = stripped.toString().indexOf("*/", index + 1);
if (j == -1)
break;
stripped.delete(index, j + 2);
}
String token;
StringTokenizer tokens = new StringTokenizer(stripped.toString(), "{}", true);
while (tokens.hasMoreTokens()) {
token = tokens.nextToken();
// next token is the name and class
String name, cl;
index = token.indexOf(":");
if (index > -1) {
name = token.substring(0, index).trim();
cl = token.substring(index + 1).trim();
} else {
System.err.println("Could not parse declaration:");
System.err.println(content);
throw new RuntimeException("Could not parse declaration: " + token);
}
// next token is the declaration for the name and class
if (!tokens.hasMoreTokens()) {
System.err.println("Could not find associations for declaration:");
System.err.println(content);
throw new RuntimeException("Could not find associations for declaration: " + name);
}
token = tokens.nextToken();
if (token.equals("{")) {
if (!tokens.hasMoreTokens())
throw new RuntimeException("Error parsing declaration: expected { but found: '" + token + "'");
token = tokens.nextToken();
}
NSMutableDictionary<String, WOAssociation> associations = new NSMutableDictionary<>();
if (!token.equals("}")) {
String line, key, value;
StringTokenizer lines = new StringTokenizer(token, ";");
while (lines.hasMoreElements()) {
line = lines.nextToken();
index = line.indexOf("=");
if (index > -1) {
if (line.length() == index + 1)
line += " ";
key = line.substring(0, index).trim();
value = line.substring(index + 1).trim();
} else {
// not a valid key: skip
key = null;
value = null;
}
if (key != null) {
// if in quotation marks
if ((value.startsWith("\"")) && (value.endsWith("\""))) {
// it's a constant value association
value = value.substring(1, value.length() - 1);
associations.put(key, WOAssociation.associationWithValue(value));
} else if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false")) {
// HACK: needed to be compatible with woextensions
// apparently true and false are allowed without quotes
associations.put(key, WOAssociation.associationWithValue(value));
} else {
// HACK: needed to be compatible with woextensions:
// apparently a standalone integer is allowed without quotes.
try {
Integer.parseInt(value); // does it parse?
associations.put(key, WOAssociation.associationWithValue(value));
} catch (NumberFormatException nfe) {
// did not parse:
// it's a key path association
associations.put(key, WOAssociation.associationWithKeyPath(value));
}
}
}
}
if (tokens.hasMoreTokens()) {
token = tokens.nextToken();
if (!token.equals("}"))
throw new RuntimeException("Error parsing declaration: expected } but found: '" + token + "'");
}
}
//associations.put(WOApplication.ELEMENT_CLASS, cl); // store classname
result.put(name, new WOElementBindings(associations, cl));
}
// System.out.println( "processDeclaration: " + result );
return result;
}
}
/*
* $Log$ Revision 1.2 2006/02/19 01:44:02 cgruber Add xmlrpc files Remove jclark
* and replace with dom4j and javax.xml.sax stuff Re-work dependencies and
* imports so it all compiles.
*
* Revision 1.1 2006/02/16 13:22:22 cgruber Check in all sources in
* eclipse-friendly maven-enabled packages.
*
* Revision 1.32 2003/08/07 00:15:14 chochos general cleanup (mostly removing
* unused imports)
*
* Revision 1.31 2003/07/24 00:23:21 chochos fixed problem with parsing wod
* files that have //-type comments. Quotes URL's would be truncated.
*
* Revision 1.30 2003/03/28 15:33:11 mpowers Now using a PushBackInputStream for
* auto detection of content encoding. No longer relying on markSupported()
* since jar input streams don't have it.
*
* Revision 1.29 2003/03/03 16:41:52 mpowers Bad characters in cvs log.
*
* Revision 1.28 2003/03/03 16:37:35 mpowers Better handling for string
* encodings. Now trying to autodetect unicode-formatted templates and
* declarations. Now handlings block-style comments in declarations.
*
* Revision 1.27 2003/01/28 19:33:51 mpowers Implemented the rest of
* WOResourceManager. Implemented support for java-style i18n. Components now
* use the resource manager to load templates.
*
* Revision 1.26 2003/01/24 20:13:22 mpowers Now accepting immutable
* NSDictionary in constructor, not Map.
*
* Revision 1.25 2003/01/21 17:53:45 mpowers Now correctly reporting error for
* missing bindings.
*
* Revision 1.24 2003/01/20 17:50:11 mpowers Caught a loop condition when same
* declaration was used twice.
*
* Revision 1.23 2003/01/19 22:33:25 mpowers Fixed problems with classpath and
* dynamic class loading. Dynamic elements now pass on ensureAwakeInContext.
* Parser how handles <standalone/> tags.
*
* Revision 1.22 2003/01/17 22:55:09 mpowers Straighted out the parent binding
* issue (I think). Fixes for woextensions compatibility.
*
* Revision 1.21 2003/01/17 20:34:57 mpowers Better handling for components and
* parents in the context's element stack.
*
* Revision 1.19 2003/01/17 15:32:22 mpowers Changes to better support generic
* elements and containers. Now preserving newlines in templates.
*
* Revision 1.17 2003/01/16 22:47:30 mpowers Compatibility changes to support
* compiling woextensions source. (34 out of 56 classes compile!)
*
* Revision 1.15 2003/01/16 15:50:43 mpowers More robust declaration parsing.
* Subcomponents are now supported. dynamicElementWithName can now return
* subcomponents.
*
* Revision 1.14 2003/01/15 19:50:49 mpowers Fixed issues with WOSession and
* Serializable. Can now persist sessions between classloaders (hot swap of
* class impls).
*
* Revision 1.13 2003/01/15 14:33:48 mpowers Refactoring: element id handling is
* now confined to WOParentElement. Other elements/components should not have to
* do element id incrementing.
*
* Revision 1.12 2003/01/14 16:05:12 mpowers Removed extraneous printlns.
*
* Revision 1.11 2003/01/13 22:24:25 mpowers Request-response cycle is working
* with session and page persistence.
*
* Revision 1.10 2003/01/10 19:33:28 mpowers Added contextID for the component
* url generation.
*
* Revision 1.9 2003/01/10 19:16:40 mpowers Implemented support for page
* caching.
*
* Revision 1.8 2003/01/09 21:16:48 mpowers Bringing request-response cycle more
* into conformance.
*
* Revision 1.7 2003/01/09 16:13:55 mpowers Implemented
* WOComponentRequestHandler: Bringing the request-response cycle more into
* conformance.
*
* Revision 1.6 2002/12/20 22:56:33 mpowers Reimplemented the template parsing
* again. Nested components are now correctly parsed. ElementID numbering is now
* working.
*
* Revision 1.3 2002/12/18 14:12:38 mpowers Support for differentiated request
* handlers. Support url generation for WOContext and WORequest.
*
* Revision 1.2 2002/11/07 18:52:33 mpowers New components courtesy of
* ezamudio@nasoft.com. Many thanks!
*
* Revision 1.1.1.1 2000/12/21 15:53:01 mpowers Contributing wotonomy.
*
* Revision 1.2 2000/12/20 16:25:49 michael Added log to all files.
*
*
*/
|