private static void getAll(final Segment segment, final ArrayList<FormControl> list, final String tagName) { for (Element element : segment.getAllElements(tagName)) { final FormControl formControl=element.getFormControl(); if (formControl!=null) list.add(formControl); } }
/** * Returns a list of all {@link Element} objects with start tags of the specified {@linkplain StartTagType type} that are {@linkplain #encloses(Segment) enclosed} by this segment. * <p> * The elements returned correspond with the start tags returned in the {@link #getAllTags(TagType)} method, * except that elements which are not entirely enclosed by this segment are excluded. * <p> * If this segment is itself an {@link Element} with the specified type, the result includes this element in the list. * * @param startTagType the {@linkplain StartTagType type} of start tags to get, must not be <code>null</code>. * @return a list of all {@link Element} objects with start tags of the specified {@linkplain StartTagType type} that are {@linkplain #encloses(Segment) enclosed} by this segment. */ public List<Element> getAllElements(final StartTagType startTagType) { if (startTagType==null) throw new IllegalArgumentException("startTagType argument must not be null"); return getAllElements(getAllStartTags(startTagType)); }
/** * Returns a list of all {@link Element} objects that are {@linkplain #encloses(Segment) enclosed} by this segment. * <p> * The {@link Source#fullSequentialParse()} method should be called after construction of the {@link Source} object * if this method is to be used on a large proportion of the source. * It is called automatically if this method is called on the {@link Source} object itself. * <p> * The elements returned correspond exactly with the start tags returned in the {@link #getAllStartTags()} method. * <p> * If this segment is itself an {@link Element}, the result includes this element in the list. * * @return a list of all {@link Element} objects that are {@linkplain #encloses(Segment) enclosed} by this segment. */ public List<Element> getAllElements() { return getAllElements(getAllStartTags()); }
/** * Returns a list of all {@link Element} objects with the specified class that are {@linkplain #encloses(Segment) enclosed} by this segment. * <p> * This matches elements with a <code>class</code> attribute that contains the specified class name, either as an exact match or where the specified class name is one of multiple * class names separated by white space in the attribute value. * <p> * The elements returned correspond with the start tags returned in the {@link #getAllStartTagsByClass(String className)} method, * except that elements which are not entirely enclosed by this segment are excluded. * <p> * If this segment is itself an {@link Element} with the specified class, the result includes this element in the list. * * @param className the class name (case sensitive) to search for, must not be <code>null</code>. * @return a list of all {@link Element} objects with the specified class that are {@linkplain #encloses(Segment) enclosed} by this segment. */ public List<Element> getAllElementsByClass(final String className) { return getAllElements(getAllStartTagsByClass(className)); }
/** * Returns a list of all {@link Element} objects with the specified attribute name/value pair that are {@linkplain #encloses(Segment) enclosed} by this segment. * <p> * The elements returned correspond with the start tags returned in the {@link #getAllStartTags(String attributeName, String value, boolean valueCaseSensitive)} method, * except that elements which are not entirely enclosed by this segment are excluded. * <p> * If this segment is itself an {@link Element} with the specified name/value pair, the result includes this element in the list. * * @param attributeName the attribute name (case insensitive) to search for, must not be <code>null</code>. * @param value the value of the specified attribute to search for, must not be <code>null</code>. * @param valueCaseSensitive specifies whether the attribute value matching is case sensitive. * @return a list of all {@link Element} objects with the specified attribute name/value pair that are {@linkplain #encloses(Segment) enclosed} by this segment. * @see #getAllElements(String attributeName, Pattern valueRegexPattern) */ public List<Element> getAllElements(final String attributeName, final String value, final boolean valueCaseSensitive) { return getAllElements(getAllStartTags(attributeName,value,valueCaseSensitive)); }
/** * Returns a list of all {@link Element} objects with the specified name that are {@linkplain #encloses(Segment) enclosed} by this segment. * <p> * The elements returned correspond with the start tags returned in the {@link #getAllStartTags(String name)} method, * except that elements which are not entirely enclosed by this segment are excluded. * <p> * Specifying a <code>null</code> argument to the <code>name</code> parameter is equivalent to {@link #getAllElements()}, which may include elements of non-{@linkplain StartTagType#NORMAL normal} tags. * <p> * This method also returns elements consisting of {@linkplain Tag#isUnregistered() unregistered} tags if the specified name is not a valid {@linkplain Tag#isXMLName(CharSequence) XML tag name}. * <p> * If this segment is itself an {@link Element} with the specified name, the result includes this element in the list. * * @param name the {@linkplain Element#getName() name} of the elements to get. * @return a list of all {@link Element} objects with the specified name that are {@linkplain #encloses(Segment) enclosed} by this segment. */ public List<Element> getAllElements(String name) { return getAllElements(getAllStartTags(name)); }
/** * Returns a list of all {@link Element} objects with the specified attribute name and value pattern that are {@linkplain #encloses(Segment) enclosed} by this segment. * <p> * The elements returned correspond with the start tags returned in the {@link #getAllStartTags(String attributeName, Pattern valueRegexPattern)} method, * except that elements which are not entirely enclosed by this segment are excluded. * <p> * Specifying a <code>null</code> argument to the <code>valueRegexPattern</code> parameter performs the search on the attribute name only, * without regard to the attribute value. This will also match an attribute that {@linkplain Attribute#hasValue() has no value} at all. * <p> * If this segment is itself an {@link Element} with the specified attribute name and value pattern, the result includes this element in the list. * * @param attributeName the attribute name (case insensitive) to search for, must not be <code>null</code>. * @param valueRegexPattern the regular expression pattern that must match the attribute value, may be <code>null</code>. * @return a list of all {@link Element} objects with the specified attribute name and value pattern that are {@linkplain #encloses(Segment) enclosed} by this segment. * @see #getAllElements(String attributeName, String value, boolean valueCaseSensitive) */ public List<Element> getAllElements(final String attributeName, final Pattern valueRegexPattern) { return getAllElements(getAllStartTags(attributeName,valueRegexPattern)); }
public static List<Segment> getStyleURISegments(final Segment segment) { if (segment==null || segment.length()==0) return Collections.emptyList(); if (segment.getFirstStartTag()==null) { // no start tags in this segment, assume the segment is a style attribute value int urlDelimiterStartPos=segment.getSource().getParseText().indexOf("url(",segment.getBegin(),segment.getEnd()); if (urlDelimiterStartPos==-1) return Collections.emptyList(); return addURLSegmentsFromCSS(new ArrayList<Segment>(),new Segment(segment.getSource(),urlDelimiterStartPos,segment.getEnd())); } List<Segment> uriSegments=new ArrayList<Segment>(); for (StartTag startTag : segment.getAllStartTags("style",null)) { addURLSegmentsFromCSS(uriSegments,startTag.getAttributes().get("style").getValueSegment()); } for (Element element : segment.getAllElements(HTMLElementName.STYLE)) { addURLSegmentsFromCSS(uriSegments,element.getContent()); } Collections.sort(uriSegments); return uriSegments; }