public boolean equals(Object obj) { if (!(obj instanceof NlsString)) { return false; } NlsString that = (NlsString) obj; return Util.equal(value, that.value) && Util.equal(charsetName, that.charsetName) && Util.equal(collation, that.collation); }
/** * Resolves a multi-part identifier such as "SCHEMA.EMP.EMPNO" to a namespace. The returned * namespace may represent a schema, table, column, etc. * * @pre names.size() > 0 * @post return != null */ public static SqlValidatorNamespace lookup(SqlValidatorScope scope, List<String> names) { Util.pre(names.size() > 0, "names.size() > 0"); SqlValidatorNamespace namespace = null; for (int i = 0; i < names.size(); i++) { String name = names.get(i); if (i == 0) { namespace = scope.resolve(name, null, null); } else { namespace = namespace.lookupChild(name); } } Util.permAssert(namespace != null, "post: namespace != null"); return namespace; }
/** * Extracts entries from the archive. * * @param ctx query context * @return text entries * @throws QueryException query exception */ private TokenList extract(final QueryContext ctx) throws QueryException { final B64 archive = (B64) checkType(checkItem(expr[0], ctx), AtomType.B64); TokenSet hs = null; if (expr.length > 1) { // filter result to specified entries hs = new TokenSet(); final Iter names = ctx.iter(expr[1]); for (Item en; (en = names.next()) != null; ) { hs.add(checkElmStr(en).string(info)); } } final TokenList tl = new TokenList(); final ArchiveIn in = ArchiveIn.get(archive.input(info), info); try { while (in.more()) { final ZipEntry ze = in.entry(); if (ze.isDirectory()) continue; if (hs == null || hs.delete(token(ze.getName())) != 0) tl.add(in.read()); } } catch (final IOException ex) { Util.debug(ex); ARCH_FAIL.thrw(info, ex); } finally { in.close(); } return tl; }
/** * Returns the entries of an archive. * * @param ctx query context * @return entries * @throws QueryException query exception */ private Iter entries(final QueryContext ctx) throws QueryException { final B64 archive = (B64) checkType(checkItem(expr[0], ctx), AtomType.B64); final ValueBuilder vb = new ValueBuilder(); final ArchiveIn in = ArchiveIn.get(archive.input(info), info); try { while (in.more()) { final ZipEntry ze = in.entry(); if (ze.isDirectory()) continue; final FElem e = new FElem(Q_ENTRY, NS); long s = ze.getSize(); if (s != -1) e.add(Q_SIZE, token(s)); s = ze.getTime(); if (s != -1) e.add(Q_LAST_MOD, new Dtm(s, info).string(info)); s = ze.getCompressedSize(); if (s != -1) e.add(Q_COMP_SIZE, token(s)); e.add(ze.getName()); vb.add(e); } return vb; } catch (final IOException ex) { Util.debug(ex); throw ARCH_FAIL.thrw(info, ex); } finally { in.close(); } }
/** * Returns the options of an archive. * * @param ctx query context * @return entries * @throws QueryException query exception */ private FElem options(final QueryContext ctx) throws QueryException { final B64 archive = (B64) checkType(checkItem(expr[0], ctx), AtomType.B64); String format = null; int level = -1; final ArchiveIn arch = ArchiveIn.get(archive.input(info), info); try { format = arch.format(); while (arch.more()) { final ZipEntry ze = arch.entry(); if (ze.isDirectory()) continue; level = ze.getMethod(); break; } } catch (final IOException ex) { Util.debug(ex); ARCH_FAIL.thrw(info, ex); } finally { arch.close(); } // create result element final FElem e = new FElem(Q_OPTIONS, NS); if (format != null) e.add(new FElem(Q_FORMAT).add(Q_VALUE, format)); if (level >= 0) { final byte[] lvl = level == 8 ? DEFLATE : level == 0 ? STORED : UNKNOWN; e.add(new FElem(Q_ALGORITHM).add(Q_VALUE, lvl)); } return e; }
/** * Returns an MD5 hash in lower case. * * @param string string to be hashed * @return md5 hash */ public static String md5(final String string) { try { final MessageDigest md = MessageDigest.getInstance("MD5"); return string(hex(md.digest(token(string)), false)); } catch (final Exception ex) { throw Util.notExpected(ex); } }
/** * Converts a token from the input encoding to UTF8. * * @param token token to be converted * @return byte array * @param encoding input encoding */ public static byte[] utf8(final byte[] token, final String encoding) { // UTF8 (comparison by ref.) or no special characters: return input if (encoding == UTF8 || ascii(token)) return token; // convert to utf8. if errors occur while converting, an empty is returned. try { return token(new String(token, encoding)); } catch (final Exception ex) { Util.debug(ex); return EMPTY; } }
@Override public void close() { try { gui.set(MainOptions.EXPORTER, options(SerialMethod.valueOf(method.getSelectedItem()))); } catch (final BaseXException ex) { throw Util.notExpected(ex); } if (!ok) return; super.close(); path.store(); }
public static void getSchemaObjectMonikers( SqlValidatorCatalogReader catalogReader, List<String> names, List<SqlMoniker> hints) { // Assume that the last name is 'dummy' or similar. List<String> subNames = Util.skipLast(names); hints.addAll(catalogReader.getAllSchemaObjectNames(subNames)); // If the name has length 0, try prepending the name of the default // schema. So, the empty name would yield a list of tables in the // default schema, as well as a list of schemas from the above code. if (subNames.size() == 0) { hints.addAll( catalogReader.getAllSchemaObjectNames( Collections.singletonList(catalogReader.getSchemaName()))); } }
/** * Creates a new archive. * * @param ctx query context * @return archive * @throws QueryException query exception */ private B64 create(final QueryContext ctx) throws QueryException { final Iter entr = ctx.iter(expr[0]); final Iter cont = ctx.iter(expr[1]); final Item opt = expr.length > 2 ? expr[2].item(ctx, info) : null; final TokenMap map = new FuncParams(Q_OPTIONS, info).parse(opt); final byte[] f = map.get(FORMAT); final String format = f != null ? string(lc(f)) : "zip"; final ArchiveOut out = ArchiveOut.get(format, info); // check algorithm final byte[] alg = map.get(ALGORITHM); int level = ZipEntry.DEFLATED; if (alg != null) { if (format.equals("zip") && !eq(alg, STORED, DEFLATE) || format.equals("gzip") && !eq(alg, DEFLATE)) { ARCH_SUPP.thrw(info, ALGORITHM, alg); } if (eq(alg, STORED)) level = ZipEntry.STORED; else if (eq(alg, DEFLATE)) level = ZipEntry.DEFLATED; } out.level(level); try { int e = 0; int c = 0; Item en, cn; while (true) { en = entr.next(); cn = cont.next(); if (en == null || cn == null) break; if (out instanceof GZIPOut && c > 0) ARCH_ONE.thrw(info, format.toUpperCase(Locale.ENGLISH)); add(checkElmStr(en), cn, out, level); e++; c++; } // count remaining entries if (cn != null) do c++; while (cont.next() != null); if (en != null) do e++; while (entr.next() != null); if (e != c) throw ARCH_DIFF.thrw(info, e, c); } catch (final IOException ex) { Util.debug(ex); throw ARCH_FAIL.thrw(info, ex); } finally { out.close(); } return new B64(out.toArray()); }
/** * Updates an archive. * * @param ctx query context * @return updated archive * @throws QueryException query exception */ private B64 update(final QueryContext ctx) throws QueryException { final B64 archive = (B64) checkType(checkItem(expr[0], ctx), AtomType.B64); // entries to be updated final TokenObjMap<Item[]> hm = new TokenObjMap<Item[]>(); final Iter entr = ctx.iter(expr[1]); final Iter cont = ctx.iter(expr[2]); int e = 0; int c = 0; Item en, cn; while (true) { en = entr.next(); cn = cont.next(); if (en == null || cn == null) break; hm.add(checkElmStr(en).string(info), new Item[] {en, cn}); e++; c++; } // count remaining entries if (cn != null) do c++; while (cont.next() != null); if (en != null) do e++; while (entr.next() != null); if (e != c) ARCH_DIFF.thrw(info, e, c); final ArchiveIn in = ArchiveIn.get(archive.input(info), info); final ArchiveOut out = ArchiveOut.get(in.format(), info); try { if (in instanceof GZIPIn) ARCH_MODIFY.thrw(info, in.format().toUpperCase(Locale.ENGLISH)); // delete entries to be updated while (in.more()) if (!hm.contains(token(in.entry().getName()))) out.write(in); // add new and updated entries for (final byte[] h : hm) { if (h == null) continue; final Item[] it = hm.get(h); add(it[0], it[1], out, ZipEntry.DEFLATED); } } catch (final IOException ex) { Util.debug(ex); ARCH_FAIL.thrw(info, ex); } finally { in.close(); out.close(); } return new B64(out.toArray()); }
/** * Tests writing of request content when @src is set. * * @throws IOException I/O Exception */ @Test public void writeFromResource() throws IOException { // Create a file form which will be read final IOFile file = new IOFile(Prop.TMP, Util.className(FnHttpTest.class)); file.write(token("test")); // Request final HttpRequest req = new HttpRequest(); req.payloadAttrs.put("src", file.url()); req.payloadAttrs.put("method", "binary"); // HTTP connection final FakeHttpConnection fakeConn = new FakeHttpConnection(new URL("http://www.test.com")); HttpClient.setRequestContent(fakeConn.getOutputStream(), req); // Delete file file.delete(); assertEquals(fakeConn.out.toString(Strings.UTF8), "test"); }
/** * Returns the string quoted for SQL, for example <code>_ISO-8859-1'is it a * plane? no it''s superman!'</code>. * * @param prefix if true, prefix the character set name * @param suffix if true, suffix the collation clause * @return the quoted string */ public String asSql(boolean prefix, boolean suffix) { StringBuilder ret = new StringBuilder(); if (prefix && (null != charsetName)) { ret.append("_"); ret.append(charsetName); } ret.append("'"); ret.append(Util.replace(value, "'", "''")); ret.append("'"); // NOTE jvs 3-Feb-2005: see FRG-78 for why this should go away if (false) { if (suffix && (null != collation)) { ret.append(" "); ret.append(collation.toString()); } } return ret.toString(); }
/** * Derives an alias for a node, and invents a mangled identifier if it cannot. * * <p>Examples: * * <ul> * <li>Alias: "1 + 2 as foo" yields "foo" * <li>Identifier: "foo.bar.baz" yields "baz" * <li>Anything else yields "expr$<i>ordinal</i>" * </ul> * * @return An alias, if one can be derived; or a synthetic alias "expr$<i>ordinal</i>" if ordinal * >= 0; otherwise null */ public static String getAlias(SqlNode node, int ordinal) { switch (node.getKind()) { case AS: // E.g. "1 + 2 as foo" --> "foo" return ((SqlCall) node).getOperands()[1].toString(); case OVER: // E.g. "bids over w" --> "bids" return getAlias(((SqlCall) node).getOperands()[0], ordinal); case IDENTIFIER: // E.g. "foo.bar" --> "bar" return Util.last(((SqlIdentifier) node).names); default: if (ordinal < 0) { return null; } else { return SqlUtil.deriveAliasFromOrdinal(ordinal); } } }
/** * Deletes files from an archive. * * @param ctx query context * @return updated archive * @throws QueryException query exception */ private B64 delete(final QueryContext ctx) throws QueryException { final B64 archive = (B64) checkType(checkItem(expr[0], ctx), AtomType.B64); // entries to be deleted final TokenObjMap<Item[]> hm = new TokenObjMap<Item[]>(); final Iter names = ctx.iter(expr[1]); for (Item en; (en = names.next()) != null; ) { hm.add(checkElmStr(en).string(info), null); } final ArchiveIn in = ArchiveIn.get(archive.input(info), info); final ArchiveOut out = ArchiveOut.get(in.format(), info); try { if (in instanceof GZIPIn) ARCH_MODIFY.thrw(info, in.format().toUpperCase(Locale.ENGLISH)); while (in.more()) if (!hm.contains(token(in.entry().getName()))) out.write(in); } catch (final IOException ex) { Util.debug(ex); ARCH_FAIL.thrw(info, ex); } finally { in.close(); out.close(); } return new B64(out.toArray()); }
public String apply(String original, int attempt, int size) { return Util.first(original, "$f") + size; }
public String apply(String original, int attempt, int size) { return Util.first(original, "EXPR$") + attempt; }
public int hashCode() { int h = value.hashCode(); h = Util.hash(h, charsetName); h = Util.hash(h, collation); return h; }