Search in sources :

Example 6 with XMLSerializer

use of org.apache.xml.serialize.XMLSerializer in project ats-framework by Axway.

the class AtsProjectConfiguration method save.

public void save() throws AtsConfigurationException {
    // save the XML file
    try {
        OutputFormat format = new OutputFormat(doc);
        format.setIndenting(true);
        format.setIndent(4);
        format.setLineWidth(1000);
        XMLSerializer serializer = new XMLSerializer(new FileOutputStream(new File(atsConfigurationFile)), format);
        serializer.serialize(doc);
    } catch (Exception e) {
        throw new AtsConfigurationException("Error saving ATS configuration in '" + atsConfigurationFile + "'", e);
    }
}
Also used : XMLSerializer(org.apache.xml.serialize.XMLSerializer) AtsConfigurationException(com.axway.ats.core.atsconfig.exceptions.AtsConfigurationException) FileOutputStream(java.io.FileOutputStream) OutputFormat(org.apache.xml.serialize.OutputFormat) File(java.io.File) AtsConfigurationException(com.axway.ats.core.atsconfig.exceptions.AtsConfigurationException)

Example 7 with XMLSerializer

use of org.apache.xml.serialize.XMLSerializer in project ats-framework by Axway.

the class LocalFileSystemSnapshot method toFile.

@Override
public void toFile(String backupFile) {
    log.info("SAVE TO FILE " + backupFile + " - START");
    // create the directory if does not exist
    File dirPath = new File(IoUtils.getFilePath(backupFile));
    if (!dirPath.exists()) {
        dirPath.mkdirs();
    }
    Document dom;
    try {
        dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    } catch (Exception e) {
        throw new FileSystemSnapshotException("Error creating DOM parser for " + backupFile, e);
    }
    // TODO - add DTD or schema for manual creation and easy validation
    Element fileSystemNode = dom.createElement(NODE_FILE_SYSTEM);
    fileSystemNode.setAttribute("name", this.name);
    fileSystemNode.setAttribute("time", SnapshotUtils.dateToString(this.snapshotTimestamp));
    dom.appendChild(fileSystemNode);
    for (Entry<String, DirectorySnapshot> dirSnapshotEntry : this.dirSnapshots.entrySet()) {
        Element dirSnapshotNode = dom.createElement(NODE_DIRECTORY);
        fileSystemNode.appendChild(dirSnapshotNode);
        dirSnapshotNode.setAttribute("alias", dirSnapshotEntry.getKey());
        dirSnapshotEntry.getValue().toFile(dom, dirSnapshotNode);
    }
    // save the XML file
    try {
        OutputFormat format = new OutputFormat(dom);
        format.setIndenting(true);
        format.setIndent(4);
        format.setLineWidth(1000);
        XMLSerializer serializer = new XMLSerializer(new FileOutputStream(new File(backupFile)), format);
        serializer.serialize(dom);
    } catch (Exception e) {
        throw new FileSystemSnapshotException("Error saving " + backupFile, e);
    }
    log.info("SAVE TO FILE " + backupFile + " - END");
}
Also used : XMLSerializer(org.apache.xml.serialize.XMLSerializer) Element(org.w3c.dom.Element) FileOutputStream(java.io.FileOutputStream) OutputFormat(org.apache.xml.serialize.OutputFormat) Document(org.w3c.dom.Document) FileSystemSnapshotException(com.axway.ats.common.filesystem.snapshot.FileSystemSnapshotException) File(java.io.File) FileSystemSnapshotException(com.axway.ats.common.filesystem.snapshot.FileSystemSnapshotException)

Example 8 with XMLSerializer

use of org.apache.xml.serialize.XMLSerializer in project lucene-solr by apache.

the class ExtractingDocumentLoader method load.

@Override
public void load(SolrQueryRequest req, SolrQueryResponse rsp, ContentStream stream, UpdateRequestProcessor processor) throws Exception {
    Parser parser = null;
    String streamType = req.getParams().get(ExtractingParams.STREAM_TYPE, null);
    if (streamType != null) {
        //Cache?  Parsers are lightweight to construct and thread-safe, so I'm told
        MediaType mt = MediaType.parse(streamType.trim().toLowerCase(Locale.ROOT));
        parser = new DefaultParser(config.getMediaTypeRegistry()).getParsers().get(mt);
    } else {
        parser = autoDetectParser;
    }
    if (parser != null) {
        Metadata metadata = new Metadata();
        // If you specify the resource name (the filename, roughly) with this parameter,
        // then Tika can make use of it in guessing the appropriate MIME type:
        String resourceName = req.getParams().get(ExtractingParams.RESOURCE_NAME, null);
        if (resourceName != null) {
            metadata.add(TikaMetadataKeys.RESOURCE_NAME_KEY, resourceName);
        }
        // Provide stream's content type as hint for auto detection
        if (stream.getContentType() != null) {
            metadata.add(HttpHeaders.CONTENT_TYPE, stream.getContentType());
        }
        InputStream inputStream = null;
        try {
            inputStream = stream.getStream();
            metadata.add(ExtractingMetadataConstants.STREAM_NAME, stream.getName());
            metadata.add(ExtractingMetadataConstants.STREAM_SOURCE_INFO, stream.getSourceInfo());
            metadata.add(ExtractingMetadataConstants.STREAM_SIZE, String.valueOf(stream.getSize()));
            metadata.add(ExtractingMetadataConstants.STREAM_CONTENT_TYPE, stream.getContentType());
            // HtmlParser and TXTParser regard Metadata.CONTENT_ENCODING in metadata
            String charset = ContentStreamBase.getCharsetFromContentType(stream.getContentType());
            if (charset != null) {
                metadata.add(HttpHeaders.CONTENT_ENCODING, charset);
            }
            String xpathExpr = params.get(ExtractingParams.XPATH_EXPRESSION);
            boolean extractOnly = params.getBool(ExtractingParams.EXTRACT_ONLY, false);
            SolrContentHandler handler = factory.createSolrContentHandler(metadata, params, req.getSchema());
            ContentHandler parsingHandler = handler;
            StringWriter writer = null;
            BaseMarkupSerializer serializer = null;
            if (extractOnly == true) {
                String extractFormat = params.get(ExtractingParams.EXTRACT_FORMAT, "xml");
                writer = new StringWriter();
                if (extractFormat.equals(TEXT_FORMAT)) {
                    serializer = new TextSerializer();
                    serializer.setOutputCharStream(writer);
                    serializer.setOutputFormat(new OutputFormat("Text", "UTF-8", true));
                } else {
                    serializer = new XMLSerializer(writer, new OutputFormat("XML", "UTF-8", true));
                }
                if (xpathExpr != null) {
                    Matcher matcher = PARSER.parse(xpathExpr);
                    //The MatchingContentHandler does not invoke startDocument.  See http://tika.markmail.org/message/kknu3hw7argwiqin
                    serializer.startDocument();
                    parsingHandler = new MatchingContentHandler(serializer, matcher);
                } else {
                    parsingHandler = serializer;
                }
            } else if (xpathExpr != null) {
                Matcher matcher = PARSER.parse(xpathExpr);
                parsingHandler = new MatchingContentHandler(handler, matcher);
            }
            try {
                //potentially use a wrapper handler for parsing, but we still need the SolrContentHandler for getting the document.
                ParseContext context = parseContextConfig.create();
                context.set(Parser.class, parser);
                context.set(HtmlMapper.class, MostlyPassthroughHtmlMapper.INSTANCE);
                // Password handling
                RegexRulesPasswordProvider epp = new RegexRulesPasswordProvider();
                String pwMapFile = params.get(ExtractingParams.PASSWORD_MAP_FILE);
                if (pwMapFile != null && pwMapFile.length() > 0) {
                    InputStream is = req.getCore().getResourceLoader().openResource(pwMapFile);
                    if (is != null) {
                        log.debug("Password file supplied: " + pwMapFile);
                        epp.parse(is);
                    }
                }
                context.set(PasswordProvider.class, epp);
                String resourcePassword = params.get(ExtractingParams.RESOURCE_PASSWORD);
                if (resourcePassword != null) {
                    epp.setExplicitPassword(resourcePassword);
                    log.debug("Literal password supplied for file " + resourceName);
                }
                parser.parse(inputStream, parsingHandler, metadata, context);
            } catch (TikaException e) {
                if (ignoreTikaException)
                    log.warn(new StringBuilder("skip extracting text due to ").append(e.getLocalizedMessage()).append(". metadata=").append(metadata.toString()).toString());
                else
                    throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e);
            }
            if (extractOnly == false) {
                addDoc(handler);
            } else {
                //serializer is not null, so we need to call endDoc on it if using xpath
                if (xpathExpr != null) {
                    serializer.endDocument();
                }
                rsp.add(stream.getName(), writer.toString());
                writer.close();
                String[] names = metadata.names();
                NamedList metadataNL = new NamedList();
                for (int i = 0; i < names.length; i++) {
                    String[] vals = metadata.getValues(names[i]);
                    metadataNL.add(names[i], vals);
                }
                rsp.add(stream.getName() + "_metadata", metadataNL);
            }
        } catch (SAXException e) {
            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e);
        } finally {
            IOUtils.closeQuietly(inputStream);
        }
    } else {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Stream type of " + streamType + " didn't match any known parsers.  Please supply the " + ExtractingParams.STREAM_TYPE + " parameter.");
    }
}
Also used : Matcher(org.apache.tika.sax.xpath.Matcher) MatchingContentHandler(org.apache.tika.sax.xpath.MatchingContentHandler) Metadata(org.apache.tika.metadata.Metadata) MatchingContentHandler(org.apache.tika.sax.xpath.MatchingContentHandler) ContentHandler(org.xml.sax.ContentHandler) XHTMLContentHandler(org.apache.tika.sax.XHTMLContentHandler) SAXException(org.xml.sax.SAXException) StringWriter(java.io.StringWriter) MediaType(org.apache.tika.mime.MediaType) SolrException(org.apache.solr.common.SolrException) DefaultParser(org.apache.tika.parser.DefaultParser) XMLSerializer(org.apache.xml.serialize.XMLSerializer) TikaException(org.apache.tika.exception.TikaException) InputStream(java.io.InputStream) NamedList(org.apache.solr.common.util.NamedList) BaseMarkupSerializer(org.apache.xml.serialize.BaseMarkupSerializer) OutputFormat(org.apache.xml.serialize.OutputFormat) Parser(org.apache.tika.parser.Parser) XPathParser(org.apache.tika.sax.xpath.XPathParser) AutoDetectParser(org.apache.tika.parser.AutoDetectParser) DefaultParser(org.apache.tika.parser.DefaultParser) TextSerializer(org.apache.xml.serialize.TextSerializer) ParseContext(org.apache.tika.parser.ParseContext)

Example 9 with XMLSerializer

use of org.apache.xml.serialize.XMLSerializer in project jersey by jersey.

the class ResourceDoclet method start.

/**
     * Start the doclet.
     *
     * @param root the root JavaDoc document.
     * @return true if no exception is thrown.
     */
public static boolean start(final RootDoc root) {
    final String output = getOptionArg(root.options(), OPTION_OUTPUT);
    final String classpath = getOptionArg(root.options(), OPTION_CLASSPATH);
    // LOG.info( "Have classpath: " + classpath );
    final String[] classpathElements = classpath.split(File.pathSeparator);
    final ClassLoader cl = Thread.currentThread().getContextClassLoader();
    final ClassLoader ncl = new Loader(classpathElements, ResourceDoclet.class.getClassLoader());
    Thread.currentThread().setContextClassLoader(ncl);
    final String docProcessorOption = getOptionArg(root.options(), OPTION_DOC_PROCESSORS);
    final String[] docProcessors = docProcessorOption != null ? docProcessorOption.split(":") : null;
    final DocProcessorWrapper docProcessor = new DocProcessorWrapper();
    try {
        if (docProcessors != null && docProcessors.length > 0) {
            final Class<?> clazz = Class.forName(docProcessors[0], true, Thread.currentThread().getContextClassLoader());
            final Class<? extends DocProcessor> dpClazz = clazz.asSubclass(DocProcessor.class);
            docProcessor.add(dpClazz.newInstance());
        }
    } catch (final Exception e) {
        LOG.log(Level.SEVERE, "Could not load docProcessors " + docProcessorOption, e);
    }
    try {
        final ResourceDocType result = new ResourceDocType();
        final ClassDoc[] classes = root.classes();
        for (final ClassDoc classDoc : classes) {
            LOG.fine("Writing class " + classDoc.qualifiedTypeName());
            final ClassDocType classDocType = new ClassDocType();
            classDocType.setClassName(classDoc.qualifiedTypeName());
            classDocType.setCommentText(classDoc.commentText());
            docProcessor.processClassDoc(classDoc, classDocType);
            for (final MethodDoc methodDoc : classDoc.methods()) {
                final MethodDocType methodDocType = new MethodDocType();
                methodDocType.setMethodName(methodDoc.name());
                methodDocType.setMethodSignature(methodDoc.signature());
                methodDocType.setCommentText(methodDoc.commentText());
                docProcessor.processMethodDoc(methodDoc, methodDocType);
                addParamDocs(methodDoc, methodDocType, docProcessor);
                addRequestRepresentationDoc(methodDoc, methodDocType);
                addResponseDoc(methodDoc, methodDocType);
                classDocType.getMethodDocs().add(methodDocType);
            }
            result.getDocs().add(classDocType);
        }
        try {
            final Class<?>[] clazzes = getJAXBContextClasses(result, docProcessor);
            final JAXBContext c = JAXBContext.newInstance(clazzes);
            final Marshaller m = c.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            final OutputStream out = new BufferedOutputStream(new FileOutputStream(output));
            final String[] cdataElements = getCDataElements(docProcessor);
            final XMLSerializer serializer = getXMLSerializer(out, cdataElements);
            m.marshal(result, serializer);
            out.close();
            LOG.info("Wrote " + output);
        } catch (final Exception e) {
            LOG.log(Level.SEVERE, "Could not serialize ResourceDoc.", e);
            return false;
        }
    } finally {
        Thread.currentThread().setContextClassLoader(cl);
    }
    return true;
}
Also used : Marshaller(javax.xml.bind.Marshaller) XMLSerializer(org.apache.xml.serialize.XMLSerializer) ClassDocType(org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.ClassDocType) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) URLClassLoader(java.net.URLClassLoader) JAXBContext(javax.xml.bind.JAXBContext) MalformedURLException(java.net.MalformedURLException) MethodDocType(org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.MethodDocType) MethodDoc(com.sun.javadoc.MethodDoc) FileOutputStream(java.io.FileOutputStream) URLClassLoader(java.net.URLClassLoader) ResourceDocType(org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.ResourceDocType) BufferedOutputStream(java.io.BufferedOutputStream) ClassDoc(com.sun.javadoc.ClassDoc)

Example 10 with XMLSerializer

use of org.apache.xml.serialize.XMLSerializer in project ats-framework by Axway.

the class DatabaseSnapshotBackupUtils method saveToFile.

/**
     * Save a snapshot into a file
     * @param snapshot the snapshot to save
     * @param backupFile the backup file name
     * @return the XML document
     */
public Document saveToFile(DatabaseSnapshot snapshot, String backupFile) {
    log.info("Save database snapshot into file " + backupFile + " - START");
    // create the directory if does not exist
    File dirPath = new File(IoUtils.getFilePath(backupFile));
    if (!dirPath.exists()) {
        dirPath.mkdirs();
    }
    Document doc;
    try {
        doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    } catch (Exception e) {
        throw new DatabaseSnapshotException("Error creating DOM parser for " + backupFile, e);
    }
    // TODO - add DTD or schema for manual creation and easy validation
    Element dbNode = doc.createElement(DatabaseSnapshotUtils.NODE_DB_SNAPSHOT);
    dbNode.setAttribute(DatabaseSnapshotUtils.ATTR_SNAPSHOT_NAME, snapshot.name);
    // this timestamp comes when user takes a snapshot from database
    dbNode.setAttribute(DatabaseSnapshotUtils.ATTR_METADATA_TIME, DatabaseSnapshotUtils.dateToString(snapshot.metadataTimestamp));
    // this timestamp now
    snapshot.contentTimestamp = System.currentTimeMillis();
    dbNode.setAttribute(DatabaseSnapshotUtils.ATTR_CONTENT_TIME, DatabaseSnapshotUtils.dateToString(snapshot.contentTimestamp));
    doc.appendChild(dbNode);
    // append table descriptions
    for (TableDescription tableDescription : snapshot.tables) {
        Element tableNode = doc.createElement(DatabaseSnapshotUtils.NODE_TABLE);
        dbNode.appendChild(tableNode);
        tableDescription.toXmlNode(doc, tableNode);
        // if the current table is in the skipTableContent we will do not need to get its all data
        if (snapshot.skipTableContent.contains(tableDescription.getName())) {
            // no rows have to be saved, so we skip this iteration
            continue;
        }
        List<String> valuesList = new ArrayList<String>();
        valuesList.addAll(snapshot.loadTableData(snapshot.name, tableDescription, snapshot.skipRulesPerTable, snapshot.skipRows, null, null));
        for (String values : valuesList) {
            Element rowNode = doc.createElement(DatabaseSnapshotUtils.NODE_ROW);
            if (!skipRow(snapshot.skipRows.get(tableDescription.getName()), values)) {
                rowNode.setTextContent(values);
            }
            tableNode.appendChild(rowNode);
        }
    }
    // append skip rules
    for (String tableName : snapshot.skipRulesPerTable.keySet()) {
        Element skipRuleNode = doc.createElement(DatabaseSnapshotUtils.NODE_SKIP_RULE);
        dbNode.appendChild(skipRuleNode);
        skipRuleNode.setAttribute(DatabaseSnapshotUtils.ATT_SKIP_RULE_TABLE, tableName);
        snapshot.skipRulesPerTable.get(tableName).toXmlNode(doc, skipRuleNode);
    }
    // save the XML file
    try {
        OutputFormat format = new OutputFormat(doc);
        format.setIndenting(true);
        format.setIndent(4);
        format.setLineWidth(1000);
        XMLSerializer serializer = new XMLSerializer(new FileOutputStream(new File(backupFile)), format);
        serializer.serialize(doc);
    } catch (Exception e) {
        throw new DatabaseSnapshotException("Error saving " + backupFile, e);
    }
    log.info("Save database snapshot into file " + backupFile + " - END");
    return doc;
}
Also used : XMLSerializer(org.apache.xml.serialize.XMLSerializer) Element(org.w3c.dom.Element) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) OutputFormat(org.apache.xml.serialize.OutputFormat) Document(org.w3c.dom.Document) File(java.io.File) TableDescription(com.axway.ats.common.dbaccess.snapshot.TableDescription) DatabaseSnapshotException(com.axway.ats.common.dbaccess.snapshot.DatabaseSnapshotException) DatabaseSnapshotException(com.axway.ats.common.dbaccess.snapshot.DatabaseSnapshotException)

Aggregations

XMLSerializer (org.apache.xml.serialize.XMLSerializer)10 OutputFormat (org.apache.xml.serialize.OutputFormat)9 FileOutputStream (java.io.FileOutputStream)5 Document (org.w3c.dom.Document)4 File (java.io.File)3 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 Element (org.w3c.dom.Element)2 DatabaseSnapshotException (com.axway.ats.common.dbaccess.snapshot.DatabaseSnapshotException)1 TableDescription (com.axway.ats.common.dbaccess.snapshot.TableDescription)1 FileSystemSnapshotException (com.axway.ats.common.filesystem.snapshot.FileSystemSnapshotException)1 AtsConfigurationException (com.axway.ats.core.atsconfig.exceptions.AtsConfigurationException)1 ClassDoc (com.sun.javadoc.ClassDoc)1 MethodDoc (com.sun.javadoc.MethodDoc)1 BufferedOutputStream (java.io.BufferedOutputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 PrintWriter (java.io.PrintWriter)1 StringReader (java.io.StringReader)1