Process a file with server-side commands, reading from reader and writing the processed version to writer. NOTE: We really should be doing this in a streaming way rather than converting it to an array first. @param reader the reader to read the file containing SSIs from @param lastModifie
(Reader reader, long lastModifiedDate, PrintWriter writer)
| 115 | * 'normal' IOExceptions. |
| 116 | */ |
| 117 | public long process(Reader reader, long lastModifiedDate, PrintWriter writer) throws IOException { |
| 118 | SSIMediator ssiMediator = new SSIMediator(ssiExternalResolver, lastModifiedDate); |
| 119 | StringWriter stringWriter = new StringWriter(); |
| 120 | IOTools.flow(reader, stringWriter); |
| 121 | String fileContents = stringWriter.toString(); |
| 122 | int index = 0; |
| 123 | boolean inside = false; |
| 124 | StringBuilder command = new StringBuilder(); |
| 125 | try { |
| 126 | while (index < fileContents.length()) { |
| 127 | char c = fileContents.charAt(index); |
| 128 | if (!inside) { |
| 129 | if (c == COMMAND_START.charAt(0) && charCmp(fileContents, index, COMMAND_START)) { |
| 130 | inside = true; |
| 131 | index += COMMAND_START.length(); |
| 132 | command.setLength(0); // clear the command string |
| 133 | } else { |
| 134 | if (!ssiMediator.getConditionalState().processConditionalCommandsOnly) { |
| 135 | writer.write(c); |
| 136 | } |
| 137 | index++; |
| 138 | } |
| 139 | } else { |
| 140 | if (c == COMMAND_END.charAt(0) && charCmp(fileContents, index, COMMAND_END)) { |
| 141 | inside = false; |
| 142 | index += COMMAND_END.length(); |
| 143 | String strCmd = parseCmd(command); |
| 144 | if (debug > 0) { |
| 145 | ssiExternalResolver.log("SSIProcessor.process -- processing command: " + strCmd, null); |
| 146 | } |
| 147 | String[] paramNames = parseParamNames(command, strCmd.length()); |
| 148 | String[] paramValues = parseParamValues(command, strCmd.length(), paramNames.length); |
| 149 | // We need to fetch this value each time, since it may change during the loop |
| 150 | String configErrMsg = ssiMediator.getConfigErrMsg(); |
| 151 | SSICommand ssiCommand = commands.get(strCmd.toLowerCase(Locale.ENGLISH)); |
| 152 | String errorMessage = null; |
| 153 | if (ssiCommand == null) { |
| 154 | errorMessage = "Unknown command: " + strCmd; |
| 155 | } else if (paramValues == null) { |
| 156 | errorMessage = "Error parsing directive parameters."; |
| 157 | } else if (paramNames.length != paramValues.length) { |
| 158 | errorMessage = |
| 159 | "Parameter names count does not match parameter values count on command: " + strCmd; |
| 160 | } else { |
| 161 | /* |
| 162 | * Don't process the command if we are processing conditional commands only and the command |
| 163 | * is not conditional |
| 164 | */ |
| 165 | if (!ssiMediator.getConditionalState().processConditionalCommandsOnly || |
| 166 | ssiCommand instanceof SSIConditional) { |
| 167 | long lmd = ssiCommand.process(ssiMediator, strCmd, paramNames, paramValues, writer); |
| 168 | if (lmd > lastModifiedDate) { |
| 169 | lastModifiedDate = lmd; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | if (errorMessage != null) { |
| 174 | ssiExternalResolver.log(errorMessage, null); |
no test coverage detected