001: package simpleorm.simplewebapp.requestlet;
002:
003: import simpleorm.simplewebapp.core.*;
004:
005: /**
006: * Web services output as a properties file.
007: */
008: public class WPropertiesRenderer extends WPageRenderer {
009:
010: @Override
011: protected void doPage() throws Exception {
012: response.setContentType("text/plain"); // "text/xml;charset=utf-8"
013: outProperty("name", page.getPageName());
014: int err = 0;
015: for (WValidationException ex : page.getErrors()) {
016: outProperty("error." + err++, ex + "");
017: }
018:
019: for (WPagelet pl : page.getPageStructure().getPagelets()
020: .values())
021: doPagelet(pl);
022: }
023:
024: void doPagelet(WPagelet pagelet) throws Exception {
025: page.getPageStructure().setCurrentPagelet(pagelet); // not strictly necessary
026: for (WFieldGroup fg : pagelet.getFieldGroupsValues())
027: doFieldGroup(fg, pagelet.getName());
028: }
029:
030: void doFieldGroup(WFieldGroup group, String pagelet)
031: throws Exception {
032: if (group instanceof WFieldGroupList) {
033: WPageletList pl = (WPageletList) group.getPagelet();
034: while (pl.doListRow())
035: doRow(group, pl.getRowIndex(), pagelet + "."
036: + group.getName() + "." + pl.getRowIndex());
037: } else
038: doRow(group, 1, pagelet + "." + group.getName());
039: }
040:
041: void doRow(WFieldGroup group, int index, String rname)
042: throws Exception {
043: for (WField field : group.getValues())
044: outProperty(rname + "." + field.getName(), field.getText());
045: }
046:
047: public void outProperty(String property, String value)
048: throws Exception {
049: out.print(property);
050: out.print("=");
051: outPropertyValue(value);
052: out.print("\n");
053: }
054:
055: public void outPropertyValue(String value) throws Exception {
056: if (value == null)
057: return;
058: out.print(saveConvert(value, false));
059: }
060:
061: /*
062: * Converts unicodes to encoded \uxxxx and escapes
063: * special characters with a preceding slash
064: * Copeid from java.util.Properties
065: */
066: private String saveConvert(String theString, boolean escapeSpace) {
067: int len = theString.length();
068: int bufLen = len * 2;
069: if (bufLen < 0) {
070: bufLen = Integer.MAX_VALUE;
071: }
072: StringBuffer outBuffer = new StringBuffer(bufLen);
073:
074: for (int x = 0; x < len; x++) {
075: char aChar = theString.charAt(x);
076: // Handle common case first, selecting largest block that
077: // avoids the specials below
078: if ((aChar > 61) && (aChar < 127)) {
079: if (aChar == '\\') {
080: outBuffer.append('\\');
081: outBuffer.append('\\');
082: continue;
083: }
084: outBuffer.append(aChar);
085: continue;
086: }
087: switch (aChar) {
088: case ' ':
089: if (x == 0 || escapeSpace)
090: outBuffer.append('\\');
091: outBuffer.append(' ');
092: break;
093: case '\t':
094: outBuffer.append('\\');
095: outBuffer.append('t');
096: break;
097: case '\n':
098: outBuffer.append('\\');
099: outBuffer.append('n');
100: break;
101: case '\r':
102: outBuffer.append('\\');
103: outBuffer.append('r');
104: break;
105: case '\f':
106: outBuffer.append('\\');
107: outBuffer.append('f');
108: break;
109: case '=': // Fall through
110: case ':': // Fall through
111: case '#': // Fall through
112: case '!':
113: outBuffer.append('\\');
114: outBuffer.append(aChar);
115: break;
116: default:
117: if ((aChar < 0x0020) || (aChar > 0x007e)) {
118: outBuffer.append('\\');
119: outBuffer.append('u');
120: outBuffer.append(toHex((aChar >> 12) & 0xF));
121: outBuffer.append(toHex((aChar >> 8) & 0xF));
122: outBuffer.append(toHex((aChar >> 4) & 0xF));
123: outBuffer.append(toHex(aChar & 0xF));
124: } else {
125: outBuffer.append(aChar);
126: }
127: }
128: }
129: return outBuffer.toString();
130: }
131:
132: }
|