PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/netbeans-7.3/uihandler/src/org/netbeans/modules/uihandler/ButtonsParser.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 249 lines | 178 code | 23 blank | 48 comment | 41 complexity | 0060c5ede49fcc9de3009b2164179cf7 MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * If you wish your version of this file to be governed by only the CDDL
  28. * or only the GPL Version 2, indicate your decision by adding
  29. * "[Contributor] elects to include this software in this distribution
  30. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  31. * single choice of license, a recipient has the option to distribute
  32. * your version of this file under either the CDDL, the GPL Version 2 or
  33. * to extend the choice of license to its licensees as provided above.
  34. * However, if you add GPL Version 2 code and therefore, elected the GPL
  35. * Version 2 license, then the option applies only if the new code is
  36. * made subject to such option by the copyright holder.
  37. *
  38. * Contributor(s):
  39. *
  40. * Portions Copyrighted 2008 Sun Microsystems, Inc.
  41. */
  42. package org.netbeans.modules.uihandler;
  43. import java.io.ByteArrayInputStream;
  44. import java.io.IOException;
  45. import java.io.InputStream;
  46. import java.io.PushbackInputStream;
  47. import java.lang.reflect.InvocationTargetException;
  48. import java.util.ArrayList;
  49. import java.util.List;
  50. import javax.swing.JButton;
  51. import javax.xml.parsers.DocumentBuilder;
  52. import javax.xml.parsers.DocumentBuilderFactory;
  53. import javax.xml.parsers.ParserConfigurationException;
  54. import org.netbeans.modules.uihandler.Installer.Button;
  55. import org.openide.awt.Mnemonics;
  56. import org.w3c.dom.Document;
  57. import org.w3c.dom.Node;
  58. import org.w3c.dom.NodeList;
  59. import org.xml.sax.EntityResolver;
  60. import org.xml.sax.InputSource;
  61. import org.xml.sax.SAXException;
  62. /**
  63. *
  64. * @author Jindrich Sedek
  65. */
  66. final class ButtonsParser {
  67. private final InputStream is;
  68. private String title;
  69. private List<Object> options;
  70. private List<Object> additionalOptions;
  71. private boolean containsExitButton = false;
  72. private List<Node> nodes;
  73. private String url;
  74. public ButtonsParser(InputStream is) {
  75. this.is = new CuttingInputStream(is);
  76. }
  77. void parse() throws IOException, ParserConfigurationException, SAXException, InterruptedException, InvocationTargetException {
  78. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  79. factory.setValidating(false);
  80. factory.setIgnoringComments(true);
  81. DocumentBuilder builder = factory.newDocumentBuilder();
  82. PushbackInputStream isWithProlog = new PushbackInputStream(is, 255);
  83. byte[] xmlHeader = new byte[5];
  84. int len = isWithProlog.read(xmlHeader);
  85. isWithProlog.unread(xmlHeader, 0, len);
  86. if (len < 5 || xmlHeader[0] != '<' ||
  87. xmlHeader[1] != '?' ||
  88. xmlHeader[2] != 'x' ||
  89. xmlHeader[3] != 'm' ||
  90. xmlHeader[4] != 'l') {
  91. String header = "<?xml version='1.0' encoding='utf-8'?>";
  92. isWithProlog.unread(header.getBytes("utf-8"));
  93. }
  94. nodes = new ArrayList<Node>();
  95. builder.setEntityResolver(new EntityResolver() {
  96. //Avoid connecting out to get DTD
  97. public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
  98. if (systemId.equals("http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd")) {
  99. InputStream is = new ByteArrayInputStream(new byte[0]);
  100. return new InputSource(is);
  101. } else {
  102. return null;
  103. }
  104. }
  105. });
  106. Document doc = builder.parse(isWithProlog);
  107. NodeList forms = doc.getElementsByTagName("form");
  108. for (int i = 0; i < forms.getLength(); i++) {
  109. String action = forms.item(i).getAttributes().getNamedItem("action").getNodeValue();
  110. if ((action == null) || ("".equals(action))) {
  111. throw new IllegalStateException("Action should not be empty");
  112. }
  113. url = action;
  114. NodeList inputs = doc.getElementsByTagName("input");
  115. for (int j = 0; j < inputs.getLength(); j++) {
  116. if (isChild(inputs.item(j), forms.item(i))) {
  117. org.w3c.dom.Node in = inputs.item(j);
  118. String type = attrValue(in, "type");
  119. if ("hidden".equals(type)) { // NOI18N
  120. nodes.add(in);
  121. }
  122. }
  123. }
  124. }
  125. NodeList titlesList = doc.getElementsByTagName("title");
  126. for (int i = 0; i < titlesList.getLength(); i++) {
  127. String t = titlesList.item(i).getTextContent();
  128. if (t != null) {
  129. title = t;
  130. break;
  131. }
  132. }
  133. }
  134. public void createButtons() {
  135. options = new ArrayList<Object>();
  136. additionalOptions = new ArrayList<Object>();
  137. for (Node node : nodes) {
  138. String name = attrValue(node, "name");
  139. String value = attrValue(node, "value");
  140. String align = attrValue(node, "align");
  141. String alt = attrValue(node, "alt");
  142. //Incorrect value but we keep it here for backward compatibility
  143. //Correct value of atribute "disabled" is "disabled"
  144. boolean enabled = true;
  145. if ("true".equals(attrValue(node, "disabled"))) { // NOI18N
  146. enabled = false;
  147. } else if ("disabled".equals(attrValue(node, "disabled"))) { // NOI18N
  148. enabled = false;
  149. }
  150. List<Object> addTo = "left".equals(align) ? additionalOptions : options;
  151. if (Button.isSubmitTrigger(name)) { // NOI18N
  152. String submitValue = value;
  153. JButton b = new JButton();
  154. Mnemonics.setLocalizedText(b, submitValue);
  155. b.setActionCommand(name); // NOI18N
  156. b.putClientProperty("url", url); // NOI18N
  157. b.setDefaultCapable(addTo.isEmpty() && addTo == options);
  158. b.putClientProperty("alt", alt); // NOI18N
  159. b.putClientProperty("now", submitValue); // NOI18N
  160. b.setEnabled(enabled);
  161. addTo.add(b);
  162. } else {
  163. JButton b = new JButton();
  164. Mnemonics.setLocalizedText(b, value);
  165. b.setActionCommand(name);
  166. b.setDefaultCapable(addTo.isEmpty() && addTo == options);
  167. b.putClientProperty("alt", alt); // NOI18N
  168. b.putClientProperty("now", value); // NOI18N
  169. b.setEnabled(enabled && Button.isKnown(name));
  170. addTo.add(b);
  171. if (Button.EXIT.isCommand(name)) {
  172. containsExitButton = true;
  173. }
  174. if (Button.REDIRECT.isCommand(name)) {
  175. b.putClientProperty("url", url); // NOI18N
  176. }
  177. }
  178. }
  179. }
  180. List<Object> getOptions() {
  181. return options;
  182. }
  183. List<Object> getAditionalOptions() {
  184. return additionalOptions;
  185. }
  186. String getTitle() {
  187. return title;
  188. }
  189. boolean containsExitButton() {
  190. return containsExitButton;
  191. }
  192. private static String attrValue(org.w3c.dom.Node in, String attrName) {
  193. org.w3c.dom.Node n = in.getAttributes().getNamedItem(attrName);
  194. return n == null ? null : n.getNodeValue();
  195. }
  196. private static boolean isChild(org.w3c.dom.Node child, org.w3c.dom.Node parent) {
  197. while (child != null) {
  198. if (child == parent) {
  199. return true;
  200. }
  201. child = child.getParentNode();
  202. }
  203. return false;
  204. }
  205. private static class CuttingInputStream extends InputStream {
  206. private static int[] TERMINAL_PATTERN = new int[]{'<', '/', 'h', 't', 'm', 'l', '>'};
  207. private final InputStream br;
  208. private int processed = 0;
  209. public CuttingInputStream(InputStream originalIS) {
  210. br = originalIS;
  211. }
  212. @Override
  213. public int read() throws IOException {
  214. if (processed >= TERMINAL_PATTERN.length){
  215. return -1;
  216. }
  217. int next = br.read();
  218. if (next == TERMINAL_PATTERN[processed]) {
  219. ++processed;
  220. } else {
  221. processed = 0;
  222. }
  223. return next;
  224. }
  225. }
  226. }