All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cat.inspiracio.html.HTMLFormControlsCollectionImp Maven / Gradle / Ivy

Go to download

HTML-parser provides a parser for HTML 5 that produces HTML 5 document object model. It aims to be a Java-implementation of http://www.w3.org/TR/html5/. It is for use in the server. It does not implement features that are relevant in the client, like event handling. It is for use from javascript, via Java's scripting library.

There is a newer version: 0.0.6
Show newest version
/*
Copyright 2015 Alexander Bunkenburg 

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cat.inspiracio.html;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import cat.inspiracio.dom.HTMLCollection;

/** The HTMLFormControlsCollection interface is used for collections of listed elements in form and fieldset elements. */
class HTMLFormControlsCollectionImp implements HTMLFormControlsCollection {
	private static final long serialVersionUID = -2810559074217028999L;
/* It would be nice to let this extends HTMLCollectionImp, but the return type of namedItem(name) is wrong. */
	
	// state ---------------------------------------------------
	
	protected Listelements=new ArrayList<>();
	
	private Filter filter;
	
	// construction --------------------------------------------
	
	/** The elements IDL attribute must return an HTMLFormControlsCollection rooted at the fieldset element, 
	 * whose filter matches listed elements. */
	HTMLFormControlsCollectionImp(HTMLFieldSetElement f){
		filter=new Filter(){
			@Override public boolean accept(HTMLElement e) {
				return e instanceof ListedElement;
			}};
		HTMLCollectionchildren=f.getChildElements();
		for(int i=0; ichildren=element.getChildElements();
		for(HTMLElement child : children)
			addAll(child);
	}

	public void add(ListedElement e){elements.add(e);}

	// accessors -----------------------------------------------

	@Override public int getLength(){return elements.size();}

	@Override public ListedElement item(int index){
		if(index<0 || elements.size()<=index)
			return null;
		return elements.get(index);
	}

	/** Returns null, an element, or a radio node list. */
	@Override public Object namedItem(String name){
		if(name==null || 0==name.length())return null;
		
		RadioNodeListImp radios=new RadioNodeListImp();
		for(ListedElement e : elements){
			boolean in= name.equals(e.getId()) || name.equals(e.getName());
			if(in)
				radios.add(e);
		}
		
		int length=radios.getLength();
		if(length==0)return null;
		if(length==1)return radios.item(0);
		return radios;
	}

	// helpers ----------------------------------------
	
	@Override public String toString(){return elements.toString();}
	@Override public Iterator iterator(){return elements.iterator();}
	
	private static interface Filter{
		boolean accept(HTMLElement e);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy