📄 formcontrol.java
字号:
final ElementContainer optionElementContainer=new ElementContainer(optionElement,true);
if (optionElementContainer.predefinedValue==null)
// use the content of the element if it has no value attribute
optionElementContainer.predefinedValue=CharacterReference.decodeCollapseWhiteSpace(optionElementContainer.element.getContent());
optionElementContainers[x++]=optionElementContainer;
}
}
public String getPredefinedValue() {
throw new UnsupportedOperationException("Use getPredefinedValues() method instead on SELECT controls");
}
public Collection<String> getPredefinedValues() {
final LinkedHashSet<String> linkedHashSet=new LinkedHashSet<String>(optionElementContainers.length*2,1.0F);
for (int i=0; i<optionElementContainers.length; i++)
linkedHashSet.add(optionElementContainers[i].predefinedValue);
return linkedHashSet;
}
public Iterator<Element> getOptionElementIterator() {
return new OptionElementIterator();
}
public boolean setValue(final String value) {
return addValue(value,false);
}
public boolean addValue(final String value) {
return addValue(value,formControlType==FormControlType.SELECT_MULTIPLE);
}
private boolean addValue(final String value, final boolean allowMultipleValues) {
boolean valueFound=false;
for (int i=0; i<optionElementContainers.length; i++) {
if (optionElementContainers[i].setSelected(value,Attribute.SELECTED,allowMultipleValues)) valueFound=true;
}
return valueFound;
}
void addValuesTo(final Collection<String> collection) {
for (int i=0; i<optionElementContainers.length; i++) {
if (optionElementContainers[i].getBooleanAttribute(Attribute.SELECTED))
addValueTo(collection,optionElementContainers[i].predefinedValue);
}
}
void addToFormFields(final FormFields formFields) {
for (int i=0; i<optionElementContainers.length; i++)
formFields.add(this,optionElementContainers[i].predefinedValue);
}
void replaceInOutputDocument(final OutputDocument outputDocument) {
if (outputStyle==FormControlOutputStyle.REMOVE) {
outputDocument.remove(getElement());
} else if (outputStyle==FormControlOutputStyle.DISPLAY_VALUE) {
final StringBuilder sb=new StringBuilder(100);
for (int i=0; i<optionElementContainers.length; i++) {
if (optionElementContainers[i].getBooleanAttribute(Attribute.SELECTED)) {
sb.append(getOptionLabel(optionElementContainers[i].element));
sb.append(FormControlOutputStyle.ConfigDisplayValue.MultipleValueSeparator);
}
}
if (sb.length()>0) sb.setLength(sb.length()-FormControlOutputStyle.ConfigDisplayValue.MultipleValueSeparator.length()); // remove last separator
outputDocument.replace(getElement(),getDisplayValueHTML(sb,false));
} else {
replaceAttributesInOutputDocumentIfModified(outputDocument);
for (int i=0; i<optionElementContainers.length; i++) {
optionElementContainers[i].replaceAttributesInOutputDocumentIfModified(outputDocument);
}
}
}
private static String getOptionLabel(final Element optionElement) {
final String labelAttributeValue=optionElement.getAttributeValue("label");
if (labelAttributeValue!=null) return labelAttributeValue;
return CharacterReference.decodeCollapseWhiteSpace(optionElement.getContent());
}
private final class OptionElementIterator implements Iterator<Element> {
private int i=0;
public boolean hasNext() {
return i<optionElementContainers.length;
}
public Element next() {
if (!hasNext()) throw new NoSuchElementException();
return optionElementContainers[i++].element;
}
public void remove() {
throw new UnsupportedOperationException();
}
}
}
final String getDisplayValueHTML(final CharSequence text, final boolean whiteSpaceFormatting) {
final StringBuilder sb=new StringBuilder((text==null ? 0 : text.length()*2)+50);
sb.append('<').append(FormControlOutputStyle.ConfigDisplayValue.ElementName);
try {
for (String attributeName : FormControlOutputStyle.ConfigDisplayValue.AttributeNames) {
final CharSequence attributeValue=elementContainer.getAttributeValue(attributeName);
if (attributeValue==null) continue;
Attribute.appendHTML(sb,attributeName,attributeValue);
}
sb.append('>');
if (text==null || text.length()==0)
sb.append(FormControlOutputStyle.ConfigDisplayValue.EmptyHTML);
else
CharacterReference.appendEncode(sb,text,whiteSpaceFormatting);
} catch (IOException ex) {throw new RuntimeException(ex);} // never happens
sb.append(EndTagType.START_DELIMITER_PREFIX).append(FormControlOutputStyle.ConfigDisplayValue.ElementName).append('>');
return sb.toString();
}
final void replaceAttributesInOutputDocumentIfModified(final OutputDocument outputDocument) {
elementContainer.replaceAttributesInOutputDocumentIfModified(outputDocument);
}
static List<FormControl> getAll(final Segment segment) {
final ArrayList<FormControl> list=new ArrayList<FormControl>();
getAll(segment,list,HTMLElementName.INPUT);
getAll(segment,list,HTMLElementName.TEXTAREA);
getAll(segment,list,HTMLElementName.SELECT);
getAll(segment,list,HTMLElementName.BUTTON);
Collections.sort(list,COMPARATOR);
return list;
}
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);
}
}
private static String getString(final char ch, final int length) {
if (length==0) return "";
final StringBuilder sb=new StringBuilder(length);
for (int i=0; i<length; i++) sb.append(ch);
return sb.toString();
}
private void verifyName() {
if (formControlType.isSubmit()) return;
String missingOrBlank;
if (name==null) {
missingOrBlank="missing";
} else {
if (name.length()!=0) return;
missingOrBlank="blank";
}
final Source source=getElement().source;
if (source.logger.isInfoEnabled()) source.logger.info(getElement().source.getRowColumnVector(getElement().begin).appendTo(new StringBuilder(200)).append(": compulsory \"name\" attribute of ").append(formControlType).append(" control is ").append(missingOrBlank).toString());
}
private static final void addValueTo(final Collection<String> collection, final String value) {
collection.add(value!=null ? value : "");
}
private static final class PositionComparator implements Comparator<FormControl> {
public int compare(final FormControl formControl1, final FormControl formControl2) {
final int formControl1Begin=formControl1.getElement().getBegin();
final int formControl2Begin=formControl2.getElement().getBegin();
if (formControl1Begin<formControl2Begin) return -1;
if (formControl1Begin>formControl2Begin) return 1;
return 0;
}
}
//////////////////////////////////////////////////////////////////////////////////////
static final class ElementContainer {
// Contains the information common to both a FormControl and to each OPTION element
// within a SELECT FormControl
public final Element element;
public Map<String,String> attributesMap=null;
public String predefinedValue; // never null for option, checkbox or radio elements
public ElementContainer(final Element element, final boolean loadPredefinedValue) {
this.element=element;
predefinedValue=loadPredefinedValue ? element.getAttributes().getValue(Attribute.VALUE) : null;
}
public Map<String,String> getAttributesMap() {
if (attributesMap==null) attributesMap=element.getAttributes().getMap(true);
return attributesMap;
}
public boolean setSelected(final String value, final String selectedOrChecked, final boolean allowMultipleValues) {
if (value!=null && predefinedValue.equals(value.toString())) {
setBooleanAttribute(selectedOrChecked,true);
return true;
}
if (!allowMultipleValues) setBooleanAttribute(selectedOrChecked,false);
return false;
}
public String getAttributeValue(final String attributeName) {
if (attributesMap!=null)
return attributesMap.get(attributeName);
else
return element.getAttributes().getValue(attributeName);
}
public void setAttributeValue(final String attributeName, final String value) {
// null value indicates attribute should be removed.
if (value==null) {
setBooleanAttribute(attributeName,false);
return;
}
if (attributesMap!=null) {
attributesMap.put(attributeName,value);
return;
}
final String existingValue=getAttributeValue(attributeName);
if (existingValue!=null && existingValue.equals(value)) return;
getAttributesMap().put(attributeName,value);
}
public boolean getBooleanAttribute(final String attributeName) {
if (attributesMap!=null)
return attributesMap.containsKey(attributeName);
else
return element.getAttributes().get(attributeName)!=null;
}
public void setBooleanAttribute(final String attributeName, final boolean value) {
final boolean oldValue=getBooleanAttribute(attributeName);
if (value==oldValue) return;
if (value)
getAttributesMap().put(attributeName,attributeName); // xhtml compatible attribute
else
getAttributesMap().remove(attributeName);
}
public void replaceAttributesInOutputDocumentIfModified(final OutputDocument outputDocument) {
if (attributesMap!=null) outputDocument.replace(element.getAttributes(),attributesMap);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -