001 package org.apache.commons.digester3.binder; 002 003 /* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022 import java.util.HashMap; 023 import java.util.Map; 024 025 import org.apache.commons.digester3.SetPropertiesRule; 026 027 /** 028 * Builder chained when invoking {@link LinkedRuleBuilder#setProperties()}. 029 */ 030 public final class SetPropertiesBuilder 031 extends AbstractBackToLinkedRuleBuilder<SetPropertiesRule> 032 { 033 034 private final Map<String, String> aliases = new HashMap<String, String>(); 035 036 private boolean ignoreMissingProperty = true; 037 038 SetPropertiesBuilder( String keyPattern, String namespaceURI, RulesBinder mainBinder, 039 LinkedRuleBuilder mainBuilder ) 040 { 041 super( keyPattern, namespaceURI, mainBinder, mainBuilder ); 042 } 043 044 /** 045 * Add an additional attribute name to property name mapping. 046 * 047 * @param attributeName The attribute to match 048 * @param propertyName The java bean property to be assigned the value 049 * @return this builder instance 050 */ 051 public SetPropertiesBuilder addAlias( String attributeName, String propertyName ) 052 { 053 if ( attributeName == null ) 054 { 055 reportError( "setProperties().addAlias( String,String )", "empty 'attributeName' not allowed" ); 056 } 057 else 058 { 059 aliases.put( attributeName, propertyName ); 060 } 061 return this; 062 } 063 064 /** 065 * Add an attribute name to the ignore list. 066 * 067 * @param attributeName The attribute to match has to be ignored 068 * @return this builder instance 069 */ 070 public SetPropertiesBuilder ignoreAttribute( String attributeName ) 071 { 072 if ( attributeName == null ) 073 { 074 reportError( "setProperties().ignoreAttribute( String )", "empty 'attributeName' not allowed" ); 075 } 076 else 077 { 078 addAlias( attributeName, null ); 079 } 080 return this; 081 } 082 083 /** 084 * Sets whether attributes found in the XML without matching properties should be ignored. 085 * 086 * If set to false, the parsing will throw an {@code NoSuchMethodException} 087 * if an unmatched attribute is found. 088 * This allows to trap misspellings in the XML file. 089 * 090 * @param ignoreMissingProperty false to stop the parsing on unmatched attributes 091 * @return this builder instance 092 */ 093 public SetPropertiesBuilder ignoreMissingProperty( boolean ignoreMissingProperty ) 094 { 095 this.ignoreMissingProperty = ignoreMissingProperty; 096 return this; 097 } 098 099 /** 100 * {@inheritDoc} 101 */ 102 @Override 103 protected SetPropertiesRule createRule() 104 { 105 SetPropertiesRule rule = new SetPropertiesRule( aliases ); 106 rule.setIgnoreMissingProperty( ignoreMissingProperty ); 107 return rule; 108 } 109 110 }