Search in sources :

Example 96 with Document

use of org.w3c.dom.Document in project weixin-java-tools by chanjarster.

the class WxCryptUtilTest method testValidateSignatureError.

public void testValidateSignatureError() throws ParserConfigurationException, SAXException, IOException {
    try {
        WxCryptUtil pc = new WxCryptUtil(token, encodingAesKey, appId);
        String afterEncrpt = pc.encrypt(replyMsg);
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        StringReader sr = new StringReader(afterEncrpt);
        InputSource is = new InputSource(sr);
        Document document = db.parse(is);
        Element root = document.getDocumentElement();
        NodeList nodelist1 = root.getElementsByTagName("Encrypt");
        String encrypt = nodelist1.item(0).getTextContent();
        String fromXML = String.format(xmlFormat, encrypt);
        // 这里签名错误
        pc.decrypt("12345", timestamp, nonce, fromXML);
    } catch (RuntimeException e) {
        assertEquals(e.getMessage(), "加密消息签名校验失败");
        return;
    }
    fail("错误流程不抛出异常???");
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) StringReader(java.io.StringReader) Document(org.w3c.dom.Document)

Example 97 with Document

use of org.w3c.dom.Document in project weixin-java-tools by chanjarster.

the class WxCryptUtil method extractEncryptPart.

static String extractEncryptPart(String xml) {
    try {
        DocumentBuilder db = builderLocal.get();
        Document document = db.parse(new InputSource(new StringReader(xml)));
        Element root = document.getDocumentElement();
        return root.getElementsByTagName("Encrypt").item(0).getTextContent();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) StringReader(java.io.StringReader) Document(org.w3c.dom.Document) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 98 with Document

use of org.w3c.dom.Document in project cogtool by cogtool.

the class BalsamiqButtonAPIConverter method parseBalsamiqFile.

/** Helper function used by importDesign.
	 * This method is used to parse the tags of each .bmml file in the 
	 * directory. The file represents a {@link File} in the directory.
     * 
     * @param  inputFile the specified directory or file
     * @see              the new design in the project window.
     */
public void parseBalsamiqFile(File inputFile) throws IOException {
    String fileName = inputFile.getName();
    int lastIndex = fileName.lastIndexOf(".");
    fileName = (lastIndex == -1) ? fileName : fileName.substring(0, lastIndex);
    /* Check to make sure that this frame has not been created yet. Cycles 
		 * by transitions may exist in the design and these files do not need 
		 * to be parsed more than once. */
    if (!visitedFramesNames.contains(fileName)) {
        visitedFramesNames.add(fileName);
        // Create a Xerces DOM Parser. A DOM Parser can parse an XML file
        // and create a tree representation of it. This same parser method
        // is used in ImportCogTool.java to import from CogTool XML.
        DOMParser parser = new DOMParser();
        InputStream fis = new FileInputStream(inputFile);
        Reader input = new InputStreamReader(fis, "UTF-8");
        // Parse the Document and traverse the DOM
        try {
            parser.parse(new InputSource(input));
        } catch (SAXException e) {
            throw new RcvrImportException("Not a valid XML file to parse.");
        }
        //Traverse the xml tags in the tree beginning at the root, the document
        Document document = parser.getDocument();
        parseFrame(document, fileName);
    }
}
Also used : InputSource(org.xml.sax.InputSource) InputStreamReader(java.io.InputStreamReader) RcvrImportException(edu.cmu.cs.hcii.cogtool.util.RcvrImportException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) DOMParser(com.sun.org.apache.xerces.internal.parsers.DOMParser) Document(org.w3c.dom.Document) FileInputStream(java.io.FileInputStream) SAXException(org.xml.sax.SAXException)

Example 99 with Document

use of org.w3c.dom.Document in project cw-omnibus by commonsguy.

the class WeatherFragment method buildForecasts.

private ArrayList<Forecast> buildForecasts(String raw) throws Exception {
    ArrayList<Forecast> forecasts = new ArrayList<Forecast>();
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = builder.parse(new InputSource(new StringReader(raw)));
    NodeList times = doc.getElementsByTagName("start-valid-time");
    for (int i = 0; i < times.getLength(); i++) {
        Element time = (Element) times.item(i);
        Forecast forecast = new Forecast();
        forecasts.add(forecast);
        forecast.setTime(time.getFirstChild().getNodeValue());
    }
    NodeList temps = doc.getElementsByTagName("value");
    for (int i = 0; i < temps.getLength(); i++) {
        Element temp = (Element) temps.item(i);
        Forecast forecast = forecasts.get(i);
        forecast.setTemp(Integer.valueOf(temp.getFirstChild().getNodeValue()));
    }
    NodeList icons = doc.getElementsByTagName("icon-link");
    for (int i = 0; i < icons.getLength(); i++) {
        Element icon = (Element) icons.item(i);
        Forecast forecast = forecasts.get(i);
        forecast.setIcon(icon.getFirstChild().getNodeValue());
    }
    return (forecasts);
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilder(javax.xml.parsers.DocumentBuilder) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) Document(org.w3c.dom.Document)

Example 100 with Document

use of org.w3c.dom.Document in project cw-android by commonsguy.

the class StaticFileDemo method onCreate.

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    selection = (TextView) findViewById(R.id.selection);
    try {
        InputStream in = getResources().openRawResource(R.raw.words);
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = builder.parse(in, null);
        NodeList words = doc.getElementsByTagName("word");
        for (int i = 0; i < words.getLength(); i++) {
            items.add(((Element) words.item(i)).getAttribute("value"));
        }
        in.close();
    } catch (Throwable t) {
        Toast.makeText(this, "Exception: " + t.toString(), Toast.LENGTH_LONG).show();
    }
    setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));
}
Also used : DocumentBuilder(javax.xml.parsers.DocumentBuilder) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document)

Aggregations

Document (org.w3c.dom.Document)2446 Element (org.w3c.dom.Element)990 DocumentBuilder (javax.xml.parsers.DocumentBuilder)719 NodeList (org.w3c.dom.NodeList)648 Node (org.w3c.dom.Node)545 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)528 IOException (java.io.IOException)425 SAXException (org.xml.sax.SAXException)301 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)299 InputSource (org.xml.sax.InputSource)250 Test (org.junit.Test)233 File (java.io.File)190 StringReader (java.io.StringReader)182 ArrayList (java.util.ArrayList)174 InputStream (java.io.InputStream)167 DOMSource (javax.xml.transform.dom.DOMSource)161 ByteArrayInputStream (java.io.ByteArrayInputStream)154 Attr (org.w3c.dom.Attr)134 DOMException (org.w3c.dom.DOMException)129 XPath (javax.xml.xpath.XPath)107