@Test public void testGetBoolean() throws XQException { SaxonXQDataSource xqds = new SaxonXQDataSource(); XQConnection xqc = xqds.getConnection(); XQExpression xqe = xqc.createExpression(); xqe.bindObject(new QName("v"), Boolean.valueOf(true), null); XQSequence xqs = xqe.executeQuery("declare variable $v external; $v instance of xs:boolean"); xqs.next(); assertTrue("expected true but got false", xqs.getBoolean()); xqe.bindObject(new QName("v"), new Byte((byte) 1), null); xqs = xqe.executeQuery("declare variable $v external; $v instance of xs:byte"); xqs.next(); assertTrue("expected true (byte) but got false", xqs.getBoolean()); xqs.close(); }
@Test public void testBinding() throws XQException { SaxonXQDataSource xqds = new SaxonXQDataSource(); XQConnection xqc = xqds.getConnection(); XQPreparedExpression xqpe = xqc.prepareExpression("declare variable $v external; $v"); xqpe.bindString(new QName("v"), "Hello world!", xqc.createAtomicType(XQItemType.XQBASETYPE_STRING)); XQSequence xqs = xqpe.executeQuery(); assertTrue(xqs.next()); assertEquals("Hello world!", xqs.getAtomicValue()); xqpe.close(); xqpe = xqc.prepareExpression("declare variable $v external; $v"); xqpe.bindString(new QName("v"), "Hello", xqc.createAtomicType(XQItemType.XQBASETYPE_NCNAME)); xqs = xqpe.executeQuery(); xqs.next(); assertEquals("A-XQDC-1.7: Successful bindXXX.", XQItemType.XQITEMKIND_ATOMIC, xqs.getItemType().getItemKind()); assertEquals("A-XQDC-1.7: Successful bindXXX.", XQItemType.XQBASETYPE_NCNAME, xqs.getItemType().getBaseType()); assertEquals("A-XQDC-1.7: Successful bindXXX.", "Hello", xqs.getObject()); xqpe.close(); }
private XQConnection initConnection(String username) throws XQException { BagriXQConnection connect = createConnection(username); Object xqp = makeInstance(XQ_PROCESSOR); if (xqp != null) { if (xqp instanceof XQProcessor) { connect.setProcessor((XQProcessor) xqp); ((XQProcessor) xqp).setXQDataFactory(connect); Object xdm = initRepository(connect); if (xdm != null) { if (xdm instanceof SchemaRepository) { ((XQProcessor) xqp).setRepository((SchemaRepository) xdm); } else { throw new XQException("Specified Repository class does not implement XDMRepository interface: " + properties.getProperty(XDM_REPOSITORY)); } } } else { throw new XQException("Specified XQ Processor class does not implement XQProcessor interface: " + properties.getProperty(XQ_PROCESSOR)); } } return connect; }
@Override public XQConnection getConnection() throws XQException { // get pooled connection from pool // if no spare connections - grow pool..? PooledXQConnection xqConn; synchronized (pool) { if (pool.size() > 0) { xqConn = pool.pop(); } else { xqConn = poolSource.getPooledConnection(); } } xqConn.addConnectionEventListener(this); XQConnection conn = xqConn.getConnection(); logger.debug("getConnection; pool size: {}; returning: {}", pool.size(), conn); return conn; }
@Test public void testStoreSecurity() throws XQException { String dName = "../etc/samples/tpox/"; String xml; try { xml = readTextFile(dName + "security1500.xml"); } catch (IOException ex) { throw new XQException(ex.getMessage()); } String query = "declare namespace bgdb=\"http://bagridb.com/bdb\";\n" + "declare variable $url external;\n" + "declare variable $xml external;\n" + "let $id := bgdb:store-document($url, $xml)\n" + "return $id\n"; XQConnection xqc = xqds.getConnection(); XQPreparedExpression xqpe = xqc.prepareExpression(query); xqpe.bindString(new QName("url"), "security1500.xml", xqc.createAtomicType(XQItemType.XQBASETYPE_ANYURI)); xqpe.bindString(new QName("xml"), xml, xqc.createAtomicType(XQItemType.XQBASETYPE_STRING)); XQSequence xqs = xqpe.executeQuery(); assertTrue(xqs.next()); xqs.close(); xqpe.close(); }
private static String runQuery(String query) throws XQException { System.out.println("runQuery: \n\t" + query); XQDataSource xqs = new ExistXQDataSource(); String returnString = ""; xqs.setProperty("serverName", "localhost"); xqs.setProperty("port", "8080"); XQConnection conn = xqs.getConnection(); XQPreparedExpression xqpe = conn.prepareExpression(query); XQResultSequence rs = xqpe.executeQuery(); while (rs.next()) { returnString += rs.getItemAsString(null).replace("xmlns=\"\"", "") + "\n"; } System.out.println("Query result: \n\t" + returnString); return returnString; }
@Before public void setUp() throws Exception { config = mock(XQueryRunConfig.class); binderFactory = mock(TypeBinderFactory.class); connection = mock(XQConnection.class); expression = mock(XQPreparedExpression.class); binder = new ContextItemBinder(binderFactory, config) { @Override protected String readFile(String fileName) throws IOException { fileRead = true; return FILE_CONTENTS; } }; typeBinder = mock(TypeBinder.class); given(binderFactory.getBinder(CONTEXT_ITEM_TYPE)).willReturn(typeBinder); }
/** * * @throws XQException * @throws XQException * @throws JAXBException * @throws IOException */ @Test public void testXQueryExpression() throws XQException { String sourceFile="synderTest.xml"; String xqFile="synderQuery1.xq"; Configuration saxonConfig = new Configuration(); SaxonXQDataSource dataSource = new SaxonXQDataSource(saxonConfig); XQConnection conn = dataSource.getConnection(); String xsString="for $Template in doc(\"synderTest.xml\")/TemplateContainer/Template/Tags "+ " return $Template" ; XQPreparedExpression exp = conn.prepareExpression(xsString); URI sourcUri=new File(sourceFile).toURI(); // exp.bindString(new QName("docName"), sourcUri.getPath(), conn // .createAtomicType(XQItemType.XQBASETYPE_STRING)); XQResultSequence result = exp.executeQuery(); String rawResult = result.getSequenceAsString(new Properties()); System.out.println("TransformTest.testXQueryExpression()..:"+rawResult); // System.out.println(tester.transfer(sourceFile, xqFile)); }
public String executeQuery(String query) throws XQException{ log.debug("语句执行begin:"+query); XQConnection connection= this.getXqConnection(); String result = connection.createExpression().executeQuery(query).getSequenceAsString(null); log.debug("语句执行end:"+result); return result; }
public XQSequence executeQuerySequence(String query) throws XQException{ log.debug("sequence语句执行begin:"+query); XQConnection connection= this.getXqConnection(); XQSequence sequence = connection.createExpression().executeQuery(query); log.debug("sequence语句执行end"); return sequence; }
@Override public XQSequence executeQuerySequence(String query, Map bind_map) throws XQException { XQSequence sequence = null; XQConnection connection = this.getXqConnection(); if (bind_map != null && !bind_map.isEmpty()) { // bind if (bind_map != null && !bind_map.isEmpty()) { XQPreparedExpression expression = connection .prepareExpression(query); Set<Map.Entry> set = bind_map.entrySet(); for (Iterator<Map.Entry> it = set.iterator(); it.hasNext();) { Map.Entry entry = (Map.Entry) it.next(); String value = (String) entry.getValue(); if(value!=null){ expression.bindString(new QName((String) entry.getKey()), value, null); } } sequence = expression.executeQuery(); } else { sequence = connection.createExpression().executeQuery(query); } } else { sequence = connection.createExpression().executeQuery(query); } return sequence; }
public XQSession openXQSession() throws XQException { BaseXXQDataSource xqds = new BaseXXQDataSource(); xqds.setProperty("serverName", ProperConfigUtil.getConfigByKey("SERVERIP")); xqds.setProperty("port",ProperConfigUtil.getConfigByKey("SERVERPORT")); XQConnection conn = xqds.getConnection(ProperConfigUtil.getConfigByKey("USER"),ProperConfigUtil.getConfigByKey("PASSWORD")); XQSession session = new XQSession(conn); return session; }
public XQSequence executeQuerySequence(String query, Map bind_map) throws XQException { XQSequence sequence = null; XQConnection connection = this.getXqConnection(); if (bind_map != null && !bind_map.isEmpty()) { if(log.isDebugEnabled()){ log.debug("查询参数:"+bind_map); } // bind if (bind_map != null && !bind_map.isEmpty()) { XQPreparedExpression expression = connection .prepareExpression(query); Set<Map.Entry> set = bind_map.entrySet(); for (Iterator<Map.Entry> it = set.iterator(); it.hasNext();) { Map.Entry entry = (Map.Entry) it.next(); String value = (String) entry.getValue(); if(value!=null){ expression.bindAtomicValue(new QName((String) entry.getKey()), value, null); } } sequence = expression.executeQuery(); } else { sequence = connection.createExpression().executeQuery(query); } } else { sequence = connection.createExpression().executeQuery(query); } return sequence; }
private void setupXQConnection(ApplicationContext ctx) throws XQException { XQDataSource xqds = ctx.getBean("xqDataSource", XQDataSource.class); String username = srvUser.getCurrentUser(); String password = srvUser.getUserPassword(username); if (password == null) { throw new XQException("no credentials found for user " + username); } XQConnection xqConn = xqds.getConnection(username, password); QueryManagement qMgr = ctx.getBean("queryManager", QueryManagement.class); qMgr.setXQConnection(xqConn); }
public static void main(String[] args) throws XQException { //System.setProperty("bdb.schema.members", "localhost:10600"); System.setProperty("logback.configurationFile", "hz-client-logging.xml"); System.setProperty("bdb.log.level", "trace"); // context = new ClassPathXmlApplicationContext("spring/xqj-client-context.xml"); XQConnection xqc = context.getBean("xqConnection", XQConnection.class); ClientApp client = new ClientApp(xqc); //HazelcastInstance hz = context.getBean("hzInstance", HazelcastInstance.class); boolean found = false; try { //client.storeSecCommand(); long id = client.storeSecQuery(); //long id = client.storeXmlDocument("axis.xml"); //System.out.println("document stored; id: " + id); //found = client.runPriceQuery("IBM"); //found &= client.runPriceQuery("IBM"); found = client.runSecQuery("IBM"); //found &= client.runSecQuery("VFINX"); //found &= client.runPriceQuery("PTTAX"); //found = client.searchSecQuery(); //found = client.searchSecQueryParams(); //client.searchSecQueryParams(); //found = client.runAxisQuery(); //client.removeSecCommand(id); } catch (XQException e) { e.printStackTrace(); } if (!found) { throw new XQException("result is empty"); } //context.close(); }
public static void main(String[] args) throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext("spring/xqj-client-context.xml"); XQConnection xqc = context.getBean("xqConnection", XQConnection.class); XQJClientSpringApp client = new XQJClientSpringApp(xqc); tester.testClient(client); }
public static void main(String[] args) throws XQException { System.setProperty(pn_config_properties_file, "embedded.properties"); context = new ClassPathXmlApplicationContext("spring/xqj-embedded-context.xml"); XQConnection xqc = context.getBean("xqFactory", XQConnection.class); EmbeddedApp client = new EmbeddedApp(xqc); //HazelcastInstance hz = context.getBean("hzInstance", HazelcastInstance.class); boolean found = false; try { //client.storeSecCommand(); long id = client.storeSecQuery(); //long id = client.storeXmlDocument("axis.xml"); //System.out.println("document stored; id: " + id); //found = client.runPriceQuery(); //client.runPriceQuery(); //found = client.runSecQuery(); //client.runSecQuery(); //found = client.searchSecQuery(); found = client.searchSecQueryParams(); //client.searchSecQueryParams(); //found = client.runAxisQuery(); client.removeSecCommand(id); } catch (XQException ex) { ex.printStackTrace(); } if (!found) { throw new XQException("result is empty"); } //context.close(); }
@Override protected XQConnection initialValue() { try { XQConnection xqc = xqds.getConnection(); setProperty(((BagriXQDataFactory) xqc).getProcessor().getProperties(), pn_client_fetchSize, null); setProperty(((BagriXQDataFactory) xqc).getProcessor().getProperties(), pn_client_submitTo, null); setProperty(((BagriXQDataFactory) xqc).getProcessor().getProperties(), pn_client_ownerParam, null); logger.info("initialValue.exit; XQC: {}", xqc); return xqc; } catch (XQException ex) { logger.error("", ex); return null; } }
@Override public void close() throws SQLException { XQConnection conn = getConnection(); if (!conn.isClosed()) { logger.info("close; XQC: {}", conn); try { conn.close(); } catch (XQException ex) { logger.error("close.error; " + ex, ex); throw new SQLException(ex); } } else { logger.debug("close; XQC is already closed: {}", conn); } }
@Override protected TPoXQueryManagerTest initialValue() { try { XQConnection xqc = xqds.getConnection(); SchemaRepository xdm = ((BagriXQDataFactory) xqc).getProcessor().getRepository(); TPoXQueryManagerTest xqmt = new TPoXQueryManagerTest(xdm); logger.info("initialValue.exit; XDM: {}", xdm); return xqmt; } catch (XQException ex) { logger.error("", ex); return null; } }
@Test public void testSaxon() throws XQException { SaxonXQDataSource xqds = new SaxonXQDataSource(); XQConnection xqc = xqds.getConnection(); XQExpression xqe = xqc.createExpression(); XQSequence xqs = xqe.executeQuery("<e>Hello world!</e>"); //XQSequence xqs = xqe.executeQuery("<a b='c'>{5+2}</a>"); while (xqs.next()) { System.out.println(xqs.getItemAsString(null)); } //System.out.println(xqds.getSchemaValidationMode()); }
@Test public void testGetByte() throws XQException { SaxonXQDataSource xqds = new SaxonXQDataSource(); XQConnection xqc = xqds.getConnection(); XQExpression xqe = xqc.createExpression(); XQSequence xqs = xqe.executeQuery("xs:byte('1')"); xqs.next(); Object o = xqs.getObject(); assertTrue(o instanceof Byte); xqe.close(); }
/** {@inheritDoc} */ @Override public XQConnection getConnection() throws XQException { String address = getAddress(); logger.trace("getConnection. creating new connection for address: {}", address); return initConnection(null); }
/** {@inheritDoc} */ @Override public XQConnection getConnection(Connection connection) throws XQException { // will work only if the Connection provided is an // another connection to the underlying cache throw new XQException("method not supported"); }
/** * {@inheritDoc} */ @Override public XQConnection getConnection(String username, String password) throws XQException { String address = getAddress(); logger.trace("getConnection. creating new connection for address: {}; user: {}", address, username); properties.put(USER, username); properties.put(PASSWORD, password); return initConnection(username); }
@Override public XQConnection getConnection(String username, String password) throws XQException { PooledXQConnection xqConn; synchronized (pool) { if (pool.size() > 0) { xqConn = pool.pop(); } else { xqConn = poolSource.getPooledConnection(username, password); } } xqConn.addConnectionEventListener(this); return xqConn.getConnection(); }
@BeforeClass public static void setUpBeforeClass() throws Exception { String dName = "../../etc/samples/test/"; String xml; try { xml = readTextFile(dName + "axis.xml"); } catch (IOException ex) { throw new XQException(ex.getMessage()); } xqds = new BagriXQDataSource(); xqds.setProperty(BagriXQDataSource.ADDRESS, "localhost:10500"); xqds.setProperty(BagriXQDataSource.SCHEMA, "default"); xqds.setProperty(BagriXQDataSource.USER, "guest"); xqds.setProperty(BagriXQDataSource.PASSWORD, "password"); xqds.setProperty(BagriXQDataSource.XQ_PROCESSOR, "com.bagri.xquery.saxon.XQProcessorClient"); xqds.setProperty(BagriXQDataSource.XDM_REPOSITORY, "com.bagri.client.hazelcast.impl.SchemaRepositoryImpl"); String query = "declare namespace bgdb=\"http://bagridb.com/bdb\";\n" + "declare variable $sec external;\n\n" + "for $id in bgdb:store-document($sec)\n" + "return $id\n"; XQConnection xqc = xqds.getConnection(); try { XQPreparedExpression xqpe = xqc.prepareExpression(query); xqpe.bindString(new QName("sec"), xml, xqc.createAtomicType(XQItemType.XQBASETYPE_STRING)); XQSequence xqs = xqpe.executeQuery(); if (xqs.next()) { axisId = xqs.getLong(); xqpe.close(); } else { xqpe.close(); throw new XQException("no response from store-document function"); } } finally { xqc.close(); } }
@AfterClass public static void tearDownAfterClass() throws Exception { XQConnection xqc = xqds.getConnection(); try { XQExpression xqe = xqc.createExpression(); xqe.bindLong(new QName("docId"), axisId, xqc.createAtomicType(XQItemType.XQBASETYPE_LONG)); xqe.executeCommand("removeDocument($docId)"); xqe.close(); } finally { xqc.close(); } }
@Test public void getConnectionTest() throws XQException { XQConnection conn = xqds.getConnection(); assertNotNull(conn); assertFalse(conn.isClosed()); conn.close(); assertTrue(conn.isClosed()); }
@Test @Ignore public void getConnectionWithCredentialsTest() throws XQException { String username = "test"; String password = "test"; XQConnection conn = xqds.getConnection(username, password); assertNull(conn); //assertFalse(conn.isClosed()); //conn.close(); }
@Test @Ignore public void testLoginTimeout() throws XQException { xqds.setLoginTimeout(1); XQConnection conn = xqds.getConnection(); assertNull(conn); xqds.setLoginTimeout(5); conn = xqds.getConnection(); assertNotNull(conn); conn.close(); }
public static ArrayList<String> sort(String xmlns, String fileName, String elementName, boolean isAsc) throws XQException { URI uri = new File(fileName).toURI(); String uriPath = uri.getPath(); ArrayList<String> rowIds = new ArrayList<String>(); XQDataSource dataSource = new SaxonXQDataSource(); XQConnection conn = dataSource.getConnection(); String queryString = "for $file in doc(\'" + uriPath + "')/xliff/file," + " $tu in $file/body//trans-unit order by $tu/" + elementName + " " + (isAsc ? "" : "descending") + " return <file original='{$file/@original}' tuid='{$tu/@id}'></file>"; if (xmlns != null) { queryString = "declare default element namespace '" + xmlns + "';" + queryString; } XQExpression expression = conn.createExpression(); XQSequence results = expression.executeQuery(queryString); while (results.next()) { Node node = results.getNode(); String original = node.getAttributes().getNamedItem("original").getNodeValue(); String tuid = node.getAttributes().getNamedItem("tuid").getNodeValue(); String rowId = RowIdUtil.getRowId(fileName, original, tuid); rowIds.add(rowId); } // 释放资源 results.close(); expression.close(); conn.close(); return rowIds; }
public XQConnection getConnection(XQDataSource dataSource) throws Exception { XQConnection connection; if (config.getDataSourceType().connectionPropertiesAreSupported() && config.getUsername() != null && config.getUsername().length() > 0) { connection = dataSource.getConnection(config.getUsername(), config.getPassword()); } else { connection = dataSource.getConnection(); } return connection; }
public void bindContextItem(XQConnection connection, XQPreparedExpression preparedExpression) throws Exception { if (config.isContextItemEnabled()) { String contextItemValue = config.isContextItemFromEditorEnabled() ? config.getContextItemText() : readFile(config.getContextItemFile()); binderFactory.getBinder(config.getContextItemType()).bind(preparedExpression, connection, XQConstants.CONTEXT_ITEM, contextItemValue, config.getContextItemType()); } }
public void bindVariables(XQConnection connection, XQPreparedExpression preparedExpression) throws Exception { for (XQueryRunnerVariable variable : config.getVariables()) { if (variable.ACTIVE) { TypeBinder binder = binderFactory.getBinder(variable.TYPE); QName name = nameExtractor.getName(variable.NAME, variable.NAMESPACE); binder.bind(preparedExpression, connection, name, variable.VALUE, variable.TYPE); } } }
public void runApp() throws Exception { XQDataSource xqs = dataSourceFactory.getDataSource(); XQConnection connection = null; try { connection = connectionFactory.getConnection(xqs); XQPreparedExpression preparedExpression = expressionFactory.getExpression(connection); XQResultSequence rs = preparedExpression.executeQuery(); rs.writeSequence(output, outputMethodFactory.getOutputMethodProperties()); } finally { if (connection != null) { connection.close(); } } }
public XQPreparedExpression getExpression(XQConnection connection) throws Exception { XQPreparedExpression preparedExpression = connection .prepareExpression(contentFactory.getXQueryContentAsStream()); contextItemBinder.bindContextItem(connection, preparedExpression); variablesBinder.bindVariables(connection, preparedExpression); return preparedExpression; }
@Before public void setUp() throws Exception { expression = mock(XQPreparedExpression.class); connection = mock(XQConnection.class); binder = new AttributeBinder(); xqItemType = mock(XQItemType.class); given(connection.createAttributeType(null, XQItemType.XQBASETYPE_ANYSIMPLETYPE)).willReturn(xqItemType); }
@Before public void setUp() throws XQException { expression = mock(XQPreparedExpression.class); connection = mock(XQConnection.class); binder = new AtomicValueBinder(); xqItemType = mock(XQItemType.class); given(connection.createAtomicType(anyInt())).willReturn(xqItemType); }