Search in sources :

Example 36 with XPathExpressionException

use of javax.xml.xpath.XPathExpressionException in project gerrit by GerritCodeReview.

the class HtmlDomUtil method compact.

private static void compact(Document doc) {
    try {
        String expr = "//text()[normalize-space(.) = '']";
        XPathFactory xp = XPathFactory.newInstance();
        XPathExpression e = xp.newXPath().compile(expr);
        NodeList empty = (NodeList) e.evaluate(doc, XPathConstants.NODESET);
        for (int i = 0; i < empty.getLength(); i++) {
            Node node = empty.item(i);
            node.getParentNode().removeChild(node);
        }
    } catch (XPathExpressionException e) {
    // Don't do the whitespace removal.
    }
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression) XPathFactory(javax.xml.xpath.XPathFactory) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node)

Example 37 with XPathExpressionException

use of javax.xml.xpath.XPathExpressionException in project lucene-solr by apache.

the class SolrXmlConfig method loadProperties.

private static Properties loadProperties(Config config) {
    try {
        Node node = ((NodeList) config.evaluate("solr", XPathConstants.NODESET)).item(0);
        XPath xpath = config.getXPath();
        NodeList props = (NodeList) xpath.evaluate("property", node, XPathConstants.NODESET);
        Properties properties = new Properties();
        for (int i = 0; i < props.getLength(); i++) {
            Node prop = props.item(i);
            properties.setProperty(DOMUtil.getAttr(prop, NAME), PropertiesUtil.substituteProperty(DOMUtil.getAttr(prop, "value"), null));
        }
        return properties;
    } catch (XPathExpressionException e) {
        log.warn("Error parsing solr.xml: " + e.getMessage());
        return null;
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) Properties(java.util.Properties)

Example 38 with XPathExpressionException

use of javax.xml.xpath.XPathExpressionException in project lucene-solr by apache.

the class QueryElevationComponent method loadElevationMap.

//load up the elevation map
private Map<String, ElevationObj> loadElevationMap(Config cfg) throws IOException {
    XPath xpath = XPathFactory.newInstance().newXPath();
    Map<String, ElevationObj> map = new HashMap<>();
    NodeList nodes = (NodeList) cfg.evaluate("elevate/query", XPathConstants.NODESET);
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        String qstr = DOMUtil.getAttr(node, "text", "missing query 'text'");
        NodeList children = null;
        try {
            children = (NodeList) xpath.evaluate("doc", node, XPathConstants.NODESET);
        } catch (XPathExpressionException e) {
            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "query requires '<doc .../>' child");
        }
        ArrayList<String> include = new ArrayList<>();
        ArrayList<String> exclude = new ArrayList<>();
        for (int j = 0; j < children.getLength(); j++) {
            Node child = children.item(j);
            String id = DOMUtil.getAttr(child, ID, "missing 'id'");
            String e = DOMUtil.getAttr(child, EXCLUDE, null);
            if (e != null) {
                if (Boolean.valueOf(e)) {
                    exclude.add(id);
                    continue;
                }
            }
            include.add(id);
        }
        ElevationObj elev = new ElevationObj(qstr, include, exclude);
        if (map.containsKey(elev.analyzed)) {
            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Boosting query defined twice for query: '" + elev.text + "' (" + elev.analyzed + "')");
        }
        map.put(elev.analyzed, elev);
    }
    return map;
}
Also used : XPath(javax.xml.xpath.XPath) HashMap(java.util.HashMap) IntIntHashMap(com.carrotsearch.hppc.IntIntHashMap) WeakHashMap(java.util.WeakHashMap) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) SolrException(org.apache.solr.common.SolrException)

Example 39 with XPathExpressionException

use of javax.xml.xpath.XPathExpressionException in project adempiere by adempiere.

the class ImportHelper method importXMLDocument.

/**
	 * Import XML Document
	 * @param result
	 * @param documentToBeImported
	 * @param trxName
	 * @throws Exception
	 * @throws SQLException
	 * @throws XPathExpressionException
	 */
public void importXMLDocument(StringBuffer result, Document documentToBeImported, String trxName) throws Exception, SQLException, XPathExpressionException {
    Element rootElement = documentToBeImported.getDocumentElement();
    // Find which Export format to Load...
    String AD_Client_Value = null;
    AD_Client_Value = rootElement.getAttribute("AD_Client_Value");
    log.info("AD_Client_Value = " + AD_Client_Value);
    if (AD_Client_Value == null || Util.isEmpty(AD_Client_Value)) {
        throw new Exception(Msg.getMsg(ctx, "XMLClientValueMandatory"));
    }
    String version = null;
    version = rootElement.getAttribute("Version");
    log.info("Version = " + version);
    if (version == null || Util.isEmpty(version)) {
        throw new Exception(Msg.getMsg(ctx, "XMLVersionAttributeMandatory"));
    }
    ///Getting Attributes.
    int ReplicationMode = new Integer(rootElement.getAttribute("ReplicationMode"));
    String ReplicationType = rootElement.getAttribute("ReplicationType");
    int ReplicationEvent = new Integer(rootElement.getAttribute("ReplicationEvent"));
    MClient client = null;
    client = getAD_ClientByValue(ctx, AD_Client_Value, trxName);
    if (client == null) {
        throw new Exception(Msg.getMsg(ctx, "XMLClientNotFound"));
    }
    log.info("XML ROOT AD_Client = " + client.toString());
    String EXP_Format_Value = null;
    EXP_Format_Value = rootElement.getNodeName();
    log.info("EXP_Format_Value = " + EXP_Format_Value);
    MEXPFormat expFormat = null;
    expFormat = MEXPFormat.getFormatByValueAD_Client_IDAndVersion(ctx, EXP_Format_Value, client.getAD_Client_ID(), version, trxName);
    if (expFormat == null || expFormat.getEXP_Format_ID() == 0) {
        // Fall back to SYSTEM Client.
        // Try to search Export format defined for SYSTEM Client!!!
        MClient systemClient = null;
        systemClient = MClient.get(ctx, 0);
        if (systemClient == null) {
            throw new Exception(Msg.getMsg(ctx, "XMLClientNotFound"));
        }
        log.info("SYSTEM Client = " + systemClient.toString());
        expFormat = MEXPFormat.getFormatByValueAD_Client_IDAndVersion(ctx, EXP_Format_Value, systemClient.getAD_Client_ID(), version, trxName);
    }
    if (expFormat == null || expFormat.getEXP_Format_ID() == 0) {
        throw new Exception(Msg.getMsg(ctx, "EXPFormatNotFound"));
    }
    log.info("expFormat = " + expFormat.toString());
    isChanged = false;
    PO po = importElement(ctx, result, rootElement, expFormat, ReplicationType, trxName);
    if (ModelValidator.TYPE_BEFORE_DELETE == ReplicationEvent || ModelValidator.TYPE_BEFORE_DELETE_REPLICATION == ReplicationEvent || ModelValidator.TYPE_DELETE == ReplicationEvent)
        ;
    else if (!po.is_Changed() && !isChanged) {
        log.info("Object not changed = " + po.toString());
        return;
    }
    if (po != null) {
        Env.setContext(po.getCtx(), "#AD_Client_ID", po.getAD_Client_ID());
        if (MReplicationStrategy.REPLICATION_TABLE == ReplicationMode) {
            // Here must invoke other method else we get cycle...
            if (ModelValidator.TYPE_BEFORE_DELETE == ReplicationEvent || ModelValidator.TYPE_BEFORE_DELETE_REPLICATION == ReplicationEvent || ModelValidator.TYPE_DELETE == ReplicationEvent) {
                po.deleteEx(true);
            } else {
                if (X_AD_ReplicationTable.REPLICATIONTYPE_Broadcast.equals(ReplicationType)) {
                    MReplicationStrategy rplStrategy = new MReplicationStrategy(client.getCtx(), client.getAD_ReplicationStrategy_ID(), po.get_TrxName());
                    ExportHelper expHelper = new ExportHelper(client, rplStrategy);
                    expHelper.exportRecord(po, MReplicationStrategy.REPLICATION_TABLE, X_AD_ReplicationTable.REPLICATIONTYPE_Merge, ModelValidator.TYPE_AFTER_CHANGE);
                    po.saveReplica(true);
                } else if (X_AD_ReplicationTable.REPLICATIONTYPE_Merge.equals(ReplicationType) || X_AD_ReplicationTable.REPLICATIONTYPE_Reference.equals(ReplicationType)) {
                    po.saveReplica(true);
                } else /*else if (X_AD_ReplicationTable.REPLICATIONTYPE_Reference.equals(ReplicationType))
					{
						//Do nothing??	
					}*/
                if (X_AD_ReplicationTable.REPLICATIONTYPE_Local.equals(ReplicationType)) {
                //Do nothing??	
                } else {
                    // Replication Type is not one of the possible values...ERROR
                    throw new Exception(Msg.getMsg(ctx, "EXPReplicationTypeNonValidType"));
                }
            }
        } else if (MReplicationStrategy.REPLICATION_DOCUMENT == ReplicationMode && X_AD_ReplicationDocument.REPLICATIONTYPE_Merge.equals(ReplicationType) && po instanceof DocAction) {
            DocAction document = (DocAction) po;
            String action = document.getDocAction();
            String status = document.getDocStatus();
            log.info("Document:" + document.toString() + " DocStauts:" + status + " DocAction:" + action);
            if (ModelValidator.TIMING_AFTER_REVERSECORRECT == ReplicationEvent) {
                if (status.equals(DocAction.STATUS_Reversed) && action.equals(DocAction.ACTION_None)) {
                    po.saveEx();
                    return;
                }
            }
            if ((action.equals(DocAction.ACTION_Complete) && status.equals(DocAction.STATUS_InProgress)) || (action.equals(DocAction.ACTION_Close) && status.equals(DocAction.STATUS_Completed))) {
                if (!document.processIt(action)) {
                    log.info("PO.toString() = can not " + po.get_Value("DocAction"));
                }
                po.saveEx();
            } else {
                po.saveEx();
                return;
            }
        }
    }
    result.append("Save Successful ;");
}
Also used : MEXPFormat(org.compiere.model.MEXPFormat) DocAction(org.compiere.process.DocAction) Element(org.w3c.dom.Element) ExportHelper(org.adempiere.process.rpl.exp.ExportHelper) MReplicationStrategy(org.compiere.model.MReplicationStrategy) XPathExpressionException(javax.xml.xpath.XPathExpressionException) SQLException(java.sql.SQLException) ParseException(java.text.ParseException) AdempiereException(org.adempiere.exceptions.AdempiereException) MClient(org.compiere.model.MClient) PO(org.compiere.model.PO)

