/** * Import an XQuery library module from the given document. The namespace and preferred prefix of * the module are extracted from the module itself. The MIME type of the document is set to * "application/xquery" as a side-effect. * * @param module the non-XML document that holds the library module's source * @return this service, to chain calls * @throws DatabaseException if the module is an XML document, or the module declaration cannot be * found at the top of the document */ public QueryService importModule(Document module) { if (module instanceof XMLDocument) throw new DatabaseException("module cannot be an XML document: " + module); Matcher matcher = MODULE_DECLARATION_DQUOTE.matcher(module.contentsAsString()); if (!matcher.find()) { matcher = MODULE_DECLARATION_SQUOTE.matcher(module.contentsAsString()); if (!matcher.find()) throw new DatabaseException("couldn't find a module declaration at the top of " + module); } module.metadata().setMimeType("application/xquery"); String moduleNamespace = matcher.group(1); // TODO: should do URILiteral processing here to replace entity and character references and // normalize // whitespace, but since it seems that eXist doesn't do it either (bug?) there's no reason to // rush. Document prevModule = moduleMap.get(moduleNamespace); if (prevModule != null && !prevModule.equals(module)) throw new DatabaseException( "module " + moduleNamespace + " already bound to " + prevModule + ", can't rebind to " + module); moduleMap.put(moduleNamespace, module); return this; }
public static void testEnum() throws Exception { Builder b = new Builder(); b.addClasspath(new File("bin")); b.setProperty("Export-Package", "test.metatype"); b.setProperty("-metatype", "*"); b.build(); assertEquals(0, b.getErrors().size()); assertEquals(0, b.getWarnings().size()); Resource r = b.getJar().getResource("OSGI-INF/metatype/test.metatype.MetatypeTest$Enums.xml"); IO.copy(r.openInputStream(), System.err); Document d = db.parse(r.openInputStream()); assertEquals( "http://www.osgi.org/xmlns/metatype/v1.1.0", d.getDocumentElement().getNamespaceURI()); Properties p = new Properties(); p.setProperty("r", "requireConfiguration"); p.setProperty("i", "ignoreConfiguration"); p.setProperty("o", "optionalConfiguration"); Enums enums = Configurable.createConfigurable(Enums.class, (Map<Object, Object>) p); assertEquals(Enums.X.requireConfiguration, enums.r()); assertEquals(Enums.X.ignoreConfiguration, enums.i()); assertEquals(Enums.X.optionalConfiguration, enums.o()); }
private String addWhiteSpace(Document doc, int offset) throws BadLocationException { StringBuilder whiteSpace = new StringBuilder("\n"); Element rootElement = doc.getDefaultRootElement(); int line = rootElement.getElementIndex(offset); int i = rootElement.getElement(line).getStartOffset(); while (true) { String temp = doc.getText(i, 1); if (temp.equals(" ") || temp.equals("\t")) { whiteSpace.append(temp); i++; } else break; } return whiteSpace.toString(); }
/** * Carries out preprocessing that makes JEuclid handle the document better. * * @param doc Document */ static void preprocessForJEuclid(Document doc) { // underbrace and overbrace NodeList list = doc.getElementsByTagName("mo"); for (int i = 0; i < list.getLength(); i++) { Element mo = (Element) list.item(i); String parentName = ((Element) mo.getParentNode()).getTagName(); if (parentName == null) { continue; } if (parentName.equals("munder") && isTextChild(mo, "\ufe38")) { mo.setAttribute("stretchy", "true"); mo.removeChild(mo.getFirstChild()); mo.appendChild(doc.createTextNode("\u23df")); } else if (parentName.equals("mover") && isTextChild(mo, "\ufe37")) { mo.setAttribute("stretchy", "true"); mo.removeChild(mo.getFirstChild()); mo.appendChild(doc.createTextNode("\u23de")); } } // menclose for long division doesn't allow enough top padding. Oh, and // <mpadded> isn't implemented. And there isn't enough padding to left of // the bar either. Solve by adding an <mover> with just an <mspace> over# // the longdiv, contained within an mrow that adds a <mspace> before it. list = doc.getElementsByTagName("menclose"); for (int i = 0; i < list.getLength(); i++) { Element menclose = (Element) list.item(i); // Only for longdiv if (!"longdiv".equals(menclose.getAttribute("notation"))) { continue; } Element mrow = doc.createElementNS(WebMathsService.NS, "mrow"); Element mover = doc.createElementNS(WebMathsService.NS, "mover"); Element mspace = doc.createElementNS(WebMathsService.NS, "mspace"); Element mspaceW = doc.createElementNS(WebMathsService.NS, "mspace"); boolean previousElement = false; for (Node previous = menclose.getPreviousSibling(); previous != null; previous = previous.getPreviousSibling()) { if (previous.getNodeType() == Node.ELEMENT_NODE) { previousElement = true; break; } } if (previousElement) { mspaceW.setAttribute("width", "4px"); } menclose.getParentNode().insertBefore(mrow, menclose); menclose.getParentNode().removeChild(menclose); mrow.appendChild(mspaceW); mrow.appendChild(mover); mover.appendChild(menclose); mover.appendChild(mspace); } }
@Override protected int drawUnselectedText(Graphics graphics, int x, int y, int p0, int p1) throws BadLocationException { try { Document doc = getDocument(); String text = doc.getText(p0, p1 - p0); Segment segment = getLineBuffer(); int initialXpos = x; SortedMap<Integer, Integer> startMap = new TreeMap<Integer, Integer>(); SortedMap<Integer, Color> colorMap = new TreeMap<Integer, Color>(); // Match all regexes on this snippet, store positions for (Map.Entry<Pattern, Color> entry : patternColors.entrySet()) { Matcher matcher = entry.getKey().matcher(text); while (matcher.find()) { startMap.put(matcher.start(1), matcher.end(1)); colorMap.put(matcher.start(1), entry.getValue()); } } // TODO: check the map for overlapping parts int i = 0; // add tag highlighted background colors if (!TAG_HIGHLIGHTED.isEmpty()) { Matcher highlightMatcher = Pattern.compile(TAG_HIGHLIGHTED).matcher(text); while (highlightMatcher.find()) { int start = highlightMatcher.start(1); int end = highlightMatcher.end(1); if (i < start) { graphics.setColor(Color.black); doc.getText(p0 + i, start - i, segment); x = Utilities.drawTabbedText(segment, x, y, graphics, this, i); } graphics.setColor(TAG_HIGHLIGHTED_COLOR); i = end; doc.getText(p0 + start, i - start, segment); int width = Utilities.getTabbedTextWidth( segment, graphics.getFontMetrics(), x, this, p0 + start); // graphics.drawLine(x, y, width, y);graphics.getFontMetrics() graphics.fillRect( x, y - graphics.getFontMetrics().getHeight() + 2, width, graphics.getFontMetrics().getHeight()); graphics.setColor(Color.black); doc.getText(p0 + start, i - start, segment); x = Utilities.drawTabbedText(segment, x, y, graphics, this, start); } } x = initialXpos; i = 0; // add highlighted background colors based on position // String textx = doc.getText(p0, p1 - p0); if ((HIGHLIGHTED_START < p1 && HIGHLIGHTED_START >= p0) || (HIGHLIGHTED_END <= p1 && HIGHLIGHTED_END > p0) || (HIGHLIGHTED_START < p1 && HIGHLIGHTED_END > p0)) { // select whole line int start = 0; int end = text.length(); // test to see if only partial line is needed. if (HIGHLIGHTED_START > p0) start = HIGHLIGHTED_START - p0; if (HIGHLIGHTED_END < p1) end -= p1 - HIGHLIGHTED_END; if (i < start) { // fill in normal color if start highlight isn't at the beginning graphics.setColor(Color.black); doc.getText(p0 + i, start - i, segment); x = Utilities.drawTabbedText(segment, x, y, graphics, this, i); } graphics.setColor(HIGHLIGHTED_COLOR); // fill in the highlight color i = end; if (i - start > 0) { doc.getText(p0 + start, i - start, segment); int width = Utilities.getTabbedTextWidth( segment, graphics.getFontMetrics(), x, this, p0 + start); // graphics.drawLine(x, y, width, y);graphics.getFontMetrics() graphics.fillRect( x, y - graphics.getFontMetrics().getHeight() + 2, width, graphics.getFontMetrics().getHeight()); graphics.setColor(Color.black); doc.getText(p0 + start, i - start, segment); x = Utilities.drawTabbedText(segment, x, y, graphics, this, start); } // else // System.out.println("invalid highlighting " + (i - start) + " is <= 0 (" + p0 + "-" + // p1 + "=" + (p1 - p0) +") " + start + ", " + end + " len=" + text.length()); } x = initialXpos; i = 0; // Color the parts of xml foreground font for (Map.Entry<Integer, Integer> entry : startMap.entrySet()) { int start = entry.getKey(); int end = entry.getValue(); if (i < start) { graphics.setColor(Color.black); doc.getText(p0 + i, start - i, segment); x = Utilities.drawTabbedText(segment, x, y, graphics, this, i); } graphics.setColor(colorMap.get(start)); i = end; doc.getText(p0 + start, i - start, segment); x = Utilities.drawTabbedText(segment, x, y, graphics, this, start); } // Paint possible remaining text black if (i < text.length()) { graphics.setColor(Color.black); doc.getText(p0 + i, text.length() - i, segment); x = Utilities.drawTabbedText(segment, x, y, graphics, this, i); } } catch (Exception e) { e.printStackTrace(); } return x; }
public static void testNaming() throws Exception { Map<String, Object> map = Create.map(); map.put("_secret", "_secret"); map.put("_secret", "_secret"); map.put(".secret", ".secret"); map.put("$new", "$new"); map.put("new", "new"); map.put("secret", "secret"); map.put("a_b_c", "a_b_c"); map.put("a.b.c", "a.b.c"); map.put(".a_b", ".a_b"); map.put("$$$$a_b", "$$$$a_b"); map.put("$$$$a.b", "$$$$a.b"); map.put("a", "a"); map.put("a$", "a$"); map.put("a$$", "a$$"); map.put("a$.$", "a$.$"); map.put("a$_$", "a$_$"); map.put("a..", "a.."); map.put("noid", "noid"); map.put("nullid", "nullid"); Naming trt = Configurable.createConfigurable(Naming.class, map); // By name assertEquals("secret", trt.secret()); assertEquals("_secret", trt.__secret()); assertEquals(".secret", trt._secret()); assertEquals("new", trt.$new()); assertEquals("$new", trt.$$new()); assertEquals("a.b.c", trt.a_b_c()); assertEquals("a_b_c", trt.a__b__c()); assertEquals(".a_b", trt._a__b()); assertEquals("$$$$a.b", trt.$$$$$$$$a_b()); assertEquals("$$$$a_b", trt.$$$$$$$$a__b()); assertEquals("a", trt.a$()); assertEquals("a$", trt.a$$()); assertEquals("a$", trt.a$$$()); assertEquals("a$.$", trt.a$$_$$()); assertEquals("a$_$", trt.a$$__$$()); assertEquals("a..", trt.a_$_()); assertEquals("noid", trt.noid()); assertEquals("nullid", trt.nullid()); // By AD assertEquals("secret", trt.xsecret()); assertEquals("_secret", trt.x__secret()); assertEquals(".secret", trt.x_secret()); assertEquals("new", trt.x$new()); assertEquals("$new", trt.x$$new()); assertEquals("a.b.c", trt.xa_b_c()); assertEquals("a_b_c", trt.xa__b__c()); assertEquals(".a_b", trt.x_a__b()); assertEquals("$$$$a.b", trt.x$$$$$$$$a_b()); assertEquals("$$$$a_b", trt.x$$$$$$$$a__b()); assertEquals("a", trt.xa$()); assertEquals("a$", trt.xa$$()); assertEquals("a$", trt.xa$$$()); assertEquals("a$.$", trt.xa$$_$$()); Builder b = new Builder(); b.addClasspath(new File("bin")); b.setProperty("Export-Package", "test.metatype"); b.setProperty("-metatype", "*"); b.build(); assertEquals(0, b.getErrors().size()); assertEquals(0, b.getWarnings().size()); Resource r = b.getJar().getResource("OSGI-INF/metatype/test.metatype.MetatypeTest$Naming.xml"); IO.copy(r.openInputStream(), System.err); Document d = db.parse(r.openInputStream(), "UTF-8"); assertEquals( "http://www.osgi.org/xmlns/metatype/v1.1.0", d.getDocumentElement().getNamespaceURI()); }
public static void testReturnTypes() throws Exception { Builder b = new Builder(); b.addClasspath(new File("bin")); b.setProperty("Export-Package", "test.metatype"); b.setProperty("-metatype", "*"); b.build(); Resource r = b.getJar().getResource("OSGI-INF/metatype/test.metatype.MetatypeTest$TestReturnTypes.xml"); assertEquals(0, b.getErrors().size()); assertEquals(0, b.getWarnings().size()); System.err.println(b.getJar().getResources().keySet()); assertNotNull(r); IO.copy(r.openInputStream(), System.err); Document d = db.parse(r.openInputStream()); assertEquals( "http://www.osgi.org/xmlns/metatype/v1.1.0", d.getDocumentElement().getNamespaceURI()); // Primitives assertEquals("Boolean", xpath.evaluate("//OCD/AD[@id='rpBoolean']/@type", d)); assertEquals("Byte", xpath.evaluate("//OCD/AD[@id='rpByte']/@type", d)); assertEquals("Character", xpath.evaluate("//OCD/AD[@id='rpCharacter']/@type", d)); assertEquals("Short", xpath.evaluate("//OCD/AD[@id='rpShort']/@type", d)); assertEquals("Integer", xpath.evaluate("//OCD/AD[@id='rpInt']/@type", d)); assertEquals("Long", xpath.evaluate("//OCD/AD[@id='rpLong']/@type", d)); assertEquals("Float", xpath.evaluate("//OCD/AD[@id='rpFloat']/@type", d)); assertEquals("Double", xpath.evaluate("//OCD/AD[@id='rpDouble']/@type", d)); // Primitive Wrappers assertEquals("Boolean", xpath.evaluate("//OCD/AD[@id='rBoolean']/@type", d)); assertEquals("Byte", xpath.evaluate("//OCD/AD[@id='rByte']/@type", d)); assertEquals("Character", xpath.evaluate("//OCD/AD[@id='rCharacter']/@type", d)); assertEquals("Short", xpath.evaluate("//OCD/AD[@id='rShort']/@type", d)); assertEquals("Integer", xpath.evaluate("//OCD/AD[@id='rInt']/@type", d)); assertEquals("Long", xpath.evaluate("//OCD/AD[@id='rLong']/@type", d)); assertEquals("Float", xpath.evaluate("//OCD/AD[@id='rFloat']/@type", d)); assertEquals("Double", xpath.evaluate("//OCD/AD[@id='rDouble']/@type", d)); // Primitive Arrays assertEquals("Boolean", xpath.evaluate("//OCD/AD[@id='rpaBoolean']/@type", d)); assertEquals("Byte", xpath.evaluate("//OCD/AD[@id='rpaByte']/@type", d)); assertEquals("Character", xpath.evaluate("//OCD/AD[@id='rpaCharacter']/@type", d)); assertEquals("Short", xpath.evaluate("//OCD/AD[@id='rpaShort']/@type", d)); assertEquals("Integer", xpath.evaluate("//OCD/AD[@id='rpaInt']/@type", d)); assertEquals("Long", xpath.evaluate("//OCD/AD[@id='rpaLong']/@type", d)); assertEquals("Float", xpath.evaluate("//OCD/AD[@id='rpaFloat']/@type", d)); assertEquals("Double", xpath.evaluate("//OCD/AD[@id='rpaDouble']/@type", d)); assertEquals("2147483647", xpath.evaluate("//OCD/AD[@id='rpaBoolean']/@cardinality", d)); assertEquals("2147483647", xpath.evaluate("//OCD/AD[@id='rpaByte']/@cardinality", d)); assertEquals("2147483647", xpath.evaluate("//OCD/AD[@id='rpaCharacter']/@cardinality", d)); assertEquals("2147483647", xpath.evaluate("//OCD/AD[@id='rpaShort']/@cardinality", d)); assertEquals("2147483647", xpath.evaluate("//OCD/AD[@id='rpaInt']/@cardinality", d)); assertEquals("2147483647", xpath.evaluate("//OCD/AD[@id='rpaLong']/@cardinality", d)); assertEquals("2147483647", xpath.evaluate("//OCD/AD[@id='rpaFloat']/@cardinality", d)); assertEquals("2147483647", xpath.evaluate("//OCD/AD[@id='rpaDouble']/@cardinality", d)); // Wrapper + Object arrays assertEquals("Boolean", xpath.evaluate("//OCD/AD[@id='raBoolean']/@type", d)); assertEquals("Byte", xpath.evaluate("//OCD/AD[@id='raByte']/@type", d)); assertEquals("Character", xpath.evaluate("//OCD/AD[@id='raCharacter']/@type", d)); assertEquals("Short", xpath.evaluate("//OCD/AD[@id='raShort']/@type", d)); assertEquals("Integer", xpath.evaluate("//OCD/AD[@id='raInt']/@type", d)); assertEquals("Long", xpath.evaluate("//OCD/AD[@id='raLong']/@type", d)); assertEquals("Float", xpath.evaluate("//OCD/AD[@id='raFloat']/@type", d)); assertEquals("Double", xpath.evaluate("//OCD/AD[@id='raDouble']/@type", d)); assertEquals("String", xpath.evaluate("//OCD/AD[@id='raString']/@type", d)); assertEquals("String", xpath.evaluate("//OCD/AD[@id='raURI']/@type", d)); assertEquals("2147483647", xpath.evaluate("//OCD/AD[@id='raBoolean']/@cardinality", d)); assertEquals("2147483647", xpath.evaluate("//OCD/AD[@id='raByte']/@cardinality", d)); assertEquals("2147483647", xpath.evaluate("//OCD/AD[@id='raCharacter']/@cardinality", d)); assertEquals("2147483647", xpath.evaluate("//OCD/AD[@id='raShort']/@cardinality", d)); assertEquals("2147483647", xpath.evaluate("//OCD/AD[@id='raInt']/@cardinality", d)); assertEquals("2147483647", xpath.evaluate("//OCD/AD[@id='raLong']/@cardinality", d)); assertEquals("2147483647", xpath.evaluate("//OCD/AD[@id='raFloat']/@cardinality", d)); assertEquals("2147483647", xpath.evaluate("//OCD/AD[@id='raDouble']/@cardinality", d)); assertEquals("2147483647", xpath.evaluate("//OCD/AD[@id='raString']/@cardinality", d)); assertEquals("2147483647", xpath.evaluate("//OCD/AD[@id='raURI']/@cardinality", d)); // Wrapper + Object collections assertEquals("Boolean", xpath.evaluate("//OCD/AD[@id='rBooleans']/@type", d)); assertEquals("Byte", xpath.evaluate("//OCD/AD[@id='rBytes']/@type", d)); assertEquals("Character", xpath.evaluate("//OCD/AD[@id='rCharacter']/@type", d)); assertEquals("Short", xpath.evaluate("//OCD/AD[@id='rShorts']/@type", d)); assertEquals("Integer", xpath.evaluate("//OCD/AD[@id='rInts']/@type", d)); assertEquals("Long", xpath.evaluate("//OCD/AD[@id='rLongs']/@type", d)); assertEquals("Float", xpath.evaluate("//OCD/AD[@id='rFloats']/@type", d)); assertEquals("Double", xpath.evaluate("//OCD/AD[@id='rDoubles']/@type", d)); assertEquals("String", xpath.evaluate("//OCD/AD[@id='rStrings']/@type", d)); assertEquals("String", xpath.evaluate("//OCD/AD[@id='rURIs']/@type", d)); assertEquals("-2147483648", xpath.evaluate("//OCD/AD[@id='rBooleans']/@cardinality", d)); assertEquals("-2147483648", xpath.evaluate("//OCD/AD[@id='rBytes']/@cardinality", d)); assertEquals("-2147483648", xpath.evaluate("//OCD/AD[@id='rCharacters']/@cardinality", d)); assertEquals("-2147483648", xpath.evaluate("//OCD/AD[@id='rShorts']/@cardinality", d)); assertEquals("-2147483648", xpath.evaluate("//OCD/AD[@id='rInts']/@cardinality", d)); assertEquals("-2147483648", xpath.evaluate("//OCD/AD[@id='rLongs']/@cardinality", d)); assertEquals("-2147483648", xpath.evaluate("//OCD/AD[@id='rFloats']/@cardinality", d)); assertEquals("-2147483648", xpath.evaluate("//OCD/AD[@id='rDoubles']/@cardinality", d)); assertEquals("-2147483648", xpath.evaluate("//OCD/AD[@id='rStrings']/@cardinality", d)); assertEquals("-2147483648", xpath.evaluate("//OCD/AD[@id='rURIs']/@cardinality", d)); }