|
| 1 | +package net.sourceforge.jnlp.deploymentrules; |
| 2 | + |
| 3 | +import net.adoptopenjdk.icedteaweb.Assert; |
| 4 | +import net.adoptopenjdk.icedteaweb.IcedTeaWebConstants; |
| 5 | +import net.adoptopenjdk.icedteaweb.JavaSystemProperties; |
| 6 | +import net.adoptopenjdk.icedteaweb.i18n.Translator; |
| 7 | +import net.adoptopenjdk.icedteaweb.jnlp.element.EntryPoint; |
| 8 | + |
| 9 | +import net.adoptopenjdk.icedteaweb.jvm.JvmUtils; |
| 10 | +import net.adoptopenjdk.icedteaweb.logging.Logger; |
| 11 | +import net.adoptopenjdk.icedteaweb.logging.LoggerFactory; |
| 12 | +import net.adoptopenjdk.icedteaweb.ui.swing.ScreenFinder; |
| 13 | +import net.adoptopenjdk.icedteaweb.xmlparser.ParseException; |
| 14 | +import net.adoptopenjdk.icedteaweb.xmlparser.XMLParser; |
| 15 | +import net.adoptopenjdk.icedteaweb.xmlparser.XmlNode; |
| 16 | + |
| 17 | + |
| 18 | +import javax.swing.JOptionPane; |
| 19 | +import java.awt.Rectangle; |
| 20 | +import java.net.MalformedURLException; |
| 21 | +import java.net.URL; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Locale; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.StringTokenizer; |
| 28 | +import java.util.regex.Matcher; |
| 29 | +import java.util.regex.Pattern; |
| 30 | + |
| 31 | +import static java.lang.Boolean.*; |
| 32 | +import static java.util.Arrays.asList; |
| 33 | + |
| 34 | +import static net.adoptopenjdk.icedteaweb.xmlparser.NodeUtils.getAttribute; |
| 35 | +import static net.adoptopenjdk.icedteaweb.xmlparser.NodeUtils.getChildNode; |
| 36 | +import static net.adoptopenjdk.icedteaweb.xmlparser.NodeUtils.getChildNodes; |
| 37 | +import static net.adoptopenjdk.icedteaweb.xmlparser.NodeUtils.getRequiredAttribute; |
| 38 | +import static net.adoptopenjdk.icedteaweb.xmlparser.NodeUtils.getRequiredURL; |
| 39 | +import static net.adoptopenjdk.icedteaweb.xmlparser.NodeUtils.getSpanText; |
| 40 | +import static net.adoptopenjdk.icedteaweb.xmlparser.NodeUtils.getURL; |
| 41 | + |
| 42 | + |
| 43 | +/** |
| 44 | +* Contains methods to parse an XML document into a DeploymentRuleSetFile. Implements JNLP |
| 45 | +* specification version 1.0. |
| 46 | +* |
| 47 | +* @author <a href="mailto:dhirenjoshi@gmail.com">Dhiren Joshi |
| 48 | +* (JAM)</a> - initial author |
| 49 | +* @version $Revision: 1.13 $ |
| 50 | +*/ |
| 51 | +public final class DeploymentRuleSetParser { |
| 52 | + |
| 53 | + private static final Logger LOG = LoggerFactory.getLogger(DeploymentRuleSetParser.class); |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + /** |
| 58 | + * the file reference |
| 59 | + */ |
| 60 | + private DeploymentRulesSet file=null; // do not use (uninitialized) |
| 61 | + |
| 62 | + /** |
| 63 | + * the root node |
| 64 | + */ |
| 65 | + private final XmlNode root; |
| 66 | + /** |
| 67 | + * whether to throw errors on non-fatal errors. |
| 68 | + */ |
| 69 | + private final boolean strict; // if strict==true parses a file with no error then strict==false should also |
| 70 | + |
| 71 | + /** |
| 72 | + * whether to allow extensions to the JNLP specification |
| 73 | + */ |
| 74 | + private final boolean allowExtensions; |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + /** |
| 79 | + * Create a parser for the Deployment rule set file |
| 80 | + * Reads the jar and ruleset.xml file is read and parsed. Adds a deploymentRuleSet tag to cover the legalities |
| 81 | + * If any with using a Oracle ruleset.xml. |
| 82 | + * |
| 83 | + * * @throws ParseException if the DeploymentRuleSet string is invalid |
| 84 | + */ |
| 85 | + /** |
| 86 | + * @param ruleSet the object created from the parsed ruleset.xml |
| 87 | + * @param root , the root XmlNode |
| 88 | + * @param settings , the parser settings |
| 89 | + * @throws ParseException |
| 90 | + */ |
| 91 | +public DeploymentRuleSetParser(final DeploymentRulesSet ruleSet, final XmlNode root, final ParserSettings settings) throws ParseException { |
| 92 | + this.file = file; |
| 93 | + this.root = root; |
| 94 | + this.strict = settings.isStrict(); |
| 95 | + this.allowExtensions = settings.isExtensionAllowed(); |
| 96 | + |
| 97 | + // ensure it's a DeploymentRuleSet node |
| 98 | + if (root == null || !root.getNodeName().equals(DeploymentRulesSet.DEPLOYMENTRULE_SET_ROOT_ELEMENT)) { |
| 99 | + throw new ParseException("Root element is not a DeploymentRuleset element."); |
| 100 | + } |
| 101 | + processXmlParsingOfRuleSet(ruleSet,root); |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | +/** |
| 108 | + * Returns the rule attributes populated |
| 109 | + * @param rule |
| 110 | + * @param root |
| 111 | + * @return rule object populated with attributes values |
| 112 | + */ |
| 113 | +public Rule getRuleIdAttributeValues(Rule rule, XmlNode root) { |
| 114 | + //certificate element |
| 115 | + final String hash = getAttribute(root, DeploymentRulesSet.HASH_ATTRIBUTE, null); |
| 116 | + //id element |
| 117 | + Certificate certs= new Certificate(); |
| 118 | + certs.setHash(hash); |
| 119 | + final String location = getAttribute(root, DeploymentRulesSet.LOCATION_ATTRIBUTE, null); |
| 120 | + rule.setcertificate(certs); |
| 121 | + rule.setLocation(location); |
| 122 | + return rule; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * |
| 127 | + * @param action |
| 128 | + * @param root |
| 129 | + * @return Action attributes sets |
| 130 | + */ |
| 131 | +public Action getActionAttributes(Action action, XmlNode root) { |
| 132 | + //action element |
| 133 | + final String permission = getAttribute(root, DeploymentRulesSet.PERMISSION_ATTRIBUTE, null); |
| 134 | + String version = getAttribute(root, DeploymentRulesSet.VERSION_ATTRIBUTE, null); |
| 135 | + action.setPermission(permission); |
| 136 | + action.setVersion(version); |
| 137 | + return action; |
| 138 | + } |
| 139 | + |
| 140 | +/** |
| 141 | + * @param ruleSet |
| 142 | + * @param parent |
| 143 | + * @throws ParseException |
| 144 | + */ |
| 145 | +public void processXmlParsingOfRuleSet(DeploymentRulesSet ruleSet, final XmlNode parent) throws ParseException { |
| 146 | + Rule rule = null; |
| 147 | + List<Rule> rules= new ArrayList<Rule>(); |
| 148 | + XmlNode child = parent.getFirstChild(); |
| 149 | + XmlNode childRuleSet =null; |
| 150 | + if (child.getNodeName().equals(DeploymentRulesSet.RULE_SET_ELEMENT)) { |
| 151 | + final XmlNode node = child; |
| 152 | + if (!child.getNodeName().equals(DeploymentRulesSet.RULE_SET_ELEMENT)) { |
| 153 | + throw new ParseException("Invalid Deployment rule set <ruleset> tag is missing"); |
| 154 | + }else { |
| 155 | + |
| 156 | + rules = getRules(child); |
| 157 | + } |
| 158 | + |
| 159 | + } |
| 160 | + ruleSet.setRuleSet(rules); |
| 161 | + |
| 162 | + } |
| 163 | + |
| 164 | + |
| 165 | + /** |
| 166 | + * @param parent |
| 167 | + * @return List<Rule> list of Rules from deplyoment rule set |
| 168 | + * @throws ParseException |
| 169 | + */ |
| 170 | +public List<Rule> getRules(final XmlNode parent) |
| 171 | + throws ParseException { |
| 172 | + final List<Rule> result = new ArrayList<Rule>(); |
| 173 | + final XmlNode rules[] = getChildNodes(parent, DeploymentRulesSet.RULE_ELEMENT); |
| 174 | + |
| 175 | + // ensure that there are at least one information section present |
| 176 | + if (rules.length == 0 ) { |
| 177 | + throw new ParseException("No rule <rule> element specified."); |
| 178 | + } |
| 179 | + for (final XmlNode rule : rules) { |
| 180 | + result.add(getRule(rule)); |
| 181 | + } |
| 182 | + return result; |
| 183 | + } |
| 184 | + |
| 185 | +/** |
| 186 | + * @return the Rule element at the specified node. |
| 187 | + * @param node |
| 188 | + * @return |
| 189 | + * @throws ParseException if the Rule eement does not exist |
| 190 | + */ |
| 191 | +private Rule getRule(final XmlNode node) throws ParseException { |
| 192 | + |
| 193 | + // create rules |
| 194 | + Rule rule=new Rule(); |
| 195 | + |
| 196 | + // step through the elements |
| 197 | + //first populate the id tag attribute |
| 198 | + XmlNode child = node.getFirstChild(); |
| 199 | + final String name = child.getNodeName(); |
| 200 | + if (name.equals(DeploymentRulesSet.ID_ELEMENT)) { |
| 201 | + getRuleIdAttributeValues(rule, child); |
| 202 | + } |
| 203 | + //next populate the action tag attribute. |
| 204 | + child = child.getNextSibling(); |
| 205 | + if (child.getNodeName().equals(DeploymentRulesSet.ACTION_ELEMENT)) { |
| 206 | + Action action= new Action(); |
| 207 | + rule.setAction(action); |
| 208 | + getActionAttributes(action, child); |
| 209 | + } |
| 210 | + return rule; |
| 211 | + } |
| 212 | + |
| 213 | +} |
| 214 | + |
0 commit comments