]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.specs/src/main/java/org/argeo/slc/process/SlcExecutionStep.java
Deal with special characters in logs
[gpl/argeo-slc.git] / runtime / org.argeo.slc.specs / src / main / java / org / argeo / slc / process / SlcExecutionStep.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.argeo.slc.process;
18
19 import java.util.ArrayList;
20 import java.util.Date;
21 import java.util.List;
22 import java.util.StringTokenizer;
23 import java.util.UUID;
24
25 public class SlcExecutionStep {
26 public final static String TYPE_START = "START";
27 public final static String TYPE_END = "END";
28 public final static String TYPE_PHASE_START = "PHASE_START";
29 public final static String TYPE_PHASE_END = "PHASE_END";
30 public final static String TYPE_LOG = "LOG";
31
32 private String uuid = UUID.randomUUID().toString();
33 private String type;
34 private Date begin = new Date();
35 private List<String> logLines = new ArrayList<String>();
36
37 /** Empty constructor */
38 public SlcExecutionStep() {
39 }
40
41 /** Creates a step of type LOG. */
42 public SlcExecutionStep(String log) {
43 this(TYPE_LOG, log);
44 }
45
46 /** Creates a step of the given type. */
47 public SlcExecutionStep(String type, String log) {
48 this.type = type;
49 addLog(log);
50 }
51
52 public String getUuid() {
53 return uuid;
54 }
55
56 public void setUuid(String uuid) {
57 this.uuid = uuid;
58 }
59
60 public String getType() {
61 return type;
62 }
63
64 public void setType(String type) {
65 this.type = type;
66 }
67
68 public Date getBegin() {
69 return begin;
70 }
71
72 public void setBegin(Date begin) {
73 this.begin = begin;
74 }
75
76 public List<String> getLogLines() {
77 return logLines;
78 }
79
80 public void setLogLines(List<String> logLines) {
81 this.logLines = logLines;
82 }
83
84 public void addLog(String log) {
85 if (log == null)
86 return;
87
88 StringTokenizer st = new StringTokenizer(log, "\n");
89 while (st.hasMoreTokens())
90 logLines.add(removeNonXmlChars(st.nextToken()));
91 }
92
93 /**
94 * Removes non XML compliant characters (from
95 * http://stackoverflow.com/questions
96 * /20762/how-do-you-remove-invalid-hexadecimal
97 * -characters-from-an-xml-based-data-source-pr)
98 */
99 private static String removeNonXmlChars(String inString) {
100 if (inString == null)
101 return null;
102
103 StringBuilder newString = new StringBuilder();
104 char ch;
105
106 for (int i = 0; i < inString.length(); i++) {
107
108 ch = inString.charAt(i);
109 // remove any characters outside the valid UTF-8 range as well as
110 // all control characters
111 // except tabs and new lines
112 if ((ch < 0x00FD && ch > 0x001F) || ch == '\t' || ch == '\n'
113 || ch == '\r') {
114 newString.append(ch);
115 }
116 }
117 return newString.toString();
118
119 }
120
121 @Override
122 public String toString() {
123 return getClass().getSimpleName() + "#" + uuid;
124 }
125
126 }