⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 crawlsettingssaxsource.java

📁 最强的爬虫工程
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        while (it.hasNext()) {            Object limit = it.next();            if (limit instanceof TimespanCriteria) {                AttributesImpl timeSpan = new AttributesImpl();                timeSpan.addAttribute(nsu,                        XMLSettingsHandler.XML_ATTRIBUTE_FROM,                        XMLSettingsHandler.XML_ATTRIBUTE_FROM, nsu,                        ((TimespanCriteria) limit).getFrom());                timeSpan.addAttribute(nsu, XMLSettingsHandler.XML_ATTRIBUTE_TO,                        XMLSettingsHandler.XML_ATTRIBUTE_TO, nsu,                        ((TimespanCriteria) limit).getTo());                writeSimpleElement(XMLSettingsHandler.XML_ELEMENT_TIMESPAN, "",                        timeSpan, indent + 2 * indentAmount);            } else if (limit instanceof PortnumberCriteria) {                writeSimpleElement(XMLSettingsHandler.XML_ELEMENT_PORTNUMBER,                        ((PortnumberCriteria) limit).getPortNumber(), nullAtts,                        indent + 2 * indentAmount);            } else if (limit instanceof RegularExpressionCriteria) {                writeSimpleElement(XMLSettingsHandler.XML_ELEMENT_URIMATCHES,                        ((RegularExpressionCriteria) limit).getRegexp(), nullAtts,                        indent + 2 * indentAmount);            }        }        handler.ignorableWhitespace(indentArray, 0, indent);        handler.endElement(nsu, XMLSettingsHandler.XML_ELEMENT_LIMITS,                XMLSettingsHandler.XML_ELEMENT_LIMITS);    }    private void parseMetaData(int indent) throws SAXException {        // Write meta information        Attributes nullAtts = new AttributesImpl();        handler.ignorableWhitespace(indentArray, 0, indent);        handler.startElement(nsu, XMLSettingsHandler.XML_ELEMENT_META,                XMLSettingsHandler.XML_ELEMENT_META, nullAtts);        // Write settings name        writeSimpleElement(XMLSettingsHandler.XML_ELEMENT_NAME, settings                .getName(), null, indent + indentAmount);        // Write settings description        writeSimpleElement(XMLSettingsHandler.XML_ELEMENT_DESCRIPTION, settings                .getDescription(), null, indent + indentAmount);        // Write settings operator        writeSimpleElement(XMLSettingsHandler.XML_ELEMENT_OPERATOR, settings                .getOperator(), null, indent + indentAmount);        // Write settings description        writeSimpleElement(XMLSettingsHandler.XML_ELEMENT_ORGANIZATION, settings                .getOrganization(), null, indent + indentAmount);        // Write settings description        writeSimpleElement(XMLSettingsHandler.XML_ELEMENT_AUDIENCE, settings                .getAudience(), null, indent + indentAmount);        // Write file date        String dateStamp = ArchiveUtils.get14DigitDate();        writeSimpleElement(XMLSettingsHandler.XML_ELEMENT_DATE, dateStamp,                null, indent + indentAmount);        try {            settings.setLastSavedTime(ArchiveUtils.parse14DigitDate(dateStamp));        } catch (ParseException e) {            // Should never happen since we just created it. If this exception            // is thrown, then there is a bug in ArchiveUtils.            e.printStackTrace();        }        handler.ignorableWhitespace(indentArray, 0, indent);        handler.endElement(nsu, XMLSettingsHandler.XML_ELEMENT_META,                XMLSettingsHandler.XML_ELEMENT_META);    }    /**     * Create SAX events from a {@link ComplexType}.     *     * @param complexType the object to creat SAX events from.     * @param indent the indentation amount for prettyprinting XML.     * @throws SAXException is thrown if an error occurs.     */    private void parseComplexType(ComplexType complexType, int indent)            throws SAXException {        if (complexType.isTransient()) {            return;        }        MBeanInfo mbeanInfo = complexType.getMBeanInfo(settings);        String objectElement = resolveElementName(complexType);        AttributesImpl atts = new AttributesImpl();        atts.addAttribute(nsu, XMLSettingsHandler.XML_ATTRIBUTE_NAME,                XMLSettingsHandler.XML_ATTRIBUTE_NAME, nsu, complexType                        .getName());        if (objectElement == XMLSettingsHandler.XML_ELEMENT_NEW_OBJECT) {            // Only 'newObject' elements have a class attribute            atts.addAttribute(nsu, XMLSettingsHandler.XML_ATTRIBUTE_CLASS,                    XMLSettingsHandler.XML_ATTRIBUTE_CLASS, nsu, mbeanInfo                            .getClassName());        }        if (complexType.getParent() == null) {            atts = new AttributesImpl();        }        handler.ignorableWhitespace(indentArray, 0, indent);        handler.startElement(nsu, objectElement, objectElement, atts);        for (Iterator it = complexType.getAttributeInfoIterator(settings); it                .hasNext();) {            ModuleAttributeInfo attribute = (ModuleAttributeInfo) it.next();            if (!attribute.isTransient()) {                parseAttribute(complexType, attribute, indent);            }        }        handler.ignorableWhitespace(indentArray, 0, indent);        handler.endElement(nsu, objectElement, objectElement);    }    private void parseAttribute(ComplexType complexType,            ModuleAttributeInfo attribute, int indent) throws SAXException {        Object value;        try {            value = complexType                    .getLocalAttribute(settings, attribute.getName());        } catch (AttributeNotFoundException e) {            throw new SAXException(e);        }        if (orderFile || value != null) {            // Write only overridden values unless this is the order file            if (attribute.isComplexType()) {                // Call method recursively for complex types                parseComplexType((ComplexType) value, indent + indentAmount);            } else {                // Write element                String elementName = SettingsHandler.getTypeName(attribute                        .getType());                AttributesImpl atts = new AttributesImpl();                atts.addAttribute(nsu, XMLSettingsHandler.XML_ATTRIBUTE_NAME,                        XMLSettingsHandler.XML_ATTRIBUTE_NAME, nsu, attribute                                .getName());                if (value == null) {                    try {                        value = complexType.getAttribute(attribute.getName());                    } catch (Exception e) {                        throw new SAXException(                                "Internal error in settings subsystem", e);                    }                }                if (value != null) {                    handler.ignorableWhitespace(indentArray, 0, indent                            + indentAmount);                    handler.startElement(nsu, elementName, elementName, atts);                    if (value instanceof ListType) {                        parseListData(value, indent + indentAmount);                        handler.ignorableWhitespace(indentArray, 0, indent                                + indentAmount);                    } else {                        char valueArray[] = value.toString().toCharArray();                        handler.characters(valueArray, 0, valueArray.length);                    }                    handler.endElement(nsu, elementName, elementName);                }            }        }    }    /** Create SAX events for the content of a {@link ListType}.     *     * @param value the ListType whose content we create SAX events for.     * @param indent the indentation amount for prettyprinting XML.     * @throws SAXException is thrown if an error occurs.     */    private void parseListData(Object value, int indent) throws SAXException {        ListType list = (ListType) value;        Iterator it = list.iterator();        while (it.hasNext()) {            Object element = it.next();            String elementName =                SettingsHandler.getTypeName(element.getClass().getName());            writeSimpleElement(                elementName,                element.toString(),                null,                indent + indentAmount);        }    }    /** Resolve the XML element name of a {@link ComplexType}.     *     * @param complexType the object to investigate.     * @return the name of the XML element.     */    private String resolveElementName(ComplexType complexType) {        String elementName;        if (complexType instanceof ModuleType) {            if (complexType.getParent() == null) {                // Top level controller element                elementName = XMLSettingsHandler.XML_ELEMENT_CONTROLLER;            } else if (                !orderFile                    && complexType.globalSettings().getModule(                        complexType.getName())                        != null) {                // This is not the order file and we are referencing an object                elementName = XMLSettingsHandler.XML_ELEMENT_OBJECT;            } else {                // The object is not referenced before                elementName = XMLSettingsHandler.XML_ELEMENT_NEW_OBJECT;            }        } else {            // It's a map            elementName =                SettingsHandler.getTypeName(complexType.getClass().getName());        }        return elementName;    }    /** Create SAX events for a simple element.     *     * Creates all the SAX events needed for prettyprinting an XML element     * with a simple value and possible attributes.     *     * @param elementName the name of the XML element.     * @param value the value to pu inside the XML element.     * @param atts the attributes for the XML element.     * @param indent the indentation amount for prettyprinting XML.     * @throws SAXException is thrown if an error occurs.     */    private void writeSimpleElement(        String elementName,        String value,        Attributes atts,        int indent)        throws SAXException {        if (atts == null) {            atts = new AttributesImpl();        }        // make sure that the value is never null        value = value == null ? "" : value;        handler.ignorableWhitespace(indentArray, 0, indent);        handler.startElement(nsu, elementName, elementName, atts);        handler.characters(value.toCharArray(), 0, value.length());        handler.endElement(nsu, elementName, elementName);    }    /* (non-Javadoc)     * @see org.xml.sax.XMLReader#parse(java.lang.String)     */    public void parse(String systemId) throws IOException, SAXException {        // Do nothing. Just for conformance to the XMLReader API.    }    /* (non-Javadoc)     * @see javax.xml.transform.sax.SAXSource#getXMLReader()     */    public XMLReader getXMLReader() {        return this;    }    /* (non-Javadoc)     * @see javax.xml.transform.sax.SAXSource#getInputSource()     */    public InputSource getInputSource() {        return new InputSource();    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -