当前位置: 首页>>代码示例>>Java>>正文


Java InputSource类代码示例

本文整理汇总了Java中org.xml.sax.InputSource的典型用法代码示例。如果您正苦于以下问题:Java InputSource类的具体用法?Java InputSource怎么用?Java InputSource使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


InputSource类属于org.xml.sax包,在下文中一共展示了InputSource类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testCDATA

import org.xml.sax.InputSource; //导入依赖的package包/类
public void testCDATA() throws Exception {
    Document doc = XMLUtil.createDocument("root", null, null, null);
    Element e = doc.createElement("sometag");
    doc.getDocumentElement().appendChild(e);

    String cdataContent = "!&<>*\n[[]]";
    CDATASection cdata = doc.createCDATASection(cdataContent);
    e.appendChild(cdata);
    
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XMLUtil.write(doc, baos, "UTF-8");

    String data = baos.toString("UTF-8").replace("\r\n", "\n");
    assertTrue("Can't find CDATA section", data.indexOf("<![CDATA[" + cdataContent + "]]>") != -1);
    
    // parse the data back to DOM
    Document doc2 = XMLUtil.parse(new InputSource(new ByteArrayInputStream(baos.toByteArray())), false, false, null, null);
    NodeList nl = doc2.getElementsByTagName("sometag");
    assertEquals("Wrong number of <sometag/> elements", 1, nl.getLength());
    nl = nl.item(0).getChildNodes();
    assertEquals("Wrong number of <sometag/> child elements", 1, nl.getLength());
    Node child = nl.item(0);
    assertTrue("Expecting CDATASection node", child instanceof CDATASection);
    assertEquals("Wrong CDATASection content", cdataContent, ((CDATASection) child).getNodeValue());
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:26,代码来源:XMLUtilTest.java

示例2: parse

import org.xml.sax.InputSource; //导入依赖的package包/类
/**
 * Parse the content given {@link org.xml.sax.InputSource}
 * as XML using the specified
 * {@link org.xml.sax.helpers.DefaultHandler}.
 *
 * @param is The InputSource containing the content to be parsed.
 * @param dh The SAX DefaultHandler to use.
 *
 * @throws IllegalArgumentException If the <code>InputSource</code> object
 *   is <code>null</code>.
 * @throws IOException If any IO errors occur.
 * @throws SAXException If any SAX errors occur during processing.
 *
 * @see org.xml.sax.DocumentHandler
 */
public void parse(InputSource is, DefaultHandler dh)
    throws SAXException, IOException {
    if (is == null) {
        throw new IllegalArgumentException("InputSource cannot be null");
    }

    XMLReader reader = this.getXMLReader();
    if (dh != null) {
        reader.setContentHandler(dh);
        reader.setEntityResolver(dh);
        reader.setErrorHandler(dh);
        reader.setDTDHandler(dh);
    }
    reader.parse(is);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:31,代码来源:SAXParser.java

示例3: resolveEntity

import org.xml.sax.InputSource; //导入依赖的package包/类
@Override
public InputSource resolveEntity(String pid, String sid)
throws SAXException {
    String publicURL = (String) dtdMap.get(pid);

    if (publicURL != null) {
        if (validate) {
            publicURL = getClass().getClassLoader().getResource(publicURL).toExternalForm();

            return new InputSource(publicURL);
        } else {
            return new InputSource(new ByteArrayInputStream(new byte[0]));
        }
    }

    return new InputSource(sid);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:18,代码来源:XMLFileSystem.java

示例4: resolveEntity

import org.xml.sax.InputSource; //导入依赖的package包/类
@Override
public InputStream resolveEntity(String publicId, String systemId, String baseUri, String namespace) {
    InputSource is = resolveEntity(publicId, systemId);

    if (is != null && !is.isEmpty()) {

        try {
            return new URL(is.getSystemId()).openStream();
        } catch (IOException ex) {
            //considered as no mapping.
        }

    }

    GroupEntry.ResolveType resolveType = ((CatalogImpl) catalog).getResolve();
    switch (resolveType) {
        case IGNORE:
            return null;
        case STRICT:
            CatalogMessages.reportError(CatalogMessages.ERR_NO_MATCH,
                    new Object[]{publicId, systemId});
    }

    //no action, allow the parser to continue
    return null;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:27,代码来源:CatalogResolverImpl.java

示例5: verifyXmlForUserAndPwd

import org.xml.sax.InputSource; //导入依赖的package包/类
/**
 * parse the XML content and verify the provisioningUsername and
 * provisioningPassword have been exported as expected
 * 
 * @param xmlString
 * @throws Exception
 */
private void verifyXmlForUserAndPwd(String xmlString) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(new InputSource(new StringReader(
            xmlString)));
    assertEquals(1, document.getChildNodes().getLength());
    Element root = document.getDocumentElement();
    assertEquals("tns:TechnicalServices", root.getNodeName());
    List<?> productNodes = XMLConverter.getNodeList(root.getChildNodes(),
            "tns:TechnicalService");
    assertEquals(1, productNodes.size());
    Element e = (Element) productNodes.get(0);
    assertEquals(PROVISIONING_USER_NAME,
            e.getAttribute("provisioningUsername"));
    assertEquals(PROVISIONING_PASSWORD,
            e.getAttribute("provisioningPassword"));
}
 
开发者ID:servicecatalog,项目名称:oscm,代码行数:25,代码来源:TechnicalProductXmlConverterTest.java

示例6: testcase13

import org.xml.sax.InputSource; //导入依赖的package包/类
/**
 * Unit test for TemplatesHandler setter/getter.
 *
 * @throws Exception If any errors occur.
 */
@Test
public void testcase13() throws Exception {
    String outputFile = USER_DIR + "saxtf013.out";
    String goldFile = GOLDEN_DIR + "saxtf013GF.out";
    try(FileInputStream fis = new FileInputStream(XML_FILE)) {
        // The transformer will use a SAX parser as it's reader.
        XMLReader reader = XMLReaderFactory.createXMLReader();

        SAXTransformerFactory saxTFactory
                = (SAXTransformerFactory) TransformerFactory.newInstance();
        TemplatesHandler thandler = saxTFactory.newTemplatesHandler();
        // I have put this as it was complaining about systemid
        thandler.setSystemId("file:///" + USER_DIR);

        reader.setContentHandler(thandler);
        reader.parse(XSLT_FILE);
        XMLFilter filter
                = saxTFactory.newXMLFilter(thandler.getTemplates());
        filter.setParent(reader);

        filter.setContentHandler(new MyContentHandler(outputFile));
        filter.parse(new InputSource(fis));
    }
    assertTrue(compareWithGold(goldFile, outputFile));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:31,代码来源:SAXTFactoryTest.java

示例7: sourceToInputSource

import org.xml.sax.InputSource; //导入依赖的package包/类
/**
 * Attempt to obtain a SAX InputSource object from a Source
 * object.
 *
 * @param source Must be a non-null Source reference.
 *
 * @return An InputSource, or null if Source can not be converted.
 */
public static InputSource sourceToInputSource(Source source) {

    if (source instanceof SAXSource) {
        return ((SAXSource) source).getInputSource();
    } else if (source instanceof StreamSource) {
        StreamSource ss      = (StreamSource) source;
        InputSource  isource = new InputSource(ss.getSystemId());

        isource.setByteStream(ss.getInputStream());
        isource.setCharacterStream(ss.getReader());
        isource.setPublicId(ss.getPublicId());

        return isource;
    } else {
        return null;
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:26,代码来源:SAXSource.java

示例8: parse

import org.xml.sax.InputSource; //导入依赖的package包/类
public void parse(InputStream xml, OutputStream finf, String workingDirectory) throws Exception {
    StAXDocumentSerializer documentSerializer = new StAXDocumentSerializer();
    documentSerializer.setOutputStream(finf);

    SAX2StAXWriter saxTostax = new SAX2StAXWriter(documentSerializer);

    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    saxParserFactory.setNamespaceAware(true);
    SAXParser saxParser = saxParserFactory.newSAXParser();

    XMLReader reader = saxParser.getXMLReader();
    reader.setProperty("http://xml.org/sax/properties/lexical-handler", saxTostax);
    reader.setContentHandler(saxTostax);

    if (workingDirectory != null) {
        reader.setEntityResolver(createRelativePathResolver(workingDirectory));
    }
    reader.parse(new InputSource(xml));

    xml.close();
    finf.close();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:23,代码来源:XML_SAX_StAX_FI.java

示例9: parse

import org.xml.sax.InputSource; //导入依赖的package包/类
public void parse(InputSource input) throws IOException, SAXException {
    try {
        InputStream s = input.getByteStream();
        if (s == null) {
            String systemId = input.getSystemId();
            if (systemId == null) {
                throw new SAXException(CommonResourceBundle.getInstance().getString("message.inputSource"));
            }
            parse(systemId);
        } else {
            parse(s);
        }
    } catch (FastInfosetException e) {
        logger.log(Level.FINE, "parsing error", e);
        throw new SAXException(e);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:18,代码来源:SAXDocumentParser.java

示例10: decode

import org.xml.sax.InputSource; //导入依赖的package包/类
/**
 * Decodeer een object.
 * @param configuration configuratie
 * @param clazz te decoderen object
 * @param reader reader om XML van de lezen
 * @param <T> type van het object
 * @return het gedecodeerde object
 * @throws ConfigurationException bij configuratie problemen (annoties op de klassen)
 * @throws DecodeException bij decodeer problemen
 */
public static <T> T decode(final Configuration configuration, final Class<T> clazz, final Reader reader) throws XmlException {
    final Context context = new Context();
    final Configuration theConfiguration = configuration == null ? DEFAULT_CONFIGURATION : configuration;
    ConfigurationHelper.setConfiguration(context, theConfiguration);

    final Root<T> item = theConfiguration.getModelFor(clazz);

    // Jaxp first
    final Document document;
    try {
        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        final DocumentBuilder builder = factory.newDocumentBuilder();
        document = builder.parse(new InputSource(reader));
    } catch (ParserConfigurationException | SAXException | IOException e) {
        throw new DecodeException(context.getElementStack(), e);
    }

    return item.decode(context, document);
}
 
开发者ID:MinBZK,项目名称:OperatieBRP,代码行数:31,代码来源:Xml.java

示例11: formatXml

import org.xml.sax.InputSource; //导入依赖的package包/类
/**
 * Format xml string.
 *
 * @param uglyXml the ugly xml
 * @return the string
 */
public static String formatXml(String uglyXml) {
	try {
		String xmlHeader= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();
		InputSource is = new InputSource(new StringReader(uglyXml));
		final Document document = db.parse(is);
		OutputFormat format = new OutputFormat(document);
		format.setLineWidth(65);
		format.setIndenting(true);
		format.setIndent(2);
		Writer out = new StringWriter();
		XMLSerializer serializer = new XMLSerializer(out, format);
		serializer.serialize(document);
		return out.toString().replace(xmlHeader,"");
	} catch (Exception e) {
		return "";
	}
}
 
开发者ID:thlws,项目名称:payment-wechat,代码行数:26,代码来源:ThlwsBeanUtil.java

示例12: retrieveValueByXpath

import org.xml.sax.InputSource; //导入依赖的package包/类
private String retrieveValueByXpath(String xml, DocumentBuilder builder,
    XPathExpression expr) {
    try {
        Document document = builder
            .parse(new InputSource(new StringReader(xml)));
        NodeList nodes = (NodeList) expr.evaluate(document,
            XPathConstants.NODESET);
        if (nodes == null || nodes.item(0) == null
            || nodes.item(0).getFirstChild() == null) {
            return null;
        }
        return nodes.item(0).getFirstChild().getNodeValue();
    } catch (SAXException | XPathExpressionException | IOException e) {
        e.printStackTrace();
    }
    return null;
}
 
开发者ID:servicecatalog,项目名称:oscm,代码行数:18,代码来源:TriggerServiceBean.java

示例13: verifyClusterInfoXML

import org.xml.sax.InputSource; //导入依赖的package包/类
public void verifyClusterInfoXML(String xml) throws JSONException, Exception {
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  DocumentBuilder db = dbf.newDocumentBuilder();
  InputSource is = new InputSource();
  is.setCharacterStream(new StringReader(xml));
  Document dom = db.parse(is);
  NodeList nodes = dom.getElementsByTagName("clusterInfo");
  assertEquals("incorrect number of elements", 1, nodes.getLength());

  for (int i = 0; i < nodes.getLength(); i++) {
    Element element = (Element) nodes.item(i);

    verifyClusterGeneric(WebServicesTestUtils.getXmlLong(element, "id"),
        WebServicesTestUtils.getXmlLong(element, "startedOn"),
        WebServicesTestUtils.getXmlString(element, "state"),
        WebServicesTestUtils.getXmlString(element, "haState"),
        WebServicesTestUtils.getXmlString(element, "hadoopVersionBuiltOn"),
        WebServicesTestUtils.getXmlString(element, "hadoopBuildVersion"),
        WebServicesTestUtils.getXmlString(element, "hadoopVersion"),
        WebServicesTestUtils.getXmlString(element,
            "resourceManagerVersionBuiltOn"),
        WebServicesTestUtils.getXmlString(element,
            "resourceManagerBuildVersion"),
        WebServicesTestUtils.getXmlString(element, "resourceManagerVersion"));
  }
}
 
开发者ID:naver,项目名称:hadoop,代码行数:27,代码来源:TestRMWebServices.java

示例14: validateAgainstAUDTDs

import org.xml.sax.InputSource; //导入依赖的package包/类
static void validateAgainstAUDTDs(InputSource input, final Path updaterJar, final Task task) throws IOException, SAXException {
    XMLUtil.parse(input, true, false, XMLUtil.rethrowHandler(), new EntityResolver() {
        ClassLoader loader = new AntClassLoader(task.getProject(), updaterJar);
        public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
            String remote = "http://www.netbeans.org/dtds/";
            if (systemId.startsWith(remote)) {
                String rsrc = "org/netbeans/updater/resources/" + systemId.substring(remote.length());
                URL u = loader.getResource(rsrc);
                if (u != null) {
                    return new InputSource(u.toString());
                } else {
                    task.log(rsrc + " not found in " + updaterJar, Project.MSG_WARN);
                }
            }
            return null;
        }
    });
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:19,代码来源:MakeNBM.java

示例15: close

import org.xml.sax.InputSource; //导入依赖的package包/类
public static void close(InputSource inputSource) {
    if (inputSource == null) {
        // Nothing to do
        return;
    }

    InputStream is = inputSource.getByteStream();
    if (is != null) {
        try {
            is.close();
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
        }
    }

}
 
开发者ID:sunmingshuai,项目名称:apache-tomcat-7.0.73-with-comment,代码行数:17,代码来源:InputSourceUtil.java


注:本文中的org.xml.sax.InputSource类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。