1 /* 2 * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH 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 de.smartics.maven.issue.util; 17 18 import java.util.regex.Matcher; 19 import java.util.regex.Pattern; 20 21 /** 22 * Grabs a hidden field's value from a HTML page. 23 */ 24 public final class Grabber 25 { 26 // ********************************* Fields ********************************* 27 28 // --- constants ------------------------------------------------------------ 29 30 /** 31 * The precompiled pattern to exactly match the hidden input field on a 32 * Bugzilla page that contains the token information. 33 */ 34 private static final Pattern TOKEN_PATTERN = Pattern.compile( 35 "<input\\s+type=\"hidden\"\\s+name=\"token\"\\s+value=\"([^\"]+)\"", 36 Pattern.MULTILINE); 37 38 /** 39 * The precompiled pattern to exactly match the title tag of a page. 40 */ 41 private static final Pattern TITLE_PATTERN = Pattern.compile( 42 "<title>\\s*([^<]+)\\s*</title>", Pattern.MULTILINE); 43 44 // --- members -------------------------------------------------------------- 45 46 /** 47 * The Bugzilla page to grab from. 48 */ 49 private final String page; 50 51 // ****************************** Initializer ******************************* 52 53 // ****************************** Constructors ****************************** 54 55 /** 56 * Default constructor. 57 * 58 * @param page the Bugzilla page to grab from. 59 */ 60 public Grabber(final String page) 61 { 62 this.page = page; 63 } 64 65 // ****************************** Inner Classes ***************************** 66 67 // ********************************* Methods ******************************** 68 69 // --- init ----------------------------------------------------------------- 70 71 // --- get&set -------------------------------------------------------------- 72 73 // --- business ------------------------------------------------------------- 74 75 /** 76 * Grabs the token. 77 * 78 * @return the token or <code>null</code>, if not present on the page. 79 */ 80 public String grabToken() 81 { 82 final Matcher matcher = TOKEN_PATTERN.matcher(page); 83 final boolean found = matcher.find(); 84 if (found) 85 { 86 final String token = matcher.group(1); 87 return token; 88 } 89 return null; 90 } 91 92 /** 93 * Grabs the title of a pahe. 94 * 95 * @return the title or <code>null</code>, if not present on the page. 96 */ 97 public String grabTitle() 98 { 99 final Matcher matcher = TITLE_PATTERN.matcher(page); 100 final boolean found = matcher.find(); 101 if (found) 102 { 103 final String title = matcher.group(1); 104 return title; 105 } 106 return null; 107 } 108 109 // --- object basics -------------------------------------------------------- 110 111 }