|
1 | | -/* |
2 | | - * Copyright (c) 2007-2019 LabKey Corporation |
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 | | -package org.labkey.flow; |
17 | | - |
18 | | -import org.apache.commons.lang3.StringUtils; |
19 | | -import org.apache.xmlbeans.XmlError; |
20 | | -import org.apache.xmlbeans.XmlException; |
21 | | -import org.apache.xmlbeans.XmlOptions; |
22 | | -import org.fhcrc.cpas.flow.script.xml.ScriptDef; |
23 | | -import org.fhcrc.cpas.flow.script.xml.ScriptDocument; |
24 | | -import org.labkey.api.util.StringUtilsLabKey; |
25 | | -import org.xml.sax.SAXParseException; |
26 | | - |
27 | | -import java.io.StringReader; |
28 | | -import java.util.ArrayList; |
29 | | -import java.util.List; |
30 | | - |
31 | | -public class ScriptParser |
32 | | -{ |
33 | | - List<Error> _errors; |
34 | | - ScriptDef _script; |
35 | | - |
36 | | - public ScriptParser() |
37 | | - { |
38 | | - } |
39 | | - |
40 | | - static public class Error |
41 | | - { |
42 | | - String _message; |
43 | | - int _line; |
44 | | - int _column; |
45 | | - |
46 | | - public Error(String message) |
47 | | - { |
48 | | - this(message, 0, 0); |
49 | | - } |
50 | | - |
51 | | - public Error(String message, int line, int column) |
52 | | - { |
53 | | - _message = message; |
54 | | - _line = line; |
55 | | - _column = column; |
56 | | - } |
57 | | - |
58 | | - public Error(SAXParseException spe) |
59 | | - { |
60 | | - this(spe.getLocalizedMessage(), spe.getLineNumber(), spe.getColumnNumber()); |
61 | | - } |
62 | | - |
63 | | - public String getMessage() |
64 | | - { |
65 | | - return _message; |
66 | | - } |
67 | | - |
68 | | - public int getLine() |
69 | | - { |
70 | | - return _line; |
71 | | - } |
72 | | - |
73 | | - public int getColumn() |
74 | | - { |
75 | | - return _column; |
76 | | - } |
77 | | - } |
78 | | - |
79 | | - public void parse(String script) |
80 | | - { |
81 | | - try |
82 | | - { |
83 | | - XmlOptions options = new XmlOptions(); |
84 | | - List<XmlError> errors = new ArrayList<>(); |
85 | | - options.setDocumentType(ScriptDocument.type); |
86 | | - script = StringUtils.replace(script, "<script>", "<script xmlns=\"" + ScriptDocument.type.getContentModel().getName().getNamespaceURI() + "\">"); |
87 | | - ScriptDocument doc = ScriptDocument.Factory.parse(new StringReader(script), options); |
88 | | - options.setErrorListener(errors); |
89 | | - |
90 | | - if (!doc.validate(options)) |
91 | | - { |
92 | | - for (XmlError xmlError : errors) |
93 | | - { |
94 | | - String message = xmlError.getMessage(); |
95 | | - message = StringUtils.replace(message, "@" + ScriptDocument.type.getContentModel().getName().getNamespaceURI(), ""); |
96 | | - String location = xmlError.getCursorLocation().xmlText(); |
97 | | - if (location.length() > 100) |
98 | | - location = StringUtilsLabKey.leftSurrogatePairFriendly(location, 100); |
99 | | - addError(new Error("Schema Validation Error: " + message + "\nLocation of invalid XML: " + location)); |
100 | | - } |
101 | | - } |
102 | | - _script = doc.getScript(); |
103 | | - } |
104 | | - catch (XmlException xmlException) |
105 | | - { |
106 | | - if (xmlException.getErrors().isEmpty()) |
107 | | - { |
108 | | - addError(new Error(xmlException.toString())); |
109 | | - } |
110 | | - else |
111 | | - { |
112 | | - for (XmlError xmlError : xmlException.getErrors()) |
113 | | - { |
114 | | - addError(new Error(xmlError.getMessage(), xmlError.getLine(), xmlError.getColumn() > 0 ? xmlError.getColumn() : 1)); |
115 | | - } |
116 | | - } |
117 | | - } |
118 | | - catch (Exception e) |
119 | | - { |
120 | | - addError(new Error(e.toString())); |
121 | | - } |
122 | | - } |
123 | | - |
124 | | - void addError(Error error) |
125 | | - { |
126 | | - if (_errors == null) |
127 | | - _errors = new ArrayList<>(); |
128 | | - _errors.add(error); |
129 | | - } |
130 | | - |
131 | | - public Error[] getErrors() |
132 | | - { |
133 | | - if (_errors == null || _errors.isEmpty()) |
134 | | - return null; |
135 | | - return _errors.toArray(new Error[0]); |
136 | | - } |
137 | | -} |
| 1 | +/* |
| 2 | + * Copyright (c) 2007-2019 LabKey Corporation |
| 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 | +package org.labkey.flow; |
| 17 | + |
| 18 | +import org.apache.commons.lang3.StringUtils; |
| 19 | +import org.apache.xmlbeans.XmlError; |
| 20 | +import org.apache.xmlbeans.XmlException; |
| 21 | +import org.apache.xmlbeans.XmlOptions; |
| 22 | +import org.fhcrc.cpas.flow.script.xml.ScriptDef; |
| 23 | +import org.fhcrc.cpas.flow.script.xml.ScriptDocument; |
| 24 | +import org.labkey.api.util.StringUtilsLabKey; |
| 25 | +import org.xml.sax.SAXParseException; |
| 26 | + |
| 27 | +import java.io.StringReader; |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | +public class ScriptParser |
| 32 | +{ |
| 33 | + List<Error> _errors; |
| 34 | + ScriptDef _script; |
| 35 | + |
| 36 | + public ScriptParser() |
| 37 | + { |
| 38 | + } |
| 39 | + |
| 40 | + static public class Error |
| 41 | + { |
| 42 | + String _message; |
| 43 | + int _line; |
| 44 | + int _column; |
| 45 | + |
| 46 | + public Error(String message) |
| 47 | + { |
| 48 | + this(message, 0, 0); |
| 49 | + } |
| 50 | + |
| 51 | + public Error(String message, int line, int column) |
| 52 | + { |
| 53 | + _message = message; |
| 54 | + _line = line; |
| 55 | + _column = column; |
| 56 | + } |
| 57 | + |
| 58 | + public Error(SAXParseException spe) |
| 59 | + { |
| 60 | + this(spe.getLocalizedMessage(), spe.getLineNumber(), spe.getColumnNumber()); |
| 61 | + } |
| 62 | + |
| 63 | + public String getMessage() |
| 64 | + { |
| 65 | + return _message; |
| 66 | + } |
| 67 | + |
| 68 | + public int getLine() |
| 69 | + { |
| 70 | + return _line; |
| 71 | + } |
| 72 | + |
| 73 | + public int getColumn() |
| 74 | + { |
| 75 | + return _column; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + public void parse(String script) |
| 80 | + { |
| 81 | + try |
| 82 | + { |
| 83 | + XmlOptions options = new XmlOptions(); |
| 84 | + List<XmlError> errors = new ArrayList<>(); |
| 85 | + options.setDocumentType(ScriptDocument.type); |
| 86 | + script = StringUtils.replace(script, "<script>", "<script xmlns=\"" + ScriptDocument.type.getContentModel().getName().getNamespaceURI() + "\">"); |
| 87 | + ScriptDocument doc = ScriptDocument.Factory.parse(new StringReader(script), options); |
| 88 | + options.setErrorListener(errors); |
| 89 | + |
| 90 | + if (!doc.validate(options)) |
| 91 | + { |
| 92 | + for (XmlError xmlError : errors) |
| 93 | + { |
| 94 | + String message = xmlError.getMessage(); |
| 95 | + message = StringUtils.replace(message, "@" + ScriptDocument.type.getContentModel().getName().getNamespaceURI(), ""); |
| 96 | + String location = xmlError.getCursorLocation().xmlText(); |
| 97 | + if (location.length() > 100) |
| 98 | + location = StringUtilsLabKey.leftSurrogatePairFriendly(location, 100); |
| 99 | + addError(new Error("Schema Validation Error: " + message + "\nLocation of invalid XML: " + location)); |
| 100 | + } |
| 101 | + } |
| 102 | + _script = doc.getScript(); |
| 103 | + } |
| 104 | + catch (XmlException xmlException) |
| 105 | + { |
| 106 | + if (xmlException.getErrors().isEmpty()) |
| 107 | + { |
| 108 | + addError(new Error(xmlException.toString())); |
| 109 | + } |
| 110 | + else |
| 111 | + { |
| 112 | + for (XmlError xmlError : xmlException.getErrors()) |
| 113 | + { |
| 114 | + addError(new Error(xmlError.getMessage(), xmlError.getLine(), xmlError.getColumn() > 0 ? xmlError.getColumn() : 1)); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + catch (Exception e) |
| 119 | + { |
| 120 | + addError(new Error(e.toString())); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + void addError(Error error) |
| 125 | + { |
| 126 | + if (_errors == null) |
| 127 | + _errors = new ArrayList<>(); |
| 128 | + _errors.add(error); |
| 129 | + } |
| 130 | + |
| 131 | + public Error[] getErrors() |
| 132 | + { |
| 133 | + if (_errors == null || _errors.isEmpty()) |
| 134 | + return null; |
| 135 | + return _errors.toArray(new Error[0]); |
| 136 | + } |
| 137 | +} |
0 commit comments