📄 complextype.html
字号:
<a name="580" href="#580">580</a> settings.getParent(), attribute.getName());<a name="581" href="#581">581</a> <a name="582" href="#582">582</a> <a href="../../../../org/archive/crawler/settings/ModuleAttributeInfo.html">ModuleAttributeInfo</a> localAttrInfo = (ModuleAttributeInfo) data<a name="583" href="#583">583</a> .getAttributeInfo(attribute.getName());<a name="584" href="#584">584</a> <a name="585" href="#585">585</a> <em class="comment">// Check if attribute exists</em><a name="586" href="#586">586</a> <strong>if</strong> (attrInfo == <strong>null</strong> && localAttrInfo == <strong>null</strong>) {<a name="587" href="#587">587</a> <strong>throw</strong> <strong>new</strong> AttributeNotFoundException(attribute.getName());<a name="588" href="#588">588</a> }<a name="589" href="#589">589</a> <a name="590" href="#590">590</a> <em class="comment">// Check if we are overriding and if that is allowed for this attribute</em><a name="591" href="#591">591</a> <strong>if</strong> (localAttrInfo == <strong>null</strong>) {<a name="592" href="#592">592</a> <strong>if</strong> (!attrInfo.isOverrideable()) {<a name="593" href="#593">593</a> <strong>throw</strong> <strong>new</strong> InvalidAttributeValueException(<a name="594" href="#594">594</a> <span class="string">"Attribute not overrideable: "</span> + attribute.getName());<a name="595" href="#595">595</a> }<a name="596" href="#596">596</a> localAttrInfo = <strong>new</strong> <a href="../../../../org/archive/crawler/settings/ModuleAttributeInfo.html">ModuleAttributeInfo</a>(attrInfo);<a name="597" href="#597">597</a> }<a name="598" href="#598">598</a> <a name="599" href="#599">599</a> <em class="comment">// Check if value is of correct type. If not, see if it is</em><a name="600" href="#600">600</a> <em class="comment">// a string and try to turn it into right type</em><a name="601" href="#601">601</a> Class typeClass = getDefinition(attribute.getName()).getLegalValueType();<a name="602" href="#602">602</a> <strong>if</strong> (!(typeClass.isInstance(value)) && value instanceof String) {<a name="603" href="#603">603</a> <strong>try</strong> {<a name="604" href="#604">604</a> value = SettingsHandler.StringToType((String) value,<a name="605" href="#605">605</a> SettingsHandler.getTypeName(typeClass.getName()));<a name="606" href="#606">606</a> } <strong>catch</strong> (ClassCastException e) {<a name="607" href="#607">607</a> <strong>throw</strong> <strong>new</strong> InvalidAttributeValueException(<a name="608" href="#608">608</a> <span class="string">"Unable to decode string '"</span> + value + <span class="string">"' into type '"</span><a name="609" href="#609">609</a> + typeClass.getName() + <span class="string">"'"</span>);<a name="610" href="#610">610</a> }<a name="611" href="#611">611</a> }<a name="612" href="#612">612</a> <a name="613" href="#613">613</a> <em class="comment">// Check if the attribute value is legal</em><a name="614" href="#614">614</a> FailedCheck error = checkValue(settings, attribute.getName(), value);<a name="615" href="#615">615</a> <strong>if</strong> (error != <strong>null</strong>) {<a name="616" href="#616">616</a> <strong>if</strong> (error.getLevel() == Level.SEVERE) {<a name="617" href="#617">617</a> <strong>throw</strong> <strong>new</strong> InvalidAttributeValueException(error.getMessage());<a name="618" href="#618">618</a> } <strong>else</strong> <strong>if</strong> (error.getLevel() == Level.WARNING) {<a name="619" href="#619">619</a> <strong>if</strong> (!getSettingsHandler().fireValueErrorHandlers(error)) {<a name="620" href="#620">620</a> <strong>throw</strong> <strong>new</strong> InvalidAttributeValueException(error.getMessage());<a name="621" href="#621">621</a> }<a name="622" href="#622">622</a> } <strong>else</strong> {<a name="623" href="#623">623</a> getSettingsHandler().fireValueErrorHandlers(error);<a name="624" href="#624">624</a> }<a name="625" href="#625">625</a> }<a name="626" href="#626">626</a> <a name="627" href="#627">627</a> <em class="comment">// Everything ok, set it</em><a name="628" href="#628">628</a> localAttrInfo.setType(value);<a name="629" href="#629">629</a> Object oldValue = data.put(attribute.getName(), localAttrInfo, value);<a name="630" href="#630">630</a> <a name="631" href="#631">631</a> <em class="comment">// If the attribute is a complex type other than the old value,</em><a name="632" href="#632">632</a> <em class="comment">// make sure that all sub attributes are correctly set</em><a name="633" href="#633">633</a> <strong>if</strong> (value instanceof ComplexType && value != oldValue) {<a name="634" href="#634">634</a> <a href="../../../../org/archive/crawler/settings/ComplexType.html">ComplexType</a> complex = (ComplexType) value;<a name="635" href="#635">635</a> replaceComplexType(settings, complex);<a name="636" href="#636">636</a> }<a name="637" href="#637">637</a> }<a name="638" href="#638">638</a> <a name="639" href="#639">639</a> <em>/**<em>*</em></em><a name="640" href="#640">640</a> <em> * Get the content type definition for an attribute.</em><a name="641" href="#641">641</a> <em> *</em><a name="642" href="#642">642</a> <em> * @param attributeName the name of the attribute to get definition for.</em><a name="643" href="#643">643</a> <em> * @return the content type definition for the attribute.</em><a name="644" href="#644">644</a> <em> */</em><a name="645" href="#645">645</a> <a href="../../../../org/archive/crawler/settings/Type.html">Type</a> getDefinition(String attributeName) {<a name="646" href="#646">646</a> <strong>return</strong> (Type) definitionMap.get(attributeName);<a name="647" href="#647">647</a> }<a name="648" href="#648">648</a> <a name="649" href="#649">649</a> <em>/**<em>*</em></em><a name="650" href="#650">650</a> <em> * Check an attribute to see if it fulfills all the constraints set on the</em><a name="651" href="#651">651</a> <em> * definition of this attribute.</em><a name="652" href="#652">652</a> <em> *</em><a name="653" href="#653">653</a> <em> * @param settings the CrawlerSettings object for which this check was</em><a name="654" href="#654">654</a> <em> * executed.</em><a name="655" href="#655">655</a> <em> * @param attributeName the name of the attribute to check.</em><a name="656" href="#656">656</a> <em> * @param value the value to check.</em><a name="657" href="#657">657</a> <em> * @return null if everything is ok, otherwise it returns a FailedCheck</em><a name="658" href="#658">658</a> <em> * object with detailed information of what went wrong.</em><a name="659" href="#659">659</a> <em> */</em><a name="660" href="#660">660</a> <strong>public</strong> FailedCheck checkValue(<a href="../../../../org/archive/crawler/settings/CrawlerSettings.html">CrawlerSettings</a> settings,<a name="661" href="#661">661</a> String attributeName, Object value) {<a name="662" href="#662">662</a> <strong>return</strong> checkValue(settings, attributeName,<a name="663" href="#663">663</a> getDefinition(attributeName), value);<a name="664" href="#664">664</a> }<a name="665" href="#665">665</a> <a name="666" href="#666">666</a> FailedCheck checkValue(<a href="../../../../org/archive/crawler/settings/CrawlerSettings.html">CrawlerSettings</a> settings, String attributeName,<a name="667" href="#667">667</a> <a href="../../../../org/archive/crawler/settings/Type.html">Type</a> definition, Object value) {<a name="668" href="#668">668</a> FailedCheck res = <strong>null</strong>;<a name="669" href="#669">669</a> <a name="670" href="#670">670</a> <em class="comment">// Check if value fulfills any constraints</em><a name="671" href="#671">671</a> List constraints = definition.getConstraints();<a name="672" href="#672">672</a> <strong>if</strong> (constraints != <strong>null</strong>) {<a name="673" href="#673">673</a> <strong>for</strong> (Iterator it = constraints.iterator(); it.hasNext()<a name="674" href="#674">674</a> && res == <strong>null</strong>;) {<a name="675" href="#675">675</a> res = ((Constraint) it.next()).check(settings, <strong>this</strong>,<a name="676" href="#676">676</a> definition, value);<a name="677" href="#677">677</a> }<a name="678" href="#678">678</a> }<a name="679" href="#679">679</a> <a name="680" href="#680">680</a> <strong>return</strong> res;<a name="681" href="#681">681</a> }<a name="682" href="#682">682</a> <a name="683" href="#683">683</a> <em>/**<em>* Unset an attribute on a per host level.</em></em><a name="684" href="#684">684</a> <em> *</em><a name="685" href="#685">685</a> <em> * This methods removes an override on a per host or per domain level.</em><a name="686" href="#686">686</a> <em> *</em><a name="687" href="#687">687</a> <em> * @param settings the settings object for which the attribute should be</em><a name="688" href="#688">688</a> <em> * unset.</em><a name="689" href="#689">689</a> <em> * @param name the name of the attribute.</em><a name="690" href="#690">690</a> <em> * @return The removed attribute or null if nothing was removed.</em><a name="691" href="#691">691</a> <em> * @throws AttributeNotFoundException is thrown if the attribute name</em><a name="692" href="#692">692</a> <em> * doesn't exist.</em><a name="693" href="#693">693</a> <em> */</em><a name="694" href="#694">694</a> <strong>public</strong> Object unsetAttribute(<a href="../../../../org/archive/crawler/settings/CrawlerSettings.html">CrawlerSettings</a> settings, String name)<a name="695" href="#695">695</a> throws AttributeNotFoundException {<a name="696" href="#696">696</a> <a name="697" href="#697">697</a> <strong>if</strong> (settings == globalSettings()) {<a name="698" href="#698">698</a> <strong>throw</strong> <strong>new</strong> IllegalArgumentException(<a name="699" href="#699">699</a> <span class="string">"Not allowed to unset attributes in Crawl Order."</span>);<a name="700" href="#700">700</a> }<a name="701" href="#701">701</a> <a name="702" href="#702">702</a> <a href="../../../../org/archive/crawler/settings/DataContainer.html">DataContainer</a> data = settings.getData(<strong>this</strong>);<a name="703" href="#703">703</a> <strong>if</strong> (data != <strong>null</strong> && data.containsKey(name)) {<a name="704" href="#704">704</a> <em class="comment">// Remove value</em><a name="705" href="#705">705</a> <strong>return</strong> data.removeElement(name);<a name="706" href="#706">706</a> }<a name="707" href="#707">707</a> <a name="708" href="#708">708</a> <em class="comment">// Value not found. Check if we should return null or throw an exception</em><a name="709" href="#709">709</a> <em class="comment">// This method throws an exception if not found.</em><a name="710" href="#710">710</a> Context context = <strong>new</strong> Context(settings, <strong>null</strong>);<a name="711" href="#711">711</a> getDataContainerRecursive(context, name);<a name="712" href="#712">712</a> <strong>return</strong> <strong>null</strong>;<a name="713" href="#713">713</a> }<a name="714" href="#714">714</a> <a name="715" href="#715">715</a> <strong>private</strong> <a href="../../../../org/archive/crawler/settings/DataContainer.html">DataContainer</a> getOrCreateDataContainer(<a href="../../../../org/archive/crawler/settings/CrawlerSettings.html">CrawlerSettings</a> settings)<a name="716" href="#716">716</a> throws InvalidAttributeValueException {<a name="717" href="#717">717</a> <a name="718" href="#718">718</a> <em class="comment">// Get this ComplexType's data container for the submitted settings</em><a name="719" href="#719">719</a> <a href="../../../../org/archive/crawler/settings/DataContainer.html">DataContainer</a> data = settings.getData(<strong>this</strong>);<a name="720" href="#720">720</a> <a name="721" href="#721">721</a> <em class="comment">// If there isn't a container, create one</em><a name="722" href="#722">722</a> <strong>if</strong> (data == <strong>null</strong>) {<a name="723" href="#723">723</a> <a href="../../../../org/archive/crawler/settings/ComplexType.html">ComplexType</a> parent = getParent();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -