Search in sources :

Example 1 with SystemIdResolver

use of org.apache.solr.util.SystemIdResolver in project lucene-solr by apache.

the class TransformerProvider method getTemplates.

/** Return a Templates object for the given filename */
private Templates getTemplates(ResourceLoader loader, String filename, int cacheLifetimeSeconds) throws IOException {
    Templates result = null;
    lastFilename = null;
    try {
        if (log.isDebugEnabled()) {
            log.debug("compiling XSLT templates:" + filename);
        }
        final String fn = "xslt/" + filename;
        final TransformerFactory tFactory = TransformerFactory.newInstance();
        tFactory.setURIResolver(new SystemIdResolver(loader).asURIResolver());
        tFactory.setErrorListener(xmllog);
        final StreamSource src = new StreamSource(loader.openResource(fn), SystemIdResolver.createSystemIdFromResourceName(fn));
        try {
            result = tFactory.newTemplates(src);
        } finally {
            // some XML parsers are broken and don't close the byte stream (but they should according to spec)
            IOUtils.closeQuietly(src.getInputStream());
        }
    } catch (Exception e) {
        log.error(getClass().getName(), "newTemplates", e);
        throw new IOException("Unable to initialize Templates '" + filename + "'", e);
    }
    lastFilename = filename;
    lastTemplates = result;
    cacheExpiresTimeout = new TimeOut(cacheLifetimeSeconds, TimeUnit.SECONDS);
    return result;
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) SystemIdResolver(org.apache.solr.util.SystemIdResolver) TimeOut(org.apache.solr.util.TimeOut) StreamSource(javax.xml.transform.stream.StreamSource) Templates(javax.xml.transform.Templates) IOException(java.io.IOException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException)

Example 2 with SystemIdResolver

use of org.apache.solr.util.SystemIdResolver in project lucene-solr by apache.

the class XPathEntityProcessor method initXpathReader.

private void initXpathReader(VariableResolver resolver) {
    reinitXPathReader = false;
    useSolrAddXml = Boolean.parseBoolean(context.getEntityAttribute(USE_SOLR_ADD_SCHEMA));
    streamRows = Boolean.parseBoolean(context.getEntityAttribute(STREAM));
    if (context.getResolvedEntityAttribute("batchSize") != null) {
        blockingQueueSize = Integer.parseInt(context.getEntityAttribute("batchSize"));
    }
    if (context.getResolvedEntityAttribute("readTimeOut") != null) {
        blockingQueueTimeOut = Integer.parseInt(context.getEntityAttribute("readTimeOut"));
    }
    String xslt = context.getEntityAttribute(XSL);
    if (xslt != null) {
        xslt = context.replaceTokens(xslt);
        try {
            // create an instance of TransformerFactory
            TransformerFactory transFact = TransformerFactory.newInstance();
            final SolrCore core = context.getSolrCore();
            final StreamSource xsltSource;
            if (core != null) {
                final ResourceLoader loader = core.getResourceLoader();
                transFact.setURIResolver(new SystemIdResolver(loader).asURIResolver());
                xsltSource = new StreamSource(loader.openResource(xslt), SystemIdResolver.createSystemIdFromResourceName(xslt));
            } else {
                // fallback for tests
                xsltSource = new StreamSource(xslt);
            }
            transFact.setErrorListener(xmllog);
            try {
                xslTransformer = transFact.newTransformer(xsltSource);
            } finally {
                // some XML parsers are broken and don't close the byte stream (but they should according to spec)
                IOUtils.closeQuietly(xsltSource.getInputStream());
            }
            LOG.info("Using xslTransformer: " + xslTransformer.getClass().getName());
        } catch (Exception e) {
            throw new DataImportHandlerException(SEVERE, "Error initializing XSL ", e);
        }
    }
    if (useSolrAddXml) {
        // Support solr add documents
        xpathReader = new XPathRecordReader("/add/doc");
        xpathReader.addField("name", "/add/doc/field/@name", true);
        xpathReader.addField("value", "/add/doc/field", true);
    } else {
        String forEachXpath = context.getResolvedEntityAttribute(FOR_EACH);
        if (forEachXpath == null)
            throw new DataImportHandlerException(SEVERE, "Entity : " + context.getEntityAttribute("name") + " must have a 'forEach' attribute");
        if (forEachXpath.equals(context.getEntityAttribute(FOR_EACH)))
            reinitXPathReader = true;
        try {
            xpathReader = new XPathRecordReader(forEachXpath);
            for (Map<String, String> field : context.getAllEntityFields()) {
                if (field.get(XPATH) == null)
                    continue;
                int flags = 0;
                if ("true".equals(field.get("flatten"))) {
                    flags = XPathRecordReader.FLATTEN;
                }
                String xpath = field.get(XPATH);
                xpath = context.replaceTokens(xpath);
                //for each xml
                if (!xpath.equals(field.get(XPATH)) && !context.isRootEntity())
                    reinitXPathReader = true;
                xpathReader.addField(field.get(DataImporter.COLUMN), xpath, Boolean.parseBoolean(field.get(DataImporter.MULTI_VALUED)), flags);
            }
        } catch (RuntimeException e) {
            throw new DataImportHandlerException(SEVERE, "Exception while reading xpaths for fields", e);
        }
    }
    String url = context.getEntityAttribute(URL);
    List<String> l = url == null ? Collections.EMPTY_LIST : resolver.getVariables(url);
    for (String s : l) {
        if (s.startsWith(entityName + ".")) {
            if (placeHolderVariables == null)
                placeHolderVariables = new ArrayList<>();
            placeHolderVariables.add(s.substring(entityName.length() + 1));
        }
    }
    for (Map<String, String> fld : context.getAllEntityFields()) {
        if (fld.get(COMMON_FIELD) != null && "true".equals(fld.get(COMMON_FIELD))) {
            if (commonFields == null)
                commonFields = new ArrayList<>();
            commonFields.add(fld.get(DataImporter.COLUMN));
        }
    }
}
Also used : ResourceLoader(org.apache.lucene.analysis.util.ResourceLoader) TransformerFactory(javax.xml.transform.TransformerFactory) SolrCore(org.apache.solr.core.SolrCore) StreamSource(javax.xml.transform.stream.StreamSource) TransformerException(javax.xml.transform.TransformerException) SystemIdResolver(org.apache.solr.util.SystemIdResolver)

Example 3 with SystemIdResolver

use of org.apache.solr.util.SystemIdResolver in project lucene-solr by apache.

the class DataImporter method loadDataConfig.

public DIHConfiguration loadDataConfig(InputSource configFile) {
    DIHConfiguration dihcfg = null;
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        // only enable xinclude, if a a SolrCore and SystemId is present (makes no sense otherwise)
        if (core != null && configFile.getSystemId() != null) {
            try {
                dbf.setXIncludeAware(true);
                dbf.setNamespaceAware(true);
            } catch (UnsupportedOperationException e) {
                LOG.warn("XML parser doesn't support XInclude option");
            }
        }
        DocumentBuilder builder = dbf.newDocumentBuilder();
        if (core != null)
            builder.setEntityResolver(new SystemIdResolver(core.getResourceLoader()));
        builder.setErrorHandler(XMLLOG);
        Document document;
        try {
            document = builder.parse(configFile);
        } finally {
            // some XML parsers are broken and don't close the byte stream (but they should according to spec)
            IOUtils.closeQuietly(configFile.getByteStream());
        }
        dihcfg = readFromXml(document);
        LOG.info("Data Configuration loaded successfully");
    } catch (Exception e) {
        throw new DataImportHandlerException(SEVERE, "Data Config problem: " + e.getMessage(), e);
    }
    for (Entity e : dihcfg.getEntities()) {
        if (e.getAllAttributes().containsKey(SqlEntityProcessor.DELTA_QUERY)) {
            isDeltaImportSupported = true;
            break;
        }
    }
    return dihcfg;
}
Also used : DIHConfiguration(org.apache.solr.handler.dataimport.config.DIHConfiguration) Entity(org.apache.solr.handler.dataimport.config.Entity) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SystemIdResolver(org.apache.solr.util.SystemIdResolver) Document(org.w3c.dom.Document) SolrException(org.apache.solr.common.SolrException) IOException(java.io.IOException)

Aggregations

SystemIdResolver (org.apache.solr.util.SystemIdResolver)3 IOException (java.io.IOException)2 TransformerFactory (javax.xml.transform.TransformerFactory)2 StreamSource (javax.xml.transform.stream.StreamSource)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 Templates (javax.xml.transform.Templates)1 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)1 TransformerException (javax.xml.transform.TransformerException)1 ResourceLoader (org.apache.lucene.analysis.util.ResourceLoader)1 SolrException (org.apache.solr.common.SolrException)1 SolrCore (org.apache.solr.core.SolrCore)1 DIHConfiguration (org.apache.solr.handler.dataimport.config.DIHConfiguration)1 Entity (org.apache.solr.handler.dataimport.config.Entity)1 TimeOut (org.apache.solr.util.TimeOut)1 Document (org.w3c.dom.Document)1