Example 40 with XPathExpressionException

use of javax.xml.xpath.XPathExpressionException in project zm-mailbox by Zimbra.

the class TestCalDav method testAppleCaldavProxyFunctions.

/**
     * http://svn.calendarserver.org/repository/calendarserver/CalendarServer/trunk/doc/Extensions/caldav-proxy.txt
     * This is an Apple standard implemented by Apple Mac OSX at least up to Yosemite and offers a fairly simple
     * sharing model for calendars.  The model is simpler than Zimbra's native model and there are mismatches,
     * for instance Zimbra requires the proposed delegate to accept shares.
     */
@Test
public void testAppleCaldavProxyFunctions() throws ServiceException, IOException {
    Account sharer = users[3].create();
    Account sharee1 = users[1].create();
    Account sharee2 = users[2].create();
    ZMailbox mboxSharer = TestUtil.getZMailbox(sharer.getName());
    ZMailbox mboxSharee1 = TestUtil.getZMailbox(sharee1.getName());
    ZMailbox mboxSharee2 = TestUtil.getZMailbox(sharee2.getName());
    setZimbraPrefAppleIcalDelegationEnabled(mboxSharer, true);
    setZimbraPrefAppleIcalDelegationEnabled(mboxSharee1, true);
    setZimbraPrefAppleIcalDelegationEnabled(mboxSharee2, true);
    // Test PROPPATCH to "calendar-proxy-read" URL
    setGroupMemberSet(TestCalDav.getCalendarProxyReadUrl(sharer), sharer, sharee2);
    // Test PROPPATCH to "calendar-proxy-write" URL
    setGroupMemberSet(TestCalDav.getCalendarProxyWriteUrl(sharer), sharer, sharee1);
    // verify that adding new members to groups triggered notification messages
    List<ZMessage> msgs = TestUtil.waitForMessages(mboxSharee1, "in:inbox subject:\"Share Created: Calendar shared by \"", 1, 10000);
    assertNotNull(String.format("Notification msgs for %s", sharee1.getName()), msgs);
    assertEquals(String.format("num msgs for %s", sharee1.getName()), 1, msgs.size());
    msgs = TestUtil.waitForMessages(mboxSharee2, "in:inbox subject:\"Share Created: Calendar shared by \"", 1, 10000);
    assertNotNull(String.format("Notification msgs for %s", sharee2.getName()), msgs);
    assertEquals(String.format("num msgs for %s", sharee2.getName()), 1, msgs.size());
    // Simulate acceptance of the shares (would normally need to be done in ZWC
    createCalendarMountPoint(mboxSharee1, sharer);
    createCalendarMountPoint(mboxSharee2, sharer);
    Document doc = delegateForExpandProperty(sharee1);
    XPath xpath = XPathFactory.newInstance().newXPath();
    xpath.setNamespaceContext(TestCalDav.NamespaceContextForXPath.forCalDAV());
    XPathExpression xPathExpr;
    try {
        String xpathS = "/D:multistatus/D:response/D:href/text()";
        xPathExpr = xpath.compile(xpathS);
        NodeList result = (NodeList) xPathExpr.evaluate(doc, XPathConstants.NODESET);
        assertEquals(String.format("num XPath nodes for %s for %s", xpathS, sharee1.getName()), 1, result.getLength());
        String text = (String) xPathExpr.evaluate(doc, XPathConstants.STRING);
        assertEquals("HREF for account owner", UrlNamespace.getPrincipalUrl(sharee1).replaceAll("@", "%40"), text);
        xpathS = "/D:multistatus/D:response/D:propstat/D:prop/CS:calendar-proxy-write-for/D:response/D:href/text()";
        xPathExpr = xpath.compile(xpathS);
        result = (NodeList) xPathExpr.evaluate(doc, XPathConstants.NODESET);
        assertEquals(String.format("num XPath nodes for %s for %s", xpathS, sharee1.getName()), 1, result.getLength());
        text = (String) xPathExpr.evaluate(doc, XPathConstants.STRING);
        assertEquals("HREF for sharer", UrlNamespace.getPrincipalUrl(sharer).replaceAll("@", "%40"), text);
    } catch (XPathExpressionException e1) {
        ZimbraLog.test.debug("xpath problem", e1);
    }
    // Check that proxy write has sharee1 in it
    doc = groupMemberSetExpandProperty(sharer, sharee1, true);
    // Check that proxy read has sharee2 in it
    doc = groupMemberSetExpandProperty(sharer, sharee2, false);
    String davBaseName = "notAllowed@There";
    String url = String.format("%s%s", getFolderUrl(sharee1, "Shared Calendar").replaceAll(" ", "%20").replaceAll("@", "%40"), davBaseName);
    HttpMethodExecutor exe = doIcalPut(url, sharee1, simpleEvent(sharer), HttpStatus.SC_MOVED_TEMPORARILY);
    String location = null;
    for (Header hdr : exe.respHeaders) {
        if ("Location".equals(hdr.getName())) {
            location = hdr.getValue();
        }
    }
    assertNotNull("Location Header not returned when creating", location);
    url = String.format("%s%s", getFolderUrl(sharee1, "Shared Calendar").replaceAll(" ", "%20").replaceAll("@", "%40"), location.substring(location.lastIndexOf('/') + 1));
    doIcalPut(url, sharee1, simpleEvent(sharer), HttpStatus.SC_CREATED);
}
Also used : ZMessage(com.zimbra.client.ZMessage) XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) Account(com.zimbra.cs.account.Account) ZMailbox(com.zimbra.client.ZMailbox) Header(org.apache.commons.httpclient.Header) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) Test(org.junit.Test)

Aggregations

XPathExpressionException (javax.xml.xpath.XPathExpressionException)157 XPath (javax.xml.xpath.XPath)79 NodeList (org.w3c.dom.NodeList)74 Document (org.w3c.dom.Document)51 Node (org.w3c.dom.Node)50 IOException (java.io.IOException)47 XPathExpression (javax.xml.xpath.XPathExpression)42 SAXException (org.xml.sax.SAXException)32 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)29 XPathFactory (javax.xml.xpath.XPathFactory)29 ArrayList (java.util.ArrayList)23 Element (org.w3c.dom.Element)22 InputSource (org.xml.sax.InputSource)20 HashMap (java.util.HashMap)19 Test (org.junit.Test)17 DocumentBuilder (javax.xml.parsers.DocumentBuilder)14 Response (com.jayway.restassured.response.Response)12 ValidatableResponse (com.jayway.restassured.response.ValidatableResponse)12 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)12 AbstractIntegrationTest (org.codice.ddf.itests.common.AbstractIntegrationTest)12