@Override public void setResult(Result result) throws IllegalArgumentException { SAXResult saxResult = toSAXResult(result); ContentHandler handler = saxResult.getHandler(); this.nextContentHandler = handler; if (handler instanceof LexicalHandler) { this.nextLexicalHandler = (LexicalHandler) handler; } if (handler instanceof DTDHandler) { this.nextDtdHandler = (DTDHandler) handler; } }
@Override public void extractText(String mimeType, InputStream input, StringBuilder outputText, int maxSize) throws IOException { WriteOutContentHandler wrapped = new WriteOutContentHandler(maxSize); ContentHandler handler = new BodyContentHandler(wrapped); try { Metadata meta = new Metadata(); Parser parser = new AutoDetectParser(new TikaConfig(getClass().getClassLoader())); parser.parse(input, handler, meta, new ParseContext()); appendText(handler, outputText, maxSize); } catch( Exception t ) { if( wrapped.isWriteLimitReached(t) ) { // keep going LOGGER.debug("PDF size limit reached. Indexing truncated text"); appendText(handler, outputText, maxSize); return; } throw Throwables.propagate(t); } }
public void parse( InputSource source, ContentHandler contentHandler, ErrorHandler errorHandler, EntityResolver entityResolver ) throws SAXException, IOException { String systemId = source.getSystemId(); Document dom = forest.get(systemId); if(dom==null) { // if no DOM tree is built for it, // let the fall back parser parse the original document. // // for example, XSOM parses datatypes.xsd (XML Schema part 2) // but this will never be built into the forest. fallbackParser.parse( source, contentHandler, errorHandler, entityResolver ); return; } scanner.scan( dom, contentHandler ); }
/** * Serialise the meta data for a table. * * @param table The meta data to serialise. * @param contentHandler Content handler to receive the meta data xml. * @throws SAXException Propagates from SAX API calls. */ private void outputTableMetaData(Table table, ContentHandler contentHandler) throws SAXException { AttributesImpl tableAttributes = new AttributesImpl(); tableAttributes.addAttribute(XmlDataSetNode.URI, XmlDataSetNode.NAME_ATTRIBUTE, XmlDataSetNode.NAME_ATTRIBUTE, XmlDataSetNode.STRING_TYPE, table.getName()); contentHandler.startElement(XmlDataSetNode.URI, XmlDataSetNode.METADATA_NODE, XmlDataSetNode.METADATA_NODE, tableAttributes); for (Column column : table.columns()) { emptyElement(contentHandler, XmlDataSetNode.COLUMN_NODE, buildColumnAttributes(column)); } // we need to sort the indexes by name to ensure consistency, since indexes don't have an explicit "sequence" in databases. List<Index> indexes = new ArrayList<>(table.indexes()); Collections.sort(indexes, new Comparator<Index>() { @Override public int compare(Index o1, Index o2) { return o1.getName().compareTo(o2.getName()); } }); for (Index index : indexes) { emptyElement(contentHandler, XmlDataSetNode.INDEX_NODE, buildIndexAttributes(index)); } contentHandler.endElement(XmlDataSetNode.URI, XmlDataSetNode.METADATA_NODE, XmlDataSetNode.METADATA_NODE); }
/** * Execute an expression in the XPath runtime context, and return the * result of the expression. * * * @param xctxt The XPath runtime context. * * @return The result of the expression in the form of a <code>XObject</code>. * * @throws javax.xml.transform.TransformerException if a runtime exception * occurs. */ public void executeCharsToContentHandler(XPathContext xctxt, ContentHandler handler) throws javax.xml.transform.TransformerException, org.xml.sax.SAXException { if(Arg0IsNodesetExpr()) { int node = getArg0AsNode(xctxt); if(DTM.NULL != node) { DTM dtm = xctxt.getDTM(node); dtm.dispatchCharactersEvents(node, handler, true); } } else { XObject obj = execute(xctxt); obj.dispatchCharactersEvents(handler); } }
/** * Test method for {@link XmlGeneratorUtils#endElement(ContentHandler, String, String)}. */ @Test public void testEndElementContentHandlerStringString() throws SAXException { final AtomicReference<String> uriRef = new AtomicReference<String>(); final AtomicReference<String> localnameRef = new AtomicReference<String>(); final AtomicReference<String> qNameRef = new AtomicReference<String>(); final ContentHandler contentHandler = new MockContentHandler() { @Override public void endElement(String uri, String localName, String qName) throws SAXException { uriRef.set(uri); localnameRef.set(localName); qNameRef.set(qName); } }; XmlGeneratorUtils.endElement(contentHandler, "prefix", "localname"); assertEquals("localname", localnameRef.get()); assertEquals("prefix:localname", qNameRef.get()); assertEquals(NULL_NS_URI, uriRef.get()); }
/** * Parses and shows the content of one sheet using the specified styles and * shared-strings tables. * * @param styles * @param strings * @param sheetInputStream */ public void processSheet(StylesTable styles, ReadOnlySharedStringsTable strings, SheetContentsHandler sheetHandler, InputStream sheetInputStream) throws IOException, ParserConfigurationException, SAXException { DataFormatter formatter = new DataFormatter(); InputSource sheetSource = new InputSource(sheetInputStream); try { XMLReader sheetParser = SAXHelper.newXMLReader(); ContentHandler handler = new XSSFSheetXMLHandler(styles, null, strings, sheetHandler, formatter, false); sheetParser.setContentHandler(handler); sheetParser.parse(sheetSource); } catch (ParserConfigurationException e) { throw new RuntimeException("SAX parser appears to be broken - " + e.getMessage()); } }
@Test public void testContentHandler() { ValidatorHandler validatorHandler = getValidatorHandler(); assertNull(validatorHandler.getContentHandler(), "When ValidatorHandler is created, initially ContentHandler should not be set."); ContentHandler handler = new DefaultHandler(); validatorHandler.setContentHandler(handler); assertSame(validatorHandler.getContentHandler(), handler); validatorHandler.setContentHandler(null); assertNull(validatorHandler.getContentHandler()); }
public final ContentHandler createContentHandler() { final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); return new ASMContentHandler(cw) { @Override public void endDocument() throws SAXException { try { os.write(cw.toByteArray()); } catch (IOException e) { throw new SAXException(e); } } }; }
/** * Verifies parserAdapter.getContentHandler() */ @Test public void contentHandler01() { ContentHandler contentHandler = new XMLFilterImpl(); parserAdapter.setContentHandler(contentHandler); assertNotNull(parserAdapter.getContentHandler()); }
public void setContentHandler(ContentHandler handler) throws NullPointerException { _sax = handler; if (handler instanceof LexicalHandler) { _lex = (LexicalHandler)handler; } if (handler instanceof SAXImpl) { _saxImpl = (SAXImpl)handler; } }
private void processEntry(final ZipInputStream zis, final ZipEntry ze, final ContentHandlerFactory handlerFactory) { ContentHandler handler = handlerFactory.createContentHandler(); try { // if (CODE2ASM.equals(command)) { // read bytecode and process it // // with TraceClassVisitor // ClassReader cr = new ClassReader(readEntry(zis, ze)); // cr.accept(new TraceClassVisitor(null, new PrintWriter(os)), // false); // } boolean singleInputDocument = inRepresentation == SINGLE_XML; if (inRepresentation == BYTECODE) { // read bytecode and process it // with handler ClassReader cr = new ClassReader(readEntry(zis, ze)); cr.accept(new SAXClassAdapter(handler, singleInputDocument), 0); } else { // read XML and process it with handler XMLReader reader = XMLReaderFactory.createXMLReader(); reader.setContentHandler(handler); reader.parse(new InputSource( singleInputDocument ? (InputStream) new ProtectedInputStream( zis) : new ByteArrayInputStream(readEntry(zis, ze)))); } } catch (Exception ex) { update(ze.getName(), 0); update(ex, 0); } }
/** * Construct * * @param namespaceService namespace service * @param nodeService node service * @param contentHandler content handler */ ViewXMLExporter(NamespaceService namespaceService, NodeService nodeService, SearchService searchService, DictionaryService dictionaryService, PermissionService permissionService, ContentHandler contentHandler) { this.namespaceService = namespaceService; this.nodeService = nodeService; this.searchService = searchService; this.dictionaryService = dictionaryService; this.permissionService = permissionService; this.contentHandler = contentHandler; VIEW_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, VIEW_LOCALNAME, namespaceService); VALUE_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, VALUE_LOCALNAME, namespaceService); VALUES_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, VALUES_LOCALNAME, namespaceService); CHILDNAME_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, CHILDNAME_LOCALNAME, namespaceService); ASPECTS_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, ASPECTS_LOCALNAME, namespaceService); PROPERTIES_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, PROPERTIES_LOCALNAME, namespaceService); ASSOCIATIONS_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, ASSOCIATIONS_LOCALNAME, namespaceService); DATATYPE_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, DATATYPE_LOCALNAME, namespaceService); ISNULL_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, ISNULL_LOCALNAME, namespaceService); METADATA_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, METADATA_LOCALNAME, namespaceService); EXPORTEDBY_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, EXPORTEDBY_LOCALNAME, namespaceService); EXPORTEDDATE_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, EXPORTEDDATE_LOCALNAME, namespaceService); EXPORTERVERSION_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, EXPORTERVERSION_LOCALNAME, namespaceService); EXPORTOF_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, EXPORTOF_LOCALNAME, namespaceService); ACL_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, ACL_LOCALNAME, namespaceService); ACE_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, ACE_LOCALNAME, namespaceService); ACCESS_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, ACCESS_LOCALNAME, namespaceService); AUTHORITY_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, AUTHORITY_LOCALNAME, namespaceService); PERMISSION_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, PERMISSION_LOCALNAME, namespaceService); INHERITPERMISSIONS_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, INHERITPERMISSIONS_LOCALNAME, namespaceService); REFERENCE_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, REFERENCE_LOCALNAME, namespaceService); PATHREF_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, PATHREF_LOCALNAME, namespaceService); NODEREF_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, NODEREF_LOCALNAME, namespaceService); LOCALE_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, LOCALE_LOCALNAME, namespaceService); MLVALUE_QNAME = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, MLVALUE_LOCALNAME, namespaceService); }
/** * @since 2.0.2 */ public final void marshal(T object, ContentHandler contentHandler, AttachmentMarshaller am) throws JAXBException { Marshaller m = context.marshallerPool.take(); m.setAttachmentMarshaller(am); marshal(m,object,contentHandler); m.setAttachmentMarshaller(null); context.marshallerPool.recycle(m); }
/** * @deprecated This method will be removed in Apache Tika 1.0. */ public void parse(InputStream stream, ContentHandler handler, Metadata metadata) throws IOException, SAXException, TikaException { parse(stream, handler, metadata, new ParseContext()); }
/** * Pop, or undeclare all namespace definitions that are currently * declared at the given element depth, or deepter. * @param elemDepth the element depth for which mappings declared at this * depth or deeper will no longer be valid * @param saxHandler The ContentHandler to notify of any endPrefixMapping() * calls. This parameter can be null. */ void popNamespaces(int elemDepth, ContentHandler saxHandler) { while (true) { if (m_nodeStack.isEmpty()) return; MappingRecord map = (MappingRecord)(m_nodeStack.peek()); int depth = map.m_declarationDepth; if (depth < elemDepth) return; /* the depth of the declared mapping is elemDepth or deeper * so get rid of it */ map = (MappingRecord) m_nodeStack.pop(); final String prefix = map.m_prefix; popNamespace(prefix); if (saxHandler != null) { try { saxHandler.endPrefixMapping(prefix); } catch (SAXException e) { // not much we can do if they aren't willing to listen } } } }
@Override protected void toXml(ContentHandler contentHandler) throws SAXException { XMLUtils.addSaxString(contentHandler, "SRC", src); XMLUtils.addSaxString(contentHandler, "NSQUOTA", Long.toString(nsQuota)); XMLUtils.addSaxString(contentHandler, "DSQUOTA", Long.toString(dsQuota)); }
/** * Constructor. * @param contentHandler The implemention of the * contentHandler operation (toXMLString, digest, ...) */ public TreeWalker(ContentHandler contentHandler, DOMHelper dh) { this.m_contentHandler = contentHandler; m_contentHandler.setDocumentLocator(m_locator); try { // Bug see Bugzilla 26741 m_locator.setSystemId(SecuritySupport.getSystemProperty("user.dir") + File.separator + "dummy.xsl"); } catch (SecurityException se){// user.dir not accessible from applet } m_dh = dh; }
/** * Writes the end element event. */ public void writeEnd(ContentHandler contentHandler) throws SAXException{ contentHandler.endElement(fixNull(nsUri),localName,getQName()); for( int i=ns.length-2; i>=0; i-=2 ) { contentHandler.endPrefixMapping(fixNull(ns[i])); } }
public ToSAXHandler( ContentHandler hdlr, LexicalHandler lex, String encoding) { setContentHandler(hdlr); setLexHandler(lex); setEncoding(encoding); }
@Override protected void toXml(ContentHandler contentHandler) throws SAXException { XMLUtils.addSaxString(contentHandler, "LENGTH", Integer.toString(length)); XMLUtils.addSaxString(contentHandler, "INODEID", Long.toString(inodeId)); XMLUtils.addSaxString(contentHandler, "PATH", path); XMLUtils.addSaxString(contentHandler, "REPLICATION", Short.valueOf(replication).toString()); XMLUtils.addSaxString(contentHandler, "MTIME", Long.toString(mtime)); XMLUtils.addSaxString(contentHandler, "ATIME", Long.toString(atime)); XMLUtils.addSaxString(contentHandler, "BLOCKSIZE", Long.toString(blockSize)); XMLUtils.addSaxString(contentHandler, "CLIENT_NAME", clientName); XMLUtils.addSaxString(contentHandler, "CLIENT_MACHINE", clientMachine); XMLUtils.addSaxString(contentHandler, "OVERWRITE", Boolean.toString(overwrite)); for (Block b : blocks) { FSEditLogOp.blockToXml(contentHandler, b); } FSEditLogOp.permissionStatusToXml(contentHandler, permissions); if (this.opCode == OP_ADD) { if (aclEntries != null) { appendAclEntriesToXml(contentHandler, aclEntries); } appendRpcIdsToXml(contentHandler, rpcClientId, rpcCallId); } }
/** * @param outputStream The output * @return A content handler * @throws IOException When there's an XML error */ private ContentHandler createContentHandler(OutputStream outputStream) throws IOException { Properties outputProperties = OutputPropertiesFactory.getDefaultMethodProperties(Method.XML); outputProperties.setProperty("indent", "yes"); outputProperties.setProperty(OutputPropertiesFactory.S_KEY_INDENT_AMOUNT, "2"); outputProperties.setProperty(OutputPropertiesFactory.S_KEY_LINE_SEPARATOR, "\n"); Serializer serializer = SerializerFactory.getSerializer(outputProperties); serializer.setOutputStream(outputStream); return serializer.asContentHandler(); }
/** * Starts the event forking. */ public void startForking(String uri, String localName, String qName, Attributes atts, ContentHandler side) throws SAXException { if(this.side!=null) throw new IllegalStateException(); // can't fork to two handlers this.side = side; depth = 1; side.setDocumentLocator(loc); side.startDocument(); for( int i=0; i<namespaces.size(); i+=2 ) side.startPrefixMapping(namespaces.get(i),namespaces.get(i+1)); side.startElement(uri,localName,qName,atts); }
public void writeTo(ContentHandler h, ErrorHandler errorHandler) throws SAXException { String nsUri = av.nsUri; String ln = av.faultDetailTag.getLocalPart(); h.startPrefixMapping("",nsUri); h.startElement(nsUri,ln,ln,EMPTY_ATTS); h.startElement(nsUri,wrapper,wrapper,EMPTY_ATTS); h.characters(problemValue.toCharArray(),0,problemValue.length()); h.endElement(nsUri,wrapper,wrapper); h.endElement(nsUri,ln,ln); }
@Override protected void toXml(ContentHandler contentHandler) throws SAXException { XMLUtils.addSaxString(contentHandler, "LENGTH", Integer.toString(length)); XMLUtils.addSaxString(contentHandler, "PATH", path); XMLUtils.addSaxString(contentHandler, "MTIME", Long.toString(mtime)); XMLUtils.addSaxString(contentHandler, "ATIME", Long.toString(atime)); }
/** * Creates ForkContentHandlers so that the specified handlers * will receive SAX events in the order of the array. */ public static ContentHandler create( ContentHandler[] handlers ) { if(handlers.length==0) throw new IllegalArgumentException(); ContentHandler result = handlers[0]; for( int i=1; i<handlers.length; i++ ) result = new ForkContentHandler( result, handlers[i] ); return result; }
@Override public void generate(CacheXmlGenerator cacheXmlGenerator) throws SAXException { final ContentHandler handler = cacheXmlGenerator.getContentHandler(); try { handler.startPrefixMapping(PREFIX, NAMESPACE); final AttributesImpl atts = new AttributesImpl(); addAttribute(atts, ATTRIBUTE_VALUE, extension.getValue()); emptyElement(handler, PREFIX, ELEMENT_CACHE, atts); } finally { handler.endPrefixMapping("mock"); } }
/** * Collects the ns declarations and starts the prefix mapping, consequently the associated endPrefixMapping needs to be called. * @param contentHandler * @param attrs * @param excludePrefix , this is to excldue the global prefix mapping "S" used at the start * @throws SAXException */ private void startPrefixMapping(ContentHandler contentHandler, NamedNodeMap attrs, String excludePrefix) throws SAXException { if(attrs == null) return; for(int i=0; i < attrs.getLength();i++) { Attr a = (Attr)attrs.item(i); //check if attr is ns declaration if("xmlns".equals(a.getPrefix()) || "xmlns".equals(a.getLocalName())) { if(!fixNull(a.getPrefix()).equals(excludePrefix)) { contentHandler.startPrefixMapping(fixNull(a.getPrefix()), a.getNamespaceURI()); } } } }
public void setContentHandler(ContentHandler handler) throws NullPointerException { _sax = handler; if (handler instanceof LexicalHandler) { _lex = (LexicalHandler) handler; } if (handler instanceof SAXImpl) { _saxImpl = (SAXImpl)handler; } }
/** * Parses a subtree starting from the element e and * reports SAX2 events to the specified handler. * * @deprecated in JAXB 2.0 * Use {@link #scan(Element)} */ public void parse( Element e, ContentHandler handler ) throws SAXException { // it might be better to set receiver at the constructor. receiver = handler; setCurrentLocation( e ); receiver.startDocument(); receiver.setDocumentLocator(locator); visit(e); setCurrentLocation( e ); receiver.endDocument(); }
private static String parse(final InputStream input) throws TikaException, SAXException, IOException { final Parser parser = new PDFParser(); final ContentHandler handler = new BodyContentHandler(); final Metadata metadata = new Metadata(); final ParseContext parseContext = new ParseContext(); parser.parse(input, handler, metadata, parseContext); return handler.toString(); }
@Override protected void toXml(ContentHandler contentHandler) throws SAXException { XMLUtils.addSaxString(contentHandler, "LENGTH", Integer.toString(length)); XMLUtils.addSaxString(contentHandler, "SRC", src); XMLUtils.addSaxString(contentHandler, "DST", dst); XMLUtils.addSaxString(contentHandler, "TIMESTAMP", Long.toString(timestamp)); appendRpcIdsToXml(contentHandler, rpcClientId, rpcCallId); }
public ContentHandler getContentHandler() { return fContentHandler; }
/** * @see SerializationHandler#setContentHandler(org.xml.sax.ContentHandler) */ public void setContentHandler(ContentHandler ch) { aMethodIsCalled(); }
protected ContentHandler getContentHandler() { return h; }
public ToSAXHandler(ContentHandler handler, String encoding) { setContentHandler(handler); setEncoding(encoding); }
public void marshal(Marshaller _m, T t, ContentHandler contentHandler) throws JAXBException { MarshallerImpl m = (MarshallerImpl)_m; m.write(tagName,bi,t,new SAXOutput(contentHandler),null); }
public ContentHandler getContentHandler() { return this.handler; }