Example usage for javax.xml.parsers SAXParserFactory setNamespaceAware

List of usage examples for javax.xml.parsers SAXParserFactory setNamespaceAware

Introduction

In this page you can find the example usage for javax.xml.parsers SAXParserFactory setNamespaceAware.

Prototype


public void setNamespaceAware(boolean awareness) 

Source Link

Document

Specifies that the parser produced by this code will provide support for XML namespaces.

Usage

From source file:com.seajas.search.contender.service.modifier.ArchiveModifierService.java

/**
 * Handle the given archive's content by sending each entry to the given handler.
 * /*w ww. ja  va 2  s  .c o m*/
 * @param archive
 * @param handler
 * @throws Exception
 */
public void handleArchive(final Archive archive, final ArchiveResultHandler handler) throws Exception {
    // Create a validating SAX parser

    final SAXParserFactory parserFactory = SAXParserFactory.newInstance();

    parserFactory.setValidating(true);
    parserFactory.setNamespaceAware(true);

    // Create a common transformer per thread

    Transformer transformer = null;

    try {
        transformer = transformerCache.getTransformer(archive.getId(), "archive",
                archive.getModificationDate());

        if (transformer == null)
            transformer = transformerCache.putContent(archive.getId(), "archive", archive.getModificationDate(),
                    archive.getTransformerContent());
    } catch (TransformerConfigurationException e) {
        logger.error("Unable to generate a (cached) transformer from the given content", e);

        return;
    } catch (TransformerFactoryConfigurationError e) {
        logger.error("Unable to generate a (cached) transformer from the given content", e);

        return;
    }

    // Store and process the files

    try {
        Map<File, String> storedResults = storeAndDecompressFiles(archive);

        List<String> deletedLinks = new ArrayList<String>();

        // Only process deletes when a deletion expression has been provided

        if (StringUtils.hasText(archive.getDeletionExpression())) {
            // Process all entries beforehand, so to exclude deletes from the final result

            for (Map.Entry<File, String> storedResult : storedResults.entrySet()) {
                File storedResultsFolder = storedResult.getKey();

                for (File entryLocation : storedResultsFolder.listFiles())
                    if (entryLocation.getName().matches(archive.getDeletionExpression())) {
                        String deleteLink = entryLocation.getName().replaceAll(archive.getDeletionExpression(),
                                archive.getInternalLink());

                        deletedLinks.add(deleteLink);

                        // Delete the actual link

                        cacheService
                                .addDeleted(new CacheService.DeletedEntry(archive.getCollection(), deleteLink));
                    }
            }
        }

        // Now process the stored results themselves

        for (Map.Entry<File, String> storedResult : storedResults.entrySet()) {
            File storedResultsFolder = storedResult.getKey();

            // Create the descriptions folder if it doesn't already exist

            File descriptionsFolderLocation = new File(storedResultsFolder, "descriptions");

            if (!descriptionsFolderLocation.exists())
                descriptionsFolderLocation.mkdirs();

            for (File entryLocation : storedResultsFolder.listFiles()) {
                if (entryLocation.isDirectory()) {
                    if (!entryLocation.getName().equals("descriptions"))
                        logger.warn("Unknown folder '" + entryLocation.getName()
                                + "'found in decompressed archive folder '"
                                + storedResultsFolder.getAbsolutePath() + "'");

                    continue;
                } else if (StringUtils.hasText(archive.getDeletionExpression())
                        && entryLocation.getName().matches(archive.getDeletionExpression()))
                    continue;

                InputStream transformationInputStream = null;

                try {
                    transformationInputStream = new BufferedInputStream(new FileInputStream(entryLocation));

                    // Now determine the content type and create a reader in case of structured content

                    MediaType entryMediaType = autoDetectParser.getDetector().detect(transformationInputStream,
                            new Metadata());

                    if (!(entryMediaType.getSubtype().equals("xml")
                            || entryMediaType.getSubtype().endsWith("+xml"))) {
                        logger.warn("Archive entry " + entryLocation.getAbsolutePath() + " contains "
                                + entryMediaType + " data which is unstructured, ignoring");

                        continue;
                    }
                } catch (IOException e) {
                    logger.error("Could not close input stream during archive processing", e);

                    if (transformationInputStream != null)
                        transformationInputStream.close();

                    continue;
                }

                // Process it as (semi-)structured content

                XmlReader transformationReader = new XmlReader(transformationInputStream, true);
                StringWriter transformationWriter = new StringWriter();

                try {
                    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF8");

                    // Use a SAX reader for entity resolution

                    XMLReader xmlReader = parserFactory.newSAXParser().getXMLReader();
                    InputSource inputSource = new InputSource(transformationReader);

                    xmlReader.setEntityResolver(transformerCache.getEntityResolver());
                    inputSource.setSystemId("file://" + transformerCache.getDtdImportPath() + "/template.xsl");

                    // Perform the actual transformation

                    transformer.setParameter("substituteUrl", archive.getInternalLink());
                    transformer.transform(new SAXSource(xmlReader, inputSource),
                            new StreamResult(transformationWriter));
                } catch (TransformerException e) {
                    logger.error("Unable to perform content transformation for entry "
                            + entryLocation.getAbsolutePath());

                    continue;
                } catch (SAXException e) {
                    logger.error("Unable to perform content transformation for entry "
                            + entryLocation.getAbsolutePath());

                    continue;
                } catch (ParserConfigurationException e) {
                    logger.error("Unable to perform content transformation for entry "
                            + entryLocation.getAbsolutePath());

                    continue;
                } finally {
                    transformationInputStream.close();
                    transformationReader.close();
                }

                // Create a syndication feed from the given result

                String resultContent = transformationWriter.toString();

                SyndFeed resultFeed = null;

                try {
                    SyndFeedInput feedInput = new SyndFeedInput();

                    resultFeed = feedInput.build(new StringReader(resultContent));
                } catch (FeedException e) {
                    logger.error("Could not parse the feed resulting from the archive entry transformation");

                    continue;
                } finally {
                    transformationWriter.close();
                }

                // Write the <description> content to a separate file and add it as an <enclosure>

                if (resultFeed.getEntries().size() > 0) {
                    Integer entryNumber = 0;

                    for (SyndEntry feedEntry : (Collection<SyndEntry>) resultFeed.getEntries()) {
                        if (!deletedLinks.contains(feedEntry.getLink())) {
                            String description = feedEntry.getDescription().getValue().trim();

                            File descriptionLocation = new File(descriptionsFolderLocation,
                                    stripExtension(entryLocation.getName()) + "-" + entryNumber++
                                            + (feedEntry.getDescription().getType().equals("text/html")
                                                    ? ".html"
                                                    : ".xml"));

                            Writer descriptionWriter = new OutputStreamWriter(
                                    new FileOutputStream(descriptionLocation), "UTF-8");

                            if (!description.endsWith("</html>"))
                                descriptionWriter.write("<html>\n<head>\n\t<title>" + feedEntry.getTitle()
                                        + "</title>\n</head>\n<body>\n");
                            descriptionWriter.write(description);
                            if (!description.endsWith("</html>"))
                                descriptionWriter.write("\n</body>\n</html>");

                            descriptionWriter.flush();
                            descriptionWriter.close();

                            // Remove the link from the processed cache should it already be in there, taking care of updates

                            cacheService.deleteElement(feedEntry.getLink());

                            // Then offer it up to the handler

                            if (logger.isDebugEnabled())
                                logger.debug("Adding result content (" + entryNumber
                                        + ") for archive entry with path " + entryLocation.getAbsolutePath());

                            try {
                                // NOTE: The encoding of 'UTF-8' is implied for archive-related files

                                handler.process(new URI(feedEntry.getLink()), archive.getHostname(), feedEntry,
                                        resultFeed);
                            } catch (FeedException e) {
                                logger.error(String.format(
                                        "Could not offer feed entry with link '%s' - invalid entry",
                                        feedEntry.getLink()), e);
                            } catch (URISyntaxException e) {
                                logger.error(String.format(
                                        "Could not offer feed entry with link '%s' - invalid link",
                                        feedEntry.getLink()), e);
                            }
                        } else
                            logger.info("Skipping over feed entry with link '" + feedEntry.getLink()
                                    + "' - marked for deletion");
                    }
                } else if (logger.isDebugEnabled())
                    logger.debug("No entries were found in archive entry with path "
                            + entryLocation.getAbsolutePath());
            }

            logger.info("Finished processing archive with name " + storedResult.getValue());

            // Now archive the entry in the cache

            cacheService.addArchived(storedResult.getValue());
        }

        logger.info("Finishing archive populator thread");
    } catch (IOException e) {
        logger.error("Could not close input stream during archive processing", e);
    }
}

From source file:it.unimi.di.big.mg4j.document.WikipediaDocumentSequence.java

@Override
public DocumentIterator iterator() throws IOException {
    final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    saxParserFactory.setNamespaceAware(true);
    final MutableString nameSpaceAccumulator = new MutableString();
    final ObjectOpenHashSet<MutableString> nameSpacesAccumulator = new ObjectOpenHashSet<MutableString>();
    final ArrayBlockingQueue<DocumentFactory> freeFactories = new ArrayBlockingQueue<DocumentFactory>(16);
    for (int i = freeFactories.remainingCapacity(); i-- != 0;)
        freeFactories.add(this.factory.copy());
    final ArrayBlockingQueue<DocumentAndFactory> readyDocumentsAndFactories = new ArrayBlockingQueue<DocumentAndFactory>(
            freeFactories.size());/*from w w  w .j  a v a2  s  .c  om*/

    final SAXParser parser;
    try {
        parser = saxParserFactory.newSAXParser();
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    final DefaultHandler handler = new DefaultHandler() {
        private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        private boolean inText;
        private boolean inTitle;
        private boolean inId;
        private boolean inTimestamp;
        private boolean inNamespaceDef;
        private boolean redirect;
        private MutableString text = new MutableString();
        private MutableString title = new MutableString();
        private MutableString id = new MutableString();
        private MutableString timestamp = new MutableString();
        private final Reference2ObjectMap<Enum<?>, Object> metadata = new Reference2ObjectOpenHashMap<Enum<?>, Object>();
        {
            metadata.put(PropertyBasedDocumentFactory.MetadataKeys.ENCODING, "UTF-8");
            metadata.put(MetadataKeys.REDIRECT, redirectAnchors);
        }

        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes)
                throws SAXException {
            if ("page".equals(localName)) {
                redirect = inText = inTitle = inId = inTimestamp = false;
                text.length(0);
                title.length(0);
                id.length(0);
                timestamp.length(0);
            } else if ("text".equals(localName))
                inText = true;
            else if ("title".equals(localName) && title.length() == 0)
                inTitle = true; // We catch only the first id/title elements.
            else if ("id".equals(localName) && id.length() == 0)
                inId = true;
            else if ("timestamp".equals(localName) && timestamp.length() == 0)
                inTimestamp = true;
            else if ("redirect".equals(localName)) {
                redirect = true;
                if (attributes.getValue("title") != null)
                    // Accumulate the title of the page as virtual text of the redirect page.
                    synchronized (redirectAnchors) {
                        final String link = Encoder.encodeTitleToUrl(attributes.getValue("title"), true);
                        redirectAnchors.add(
                                new AnchorExtractor.Anchor(new MutableString(baseURL.length() + link.length())
                                        .append(baseURL).append(link), title.copy()));
                    }
            } else if ("namespace".equals(localName)) {
                // Found a new namespace
                inNamespaceDef = true;
                nameSpaceAccumulator.length(0);
            }
        }

        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
            if ("namespace".equals(localName)) { // Collecting a namespace
                if (nameSpaceAccumulator.length() != 0)
                    nameSpacesAccumulator.add(nameSpaceAccumulator.copy().toLowerCase());
                return;
            }

            if ("namespaces".equals(localName)) { // All namespaces collected
                nameSpaces = ImmutableSet.copyOf(nameSpacesAccumulator);
                return;
            }

            if (!redirect) {
                if ("title".equals(localName)) {
                    // Set basic metadata for the page
                    metadata.put(PropertyBasedDocumentFactory.MetadataKeys.TITLE, title.copy());
                    String link = Encoder.encodeTitleToUrl(title.toString(), true);
                    metadata.put(PropertyBasedDocumentFactory.MetadataKeys.URI,
                            new MutableString(baseURL.length() + link.length()).append(baseURL).append(link));
                    inTitle = false;
                } else if ("id".equals(localName)) {
                    metadata.put(MetadataKeys.ID, Long.valueOf(id.toString()));
                    inId = false;
                } else if ("timestamp".equals(localName)) {
                    try {
                        metadata.put(MetadataKeys.LASTEDIT, dateFormat.parse(timestamp.toString()));
                    } catch (ParseException e) {
                        throw new RuntimeException(e.getMessage(), e);
                    }
                    inTimestamp = false;
                } else if ("text".equals(localName)) {
                    inText = false;
                    if (!keepNamespaced) {
                        // Namespaces are case-insensitive and language-dependent
                        final int pos = title.indexOf(':');
                        if (pos != -1 && nameSpaces.contains(title.substring(0, pos).toLowerCase()))
                            return;
                    }
                    try {
                        final MutableString html = new MutableString();
                        DocumentFactory freeFactory;
                        try {
                            freeFactory = freeFactories.take();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e.getMessage(), e);
                        }
                        if (parseText) {
                            if (DISAMBIGUATION.search(text) != -1) { // It's a disambiguation page.
                                /* Roi's hack: duplicate links using the page title, so the generic name will end up as anchor text. */
                                final MutableString newLinks = new MutableString();
                                for (int start = 0, end; (start = BRACKETS_OPEN.search(text,
                                        start)) != -1; start = end) {
                                    end = start;
                                    final int endOfLink = text.indexOfAnyOf(END_OF_DISAMBIGUATION_LINK, start);
                                    // Note that we don't escape title because we are working at the Wikipedia raw text level.
                                    if (endOfLink != -1) {
                                        newLinks.append(text.array(), start, endOfLink - start).append('|')
                                                .append(title).append("]]\n");
                                        end = endOfLink;
                                    }
                                    end++;
                                }

                                text.append(newLinks);
                            }
                            // We separate categories by OXOXO, so we don't get overflowing phrases.
                            final MutableString category = new MutableString();
                            for (int start = 0, end; (start = CATEGORY_START.search(text,
                                    start)) != -1; start = end) {
                                end = BRACKETS_CLOSED.search(text, start += CATEGORY_START.length());
                                if (end != -1)
                                    category.append(text.subSequence(start, end)).append(" OXOXO ");
                                else
                                    break;
                            }
                            metadata.put(MetadataKeys.CATEGORY, category);

                            // Heuristics to get the first paragraph
                            metadata.put(MetadataKeys.FIRSTPAR, new MutableString());
                            String plainText = wikiModel.render(new PlainTextConverter(true), text.toString());
                            for (int start = 0; start < plainText.length(); start++) {
                                //System.err.println("Examining " + plainText.charAt( start )  );
                                if (Character.isWhitespace(plainText.charAt(start)))
                                    continue;
                                if (plainText.charAt(start) == '{') {
                                    //System.err.print( "Braces " + start + " text: \"" + plainText.subSequence( start, start + 10 )  + "\" -> " );
                                    start = BRACES_CLOSED.search(plainText, start);
                                    //System.err.println( start + " text: \"" + plainText.subSequence( start, start + 10 ) + "\"" );
                                    if (start == -1)
                                        break;
                                    start++;
                                } else if (plainText.charAt(start) == '[') {
                                    start = BRACKETS_CLOSED.search(plainText, start);
                                    if (start == -1)
                                        break;
                                    start++;
                                } else {
                                    final int end = plainText.indexOf('\n', start);
                                    if (end != -1)
                                        metadata.put(MetadataKeys.FIRSTPAR,
                                                new MutableString(plainText.substring(start, end)));
                                    break;
                                }
                            }

                            try {
                                wikiModel.render(new HTMLConverter(), text.toString(), html, false, true);
                                final Map<String, String> categories = wikiModel.getCategories();
                                // Put back category links in the page (they have been parsed by bliki and to not appear anymore in the HTML rendering)
                                for (Entry<String, String> entry : categories.entrySet()) {
                                    final String key = entry.getKey();
                                    final String value = entry.getValue().trim();
                                    if (value.length() != 0) // There are empty such things
                                        html.append("\n<a href=\"").append(baseURL).append("Category:")
                                                .append(Encoder.encodeTitleToUrl(key, true)).append("\">")
                                                .append(HtmlEscapers.htmlEscaper().escape(key))
                                                .append("</a>\n");
                                }
                            } catch (Exception e) {
                                LOGGER.error("Unexpected exception while parsing " + title, e);
                            }
                        }
                        readyDocumentsAndFactories.put(new DocumentAndFactory(
                                freeFactory.getDocument(IOUtils.toInputStream(html, Charsets.UTF_8),
                                        new Reference2ObjectOpenHashMap<Enum<?>, Object>(metadata)),
                                freeFactory));
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e.getMessage(), e);
                    } catch (IOException e) {
                        throw new RuntimeException(e.getMessage(), e);
                    }
                }
            }
        }

        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            if (inText && parseText)
                text.append(ch, start, length);
            if (inTitle)
                title.append(ch, start, length);
            if (inId)
                id.append(ch, start, length);
            if (inTimestamp)
                timestamp.append(ch, start, length);
            if (inNamespaceDef) {
                nameSpaceAccumulator.append(ch, start, length);
                inNamespaceDef = false; // Dirty, but it works
            }
        }

        @Override
        public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
            if (inText && parseText)
                text.append(ch, start, length);
            if (inTitle)
                title.append(ch, start, length);
        }
    };

    final Thread parsingThread = new Thread() {
        public void run() {
            try {
                InputStream in = new FileInputStream(wikipediaXmlDump);
                if (bzipped)
                    in = new BZip2CompressorInputStream(in);
                parser.parse(
                        new InputSource(new InputStreamReader(new FastBufferedInputStream(in), Charsets.UTF_8)),
                        handler);
                readyDocumentsAndFactories.put(END);
            } catch (Exception e) {
                throw new RuntimeException(e.getMessage(), e);
            }
        }
    };

    parsingThread.start();

    return new AbstractDocumentIterator() {
        private DocumentFactory lastFactory;

        @Override
        public Document nextDocument() throws IOException {
            try {
                final DocumentAndFactory documentAndFactory = readyDocumentsAndFactories.take();
                if (lastFactory != null)
                    freeFactories.put(lastFactory);
                if (documentAndFactory == END)
                    return null;
                lastFactory = documentAndFactory.factory;
                return documentAndFactory.document;
            } catch (InterruptedException e) {
                throw new RuntimeException(e.getMessage(), e);
            }
        }
    };
}

From source file:efen.parsewiki.WikipediaDocumentSequence.java

@Override
public DocumentIterator iterator() throws IOException {
    final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    saxParserFactory.setNamespaceAware(true);
    final MutableString nameSpaceAccumulator = new MutableString();
    final ObjectOpenHashSet<MutableString> nameSpacesAccumulator = new ObjectOpenHashSet<MutableString>();
    final ArrayBlockingQueue<DocumentFactory> freeFactories = new ArrayBlockingQueue<DocumentFactory>(16);
    for (int i = freeFactories.remainingCapacity(); i-- != 0;)
        freeFactories.add(this.factory.copy());
    final ArrayBlockingQueue<DocumentAndFactory> readyDocumentsAndFactories = new ArrayBlockingQueue<DocumentAndFactory>(
            freeFactories.size());// w w w .j  ava 2  s  . c o  m

    final SAXParser parser;
    try {
        parser = saxParserFactory.newSAXParser();
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    final DefaultHandler handler = new DefaultHandler() {
        private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        private boolean inText;
        private boolean inTitle;
        private boolean inId;
        private boolean inTimestamp;
        private boolean inNamespaceDef;
        private boolean redirect;
        private MutableString text = new MutableString();
        private MutableString title = new MutableString();
        private MutableString id = new MutableString();
        private MutableString timestamp = new MutableString();
        private final Reference2ObjectMap<Enum<?>, Object> metadata = new Reference2ObjectOpenHashMap<Enum<?>, Object>();
        {
            metadata.put(PropertyBasedDocumentFactory.MetadataKeys.ENCODING, "UTF-8");
            metadata.put(MetadataKeys.REDIRECT, redirectAnchors);
        }

        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes)
                throws SAXException {
            if ("page".equals(localName)) {
                redirect = inText = inTitle = inId = inTimestamp = false;
                text.length(0);
                title.length(0);
                id.length(0);
                timestamp.length(0);
            } else if ("text".equals(localName))
                inText = true;
            else if ("title".equals(localName) && title.length() == 0)
                inTitle = true; // We catch only the first id/title elements.
            else if ("id".equals(localName) && id.length() == 0)
                inId = true;
            else if ("timestamp".equals(localName) && timestamp.length() == 0)
                inTimestamp = true;
            else if ("redirect".equals(localName)) {
                redirect = true;
                if (attributes.getValue("title") != null)
                    // Accumulate the title of the page as virtual text of the redirect page.
                    synchronized (redirectAnchors) {
                        final String link = Encoder.encodeTitleToUrl(attributes.getValue("title"), true);
                        redirectAnchors.add(
                                new AnchorExtractor.Anchor(new MutableString(baseURL.length() + link.length())
                                        .append(baseURL).append(link), title.copy()));
                    }
            } else if ("namespace".equals(localName)) {
                // Found a new namespace
                inNamespaceDef = true;
                nameSpaceAccumulator.length(0);
            }
        }

        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
            if ("namespace".equals(localName)) { // Collecting a namespace
                if (nameSpaceAccumulator.length() != 0)
                    nameSpacesAccumulator.add(nameSpaceAccumulator.copy().toLowerCase());
                return;
            }

            if ("namespaces".equals(localName)) { // All namespaces collected
                nameSpaces = ImmutableSet.copyOf(nameSpacesAccumulator);
                return;
            }

            if (!redirect) {
                if ("title".equals(localName)) {
                    // Set basic metadata for the page
                    metadata.put(PropertyBasedDocumentFactory.MetadataKeys.TITLE, title.copy());
                    String link = Encoder.encodeTitleToUrl(title.toString(), true);
                    metadata.put(PropertyBasedDocumentFactory.MetadataKeys.URI,
                            new MutableString(baseURL.length() + link.length()).append(baseURL).append(link));
                    inTitle = false;
                } else if ("id".equals(localName)) {
                    metadata.put(MetadataKeys.ID, Long.valueOf(id.toString()));
                    inId = false;
                } else if ("timestamp".equals(localName)) {
                    try {
                        metadata.put(MetadataKeys.LASTEDIT, dateFormat.parse(timestamp.toString()));
                    } catch (ParseException e) {
                        throw new RuntimeException(e.getMessage(), e);
                    }
                    inTimestamp = false;
                } else if ("text".equals(localName)) {
                    inText = false;
                    if (!keepNamespaced) {
                        // Namespaces are case-insensitive and language-dependent
                        final int pos = title.indexOf(':');
                        if (pos != -1 && isATrueNamespace(title.substring(0, pos)))
                            return;
                    }
                    try {
                        final MutableString html = new MutableString();
                        DocumentFactory freeFactory;
                        try {
                            freeFactory = freeFactories.take();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e.getMessage(), e);
                        }
                        if (parseText) {
                            if (DISAMBIGUATION.search(text) != -1) { // It's a disambiguation page.
                                /* Roi's hack: duplicate links using the page title, so the generic name will end up as anchor text. */
                                final MutableString newLinks = new MutableString();
                                for (int start = 0, end; (start = BRACKETS_OPEN.search(text,
                                        start)) != -1; start = end) {
                                    end = start;
                                    final int endOfLink = text.indexOfAnyOf(END_OF_DISAMBIGUATION_LINK, start);
                                    // Note that we don't escape title because we are working at the Wikipedia raw text level.
                                    if (endOfLink != -1) {
                                        newLinks.append(text.array(), start, endOfLink - start).append('|')
                                                .append(title).append("]]\n");
                                        end = endOfLink;
                                    }
                                    end++;
                                }

                                text.append(newLinks);
                            }
                            // We separate categories by OXOXO, so we don't get overflowing phrases.
                            final MutableString category = new MutableString();
                            for (int start = 0, end; (start = CATEGORY_START.search(text,
                                    start)) != -1; start = end) {
                                end = BRACKETS_CLOSED.search(text, start += CATEGORY_START.length());
                                if (end != -1)
                                    category.append(text.subSequence(start, end)).append(" OXOXO ");
                                else
                                    break;
                            }
                            metadata.put(MetadataKeys.CATEGORY, category);

                            // Heuristics to get the first paragraph
                            metadata.put(MetadataKeys.FIRSTPAR, new MutableString());
                            String plainText = new WikiModel(imageBaseURL, linkBaseURL)
                                    .render(new PlainTextConverter(true), text.toString());
                            for (int start = 0; start < plainText.length(); start++) {
                                //System.err.println("Examining " + plainText.charAt( start )  );
                                if (Character.isWhitespace(plainText.charAt(start)))
                                    continue;
                                if (plainText.charAt(start) == '{') {
                                    //System.err.print( "Braces " + start + " text: \"" + plainText.subSequence( start, start + 10 )  + "\" -> " );
                                    start = BRACES_CLOSED.search(plainText, start);
                                    //System.err.println( start + " text: \"" + plainText.subSequence( start, start + 10 ) + "\"" );
                                    if (start == -1)
                                        break;
                                    start++;
                                } else if (plainText.charAt(start) == '[') {
                                    start = BRACKETS_CLOSED.search(plainText, start);
                                    if (start == -1)
                                        break;
                                    start++;
                                } else {
                                    final int end = plainText.indexOf('\n', start);
                                    if (end != -1)
                                        metadata.put(MetadataKeys.FIRSTPAR,
                                                new MutableString(plainText.substring(start, end)));//new MutableString( new WikiModel( imageBaseURL, linkBaseURL ).render( new PlainTextConverter( true ), text.substring( start, end ).toString() ) ) );
                                    break;
                                }
                            }

                            try {
                                WikiModel wikiModel = new WikiModel(imageBaseURL, linkBaseURL);
                                wikiModel.render(new HTMLConverter(), text.toString(), html, false, false);
                                final Map<String, String> categories = wikiModel.getCategories();
                                // Put back category links in the page (they have been parsed by bliki and to not appear anymore in the HTML rendering)
                                for (Entry<String, String> entry : categories.entrySet()) {
                                    final String key = entry.getKey();
                                    final String value = entry.getValue().trim();
                                    if (value.length() != 0) // There are empty such things
                                        html.append("\n<a href=\"").append(baseURL).append("Category:")
                                                .append(Encoder.encodeTitleToUrl(key, true)).append("\">")
                                                .append(HtmlEscapers.htmlEscaper().escape(key))
                                                .append("</a>\n");
                                }
                            } catch (Exception e) {
                                LOGGER.error("Unexpected exception while parsing " + title, e);
                            }
                        }
                        readyDocumentsAndFactories.put(new DocumentAndFactory(
                                freeFactory.getDocument(IOUtils.toInputStream(html, Charsets.UTF_8),
                                        new Reference2ObjectOpenHashMap<Enum<?>, Object>(metadata)),
                                freeFactory));
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e.getMessage(), e);
                    } catch (IOException e) {
                        throw new RuntimeException(e.getMessage(), e);
                    }
                }
            }
        }

        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            if (inText && parseText)
                text.append(ch, start, length);
            if (inTitle)
                title.append(ch, start, length);
            if (inId)
                id.append(ch, start, length);
            if (inTimestamp)
                timestamp.append(ch, start, length);
            if (inNamespaceDef) {
                nameSpaceAccumulator.append(ch, start, length);
                inNamespaceDef = false; // Dirty, but it works
            }
        }

        @Override
        public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
            if (inText && parseText)
                text.append(ch, start, length);
            if (inTitle)
                title.append(ch, start, length);
        }
    };

    final Thread parsingThread = new Thread() {
        public void run() {
            try {
                InputStream in = new FileInputStream(wikipediaXmlDump);
                if (bzipped)
                    in = new BZip2CompressorInputStream(in);
                parser.parse(
                        new InputSource(new InputStreamReader(new FastBufferedInputStream(in), Charsets.UTF_8)),
                        handler);
                readyDocumentsAndFactories.put(END);
            } catch (Exception e) {
                throw new RuntimeException(e.getMessage(), e);
            }
        }
    };

    parsingThread.start();

    return new AbstractDocumentIterator() {
        private DocumentFactory lastFactory;

        @Override
        public Document nextDocument() throws IOException {
            try {
                final DocumentAndFactory documentAndFactory = readyDocumentsAndFactories.take();
                if (lastFactory != null)
                    freeFactories.put(lastFactory);
                if (documentAndFactory == END)
                    return null;
                lastFactory = documentAndFactory.factory;
                return documentAndFactory.document;
            } catch (InterruptedException e) {
                throw new RuntimeException(e.getMessage(), e);
            }
        }
    };
}

From source file:com.silverwrist.venice.core.impl.ConferencingImporter.java

final List importMessages(InputStream xmlstm) throws DataException {
    try { // create a SAX parser and let it loose on the input data with our listener
        SAXParserFactory fact = SAXParserFactory.newInstance();
        fact.setNamespaceAware(false);
        fact.setValidating(false);/*w  w  w  .ja  v  a 2 s .c  o m*/
        SAXParser parser = fact.newSAXParser();
        parser.parse(xmlstm, new Listener());

    } // end try
    catch (ParserConfigurationException e) { // configuration error
        throw new DataException("Error configuring XML parser for message import: " + e.getMessage(), e);

    } // end catch
    catch (SAXException e) { // give an error message
        throw new DataException("Error importing messages: " + e.getMessage(), e);

    } // end catch
    catch (IOException e) { // I/O error in parsing!
        throw new DataException("Error importing messages: " + e.getMessage(), e);

    } // end catch

    if (m_events == null)
        return Collections.EMPTY_LIST;
    ArrayList rc = new ArrayList(m_events);
    m_events = null;
    return Collections.unmodifiableList(rc);

}

From source file:com.silverpeas.importExport.control.ImportExport.java

/**
 * Mthode retournant l'arbre des objets mapps sur le fichier xml pass en paramtre.
 *
 * @param xmlFileName le fichier xml interprt par Castor
 * @return Un objet SilverPeasExchangeType contenant le mapping d'un fichier XML Castor
 * @throws ImportExportException// www  .j a v a2s .co  m
 */
SilverPeasExchangeType loadSilverpeasExchange(String xmlFileName) throws ImportExportException {
    SilverTrace.debug("importExport", "ImportExportSessionController.loadSilverpeasExchange",
            "root.MSG_GEN_ENTER_METHOD", "xmlFileName = " + xmlFileName);

    try {
        InputSource xmlInputSource = new InputSource(xmlFileName);
        String xsdPublicId = settings.getString("xsdPublicId");
        String xsdSystemId = settings.getString("xsdDefaultSystemId");

        // Load and parse default XML schema for import/export
        SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema",
                "com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory", null);
        Schema schema = schemaFactory.newSchema(new StreamSource(xsdSystemId));

        // Create an XML parser for loading XML import file
        SAXParserFactory factory = SAXParserFactory
                .newInstance("com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl", null);
        factory.setValidating(false);
        factory.setNamespaceAware(true);
        factory.setSchema(schema);
        SAXParser parser = factory.newSAXParser();

        // First try to determine to load the XML file using the default
        // XML-Schema
        ImportExportErrorHandler errorHandler = new ImportExportErrorHandler();
        XMLReader xmlReader = parser.getXMLReader();
        xmlReader.setErrorHandler(errorHandler);

        try {
            xmlReader.parse(xmlInputSource);

        } catch (SAXException ex) {
            SilverTrace.debug("importExport", "ImportExportSessionController.loadSilverpeasExchange",
                    "root.MSG_GEN_PARAM_VALUE", (new StringBuilder("XML File ")).append(xmlFileName)
                            .append(" is not valid according to default schema").toString());

            // If case the default schema is not the one specified by the
            // XML import file, try to get the right XML-schema and
            // namespace (this is done by parsing without validation)
            ImportExportNamespaceHandler nsHandler = new ImportExportNamespaceHandler();
            factory.setSchema(null);
            parser = factory.newSAXParser();
            xmlReader = parser.getXMLReader();
            xmlReader.setContentHandler(nsHandler);
            xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
            xmlReader.parse(xmlInputSource);

            // If OK, extract the name and location of the schema
            String nsSpec = nsHandler.getNsSpec();
            if (nsSpec == null || xsdPublicId.equals(nsSpec)) {
                throw ex;
            }

            String nsVersion = extractUriNameIndex(nsSpec);
            if (nsVersion.length() == 0) {
                throw ex;
            }

            String altXsdSystemId = settings.getStringWithParam("xsdSystemId", nsVersion);
            if ((altXsdSystemId == null) || (altXsdSystemId.equals(xsdSystemId))) {
                throw ex;
            }

            SilverTrace.debug("importExport", "ImportExportSessionController.loadSilverpeasExchange",
                    "root.MSG_GEN_PARAM_VALUE",
                    (new StringBuilder("Trying again using schema specification located at "))
                            .append(altXsdSystemId).toString());

            // Try again to load, parse and validate the XML import file,
            // using the new schema specification
            schema = schemaFactory.newSchema(new StreamSource(altXsdSystemId));
            factory.setSchema(schema);
            parser = factory.newSAXParser();
            xmlReader = parser.getXMLReader();
            xmlReader.setErrorHandler(errorHandler);
            xmlReader.parse(xmlInputSource);
        }

        SilverTrace.debug("importExport", "ImportExportSessionController.loadSilverpeasExchange",
                "root.MSG_GEN_PARAM_VALUE", "XML Validation complete");

        // Mapping file for Castor
        String mappingDir = settings.getString("mappingDir");
        String mappingFileName = settings.getString("importExportMapping");
        String mappingFile = mappingDir + mappingFileName;
        Mapping mapping = new Mapping();

        // Load mapping and instantiate a Unmarshaller
        mapping.loadMapping(mappingFile);
        Unmarshaller unmar = new Unmarshaller(SilverPeasExchangeType.class);
        unmar.setMapping(mapping);
        unmar.setValidation(false);

        // Unmarshall the process model
        SilverPeasExchangeType silverpeasExchange = (SilverPeasExchangeType) unmar.unmarshal(xmlInputSource);
        SilverTrace.debug("importExport", "ImportExportSessionController.loadSilverpeasExchange",
                "root.MSG_GEN_PARAM_VALUE", "Unmarshalling complete");
        return silverpeasExchange;

    } catch (MappingException me) {
        throw new ImportExportException("ImportExport.loadSilverpeasExchange",
                "importExport.EX_LOADING_XML_MAPPING_FAILED",
                "XML Filename " + xmlFileName + ": " + me.getLocalizedMessage(), me);
    } catch (MarshalException me) {
        throw new ImportExportException("ImportExport.loadSilverpeasExchange",
                "importExport.EX_UNMARSHALLING_FAILED",
                "XML Filename " + xmlFileName + ": " + me.getLocalizedMessage(), me);
    } catch (ValidationException ve) {
        throw new ImportExportException("ImportExport.loadSilverpeasExchange", "importExport.EX_PARSING_FAILED",
                "XML Filename " + xmlFileName + ": " + ve.getLocalizedMessage(), ve);
    } catch (IOException ioe) {
        throw new ImportExportException("ImportExport.loadSilverpeasExchange",
                "importExport.EX_LOADING_XML_MAPPING_FAILED",
                "XML Filename " + xmlFileName + ": " + ioe.getLocalizedMessage(), ioe);
    } catch (ParserConfigurationException ex) {
        throw new ImportExportException("ImportExport.loadSilverpeasExchange", "importExport.EX_PARSING_FAILED",
                "XML Filename " + xmlFileName + ": " + ex.getLocalizedMessage(), ex);
    } catch (SAXNotRecognizedException snre) {
        throw new ImportExportException("ImportExport.loadSilverpeasExchange", "importExport.EX_PARSING_FAILED",
                "XML Filename " + xmlFileName + ": " + snre.getLocalizedMessage(), snre);
    } catch (SAXNotSupportedException snse) {
        throw new ImportExportException("ImportExport.loadSilverpeasExchange", "importExport.EX_PARSING_FAILED",
                "XML Filename " + xmlFileName + ": " + snse.getLocalizedMessage(), snse);
    } catch (SAXException se) {
        throw new ImportExportException("ImportExport.loadSilverpeasExchange", "importExport.EX_PARSING_FAILED",
                "XML Filename " + xmlFileName + ": " + se.getLocalizedMessage(), se);
    }
}

From source file:forseti.admon.JAdmCFDDlg.java

@SuppressWarnings({ "rawtypes", "unchecked" })
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    super.doPost(request, response);

    String adm_cfdi_dlg = "";
    request.setAttribute("adm_cfdi_dlg", adm_cfdi_dlg);

    String mensaje = "";
    short idmensaje = -1;

    if (request.getContentType() != null
            && request.getContentType().toLowerCase().indexOf("multipart/form-data") > -1) {
        if (!getSesion(request).getRegistrado()) {
            irApag("/forsetiweb/errorAtributos.jsp", request, response);
            return;
        } else {//from   w  w  w .j av a 2s.  co  m
            if (!getSesion(request).getPermiso("ADM_CFDI_AGREGAR")) {
                idmensaje = 3;
                mensaje += MsjPermisoDenegado(request, "CEF", "ADM_CFDI_AGREGAR");
                getSesion(request).setID_Mensaje(idmensaje, mensaje);
                RDP("CEF", getSesion(request).getConBD(), "NA", getSesion(request).getID_Usuario(),
                        "ADM_CFDI_AGREGAR", "ACFD||||", mensaje);
                irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                return;
            }

            try {
                Vector archivos = new Vector();
                DiskFileUpload fu = new DiskFileUpload();
                List items = fu.parseRequest(request);
                Iterator iter = items.iterator();
                while (iter.hasNext()) {
                    FileItem item = (FileItem) iter.next();
                    if (!item.isFormField())
                        archivos.addElement(item);
                }

                SubirArchivosCFD(request, response, archivos);
                return;
            } catch (FileUploadException e) {
                e.printStackTrace();
                return;
            }
        }

    }

    if (request.getParameter("proceso") != null && !request.getParameter("proceso").equals("")) {
        if (request.getParameter("proceso").equals("VERIFICAR_CFD")) {
            // Verificacion
            if (!getSesion(request).getPermiso("ADM_CFDI_AGREGAR")) {
                idmensaje = 3;
                mensaje += MsjPermisoDenegado(request, "CEF", "ADM_CFDI_AGREGAR");
                getSesion(request).setID_Mensaje(idmensaje, mensaje);
                RDP("CEF", getSesion(request).getConBD(), "NA", getSesion(request).getID_Usuario(),
                        "ADM_CFDI_AGREGAR", "ACFD||||", mensaje);
                irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                return;
            }

            if (VerificarCertificados(request, response))
                AgregarCertificado(request, response);

            return;
        } else if (request.getParameter("proceso").equals("CAMBIAR_EMISOR")) {
            System.out.println(request.getParameter("cfd_regimenfiscal"));
            // Revisa si tiene permisos
            if (!getSesion(request).getPermiso("ADM_CFDI_AGREGAR")) {
                idmensaje = 3;
                mensaje += MsjPermisoDenegado(request, "CEF", "ADM_CFDI_AGREGAR");
                getSesion(request).setID_Mensaje(idmensaje, mensaje);
                RDP("CEF", getSesion(request).getConBD(), "NA", getSesion(request).getID_Usuario(),
                        "ADM_CFDI_AGREGAR", "ACFD||||", mensaje);
                irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                return;
            }
            if (request.getParameter("subproceso") != null
                    && request.getParameter("subproceso").equals("ENVIAR")) {
                // Verificacion
                if (VerificarParametros(request, response)) {
                    Cambiar(request, response);
                    return;
                }

                irApag("/forsetiweb/administracion/adm_cfd_dlg.jsp", request, response);
                return;
            } else if (request.getParameter("subproceso") != null
                    && request.getParameter("subproceso").equals("ACTUALIZAR")) {
                irApag("/forsetiweb/administracion/adm_cfd_dlg.jsp", request, response);
                return;
            } else // Como el subproceso no es ENVIAR, abre la ventana del proceso de CAMBIADO para cargar el cambio
            {
                getSesion(request).setID_Mensaje(idmensaje, mensaje);
                irApag("/forsetiweb/administracion/adm_cfd_dlg.jsp", request, response);
                return;
            }

        } else if (request.getParameter("proceso").equals("AGREGAR_CERTIFICADO")) {
            // Revisa si tiene permisos
            if (!getSesion(request).getPermiso("ADM_CFDI_AGREGAR")) {
                idmensaje = 3;
                mensaje += MsjPermisoDenegado(request, "CEF", "ADM_CFDI_AGREGAR");
                getSesion(request).setID_Mensaje(idmensaje, mensaje);
                RDP("CEF", getSesion(request).getConBD(), "NA", getSesion(request).getID_Usuario(),
                        "ADM_CFDI_AGREGAR", "ACFD||||", mensaje);
                irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                return;
            }

            Integer subir_archivos = new Integer(2);
            request.setAttribute("subir_archivos", subir_archivos);

            getSesion(request).setID_Mensaje(idmensaje, mensaje);
            irApag("/forsetiweb/subir_archivos.jsp?verif=/servlet/CEFAdmCFDDlg&archivo_1=cer&archivo_2=key&proceso=AGREGAR_CERTIFICADO&subproceso=ENVIAR",
                    request, response);
            return;

        } else if (request.getParameter("proceso").equals("AGREGAR_EXPEDITOR")) {
            // Revisa si tiene permisos
            if (!getSesion(request).getPermiso("ADM_CFDI_AGREGAR")) {
                idmensaje = 3;
                mensaje += MsjPermisoDenegado(request, "CEF", "ADM_CFDI_AGREGAR");
                getSesion(request).setID_Mensaje(idmensaje, mensaje);
                RDP("CEF", getSesion(request).getConBD(), "NA", getSesion(request).getID_Usuario(),
                        "ADM_CFDI_AGREGAR", "ACFD||||", mensaje);
                irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                return;
            }
            if (request.getParameter("subproceso") != null
                    && request.getParameter("subproceso").equals("ENVIAR")) {
                // Verificacion
                if (VerificarParametrosExpRec(request, response)) {
                    AgregarCambiarExpRec(request, response, JForsetiApl.AGREGAR, "EXP");
                    return;
                }
                irApag("/forsetiweb/administracion/adm_cfd_dlg_exprec.jsp", request, response);
                return;
            } else if (request.getParameter("subproceso") != null
                    && request.getParameter("subproceso").equals("ACTUALIZAR")) {
                irApag("/forsetiweb/administracion/adm_cfd_dlg_exprec.jsp", request, response);
                return;
            } else // Como el subproceso no es ENVIAR, abre la ventana del proceso de CAMBIADO para cargar el cambio
            {
                getSesion(request).setID_Mensaje(idmensaje, mensaje);
                irApag("/forsetiweb/administracion/adm_cfd_dlg_exprec.jsp", request, response);
                return;
            }

        } else if (request.getParameter("proceso").equals("CAMBIAR_EXPEDITOR")) {
            // Revisa si tiene permisos
            if (!getSesion(request).getPermiso("ADM_CFDI_AGREGAR")) {
                idmensaje = 3;
                mensaje += MsjPermisoDenegado(request, "CEF", "ADM_CFDI_AGREGAR");
                getSesion(request).setID_Mensaje(idmensaje, mensaje);
                RDP("CEF", getSesion(request).getConBD(), "NA", getSesion(request).getID_Usuario(),
                        "ADM_CFDI_AGREGAR", "ACFD||||", mensaje);
                irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                return;
            }

            if (request.getParameter("id") != null) {
                String[] valoresParam = request.getParameterValues("id");
                if (valoresParam.length == 1) {
                    if (request.getParameter("subproceso") != null
                            && request.getParameter("subproceso").equals("ENVIAR")) {
                        // Verificacion
                        if (VerificarParametrosExpRec(request, response)) {
                            AgregarCambiarExpRec(request, response, JForsetiApl.CAMBIAR, "EXP");
                            return;
                        }
                        irApag("/forsetiweb/administracion/adm_cfd_dlg_exprec.jsp", request, response);
                        return;
                    } else if (request.getParameter("subproceso") != null
                            && request.getParameter("subproceso").equals("ACTUALIZAR")) {
                        irApag("/forsetiweb/administracion/adm_cfd_dlg_exprec.jsp", request, response);
                        return;
                    } else // Como el subproceso no es ENVIAR, abre la ventana del proceso de CAMBIADO para cargar el cambio
                    {
                        getSesion(request).setID_Mensaje(idmensaje, mensaje);
                        irApag("/forsetiweb/administracion/adm_cfd_dlg_exprec.jsp", request, response);
                        return;
                    }
                } else {
                    idmensaje = 1;
                    mensaje += JUtil.Msj("GLB", "VISTA", "GLB", "SELEC-PROC", 2);
                    getSesion(request).setID_Mensaje(idmensaje, mensaje);
                    irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                    return;
                }
            } else {
                idmensaje = 3;
                mensaje += JUtil.Msj("GLB", "VISTA", "GLB", "SELEC-PROC", 1);
                getSesion(request).setID_Mensaje(idmensaje, mensaje);
                irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                return;
            }
        } else if (request.getParameter("proceso").equals("AGREGAR_RECEPTOR")) {
            // Revisa si tiene permisos
            if (!getSesion(request).getPermiso("ADM_CFDI_AGREGAR")) {
                idmensaje = 3;
                mensaje += MsjPermisoDenegado(request, "CEF", "ADM_CFDI_AGREGAR");
                getSesion(request).setID_Mensaje(idmensaje, mensaje);
                RDP("CEF", getSesion(request).getConBD(), "NA", getSesion(request).getID_Usuario(),
                        "ADM_CFDI_AGREGAR", "ACFD||||", mensaje);
                irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                return;
            }

            if (request.getParameter("subproceso") != null
                    && request.getParameter("subproceso").equals("ENVIAR")) {
                // Verificacion
                if (VerificarParametrosExpRec(request, response)) {
                    AgregarCambiarExpRec(request, response, JForsetiApl.AGREGAR, "REC");
                    return;
                }
                irApag("/forsetiweb/administracion/adm_cfd_dlg_exprec.jsp", request, response);
                return;
            } else // Como el subproceso no es ENVIAR, abre la ventana del proceso de CAMBIADO para cargar el cambio
            {
                getSesion(request).setID_Mensaje(idmensaje, mensaje);
                irApag("/forsetiweb/administracion/adm_cfd_dlg_exprec.jsp", request, response);
                return;
            }

        } else if (request.getParameter("proceso").equals("CAMBIAR_RECEPTOR")) {
            // Revisa si tiene permisos
            if (!getSesion(request).getPermiso("ADM_CFDI_AGREGAR")) {
                idmensaje = 3;
                mensaje += MsjPermisoDenegado(request, "CEF", "ADM_CFDI_AGREGAR");
                getSesion(request).setID_Mensaje(idmensaje, mensaje);
                RDP("CEF", getSesion(request).getConBD(), "NA", getSesion(request).getID_Usuario(),
                        "ADM_CFDI_AGREGAR", "ACFD||||", mensaje);
                irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                return;
            }

            if (request.getParameter("id") != null) {
                String[] valoresParam = request.getParameterValues("id");
                if (valoresParam.length == 1) {
                    if (request.getParameter("subproceso") != null
                            && request.getParameter("subproceso").equals("ENVIAR")) {
                        // Verificacion
                        if (VerificarParametrosExpRec(request, response)) {
                            AgregarCambiarExpRec(request, response, JForsetiApl.CAMBIAR, "REC");
                            return;
                        }
                        irApag("/forsetiweb/administracion/adm_cfd_dlg_exprec.jsp", request, response);
                        return;
                    } else // Como el subproceso no es ENVIAR, abre la ventana del proceso de CAMBIADO para cargar el cambio
                    {
                        getSesion(request).setID_Mensaje(idmensaje, mensaje);
                        irApag("/forsetiweb/administracion/adm_cfd_dlg_exprec.jsp", request, response);
                        return;
                    }
                } else {
                    idmensaje = 1;
                    mensaje += JUtil.Msj("GLB", "VISTA", "GLB", "SELEC-PROC", 2);
                    getSesion(request).setID_Mensaje(idmensaje, mensaje);
                    irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                    return;
                }
            } else {
                idmensaje = 3;
                mensaje += JUtil.Msj("GLB", "VISTA", "GLB", "SELEC-PROC", 1);
                getSesion(request).setID_Mensaje(idmensaje, mensaje);
                irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                return;
            }
        } else if (request.getParameter("proceso").equals("GENERAR_CE")) {
            // Revisa si tiene permisos
            if (!getSesion(request).getPermiso("ADM_CFDI_GCEXML")) {
                idmensaje = 3;
                mensaje += MsjPermisoDenegado(request, "CEF", "ADM_CFDI_GCEXML");
                getSesion(request).setID_Mensaje(idmensaje, mensaje);
                RDP("CEF", getSesion(request).getConBD(), "NA", getSesion(request).getID_Usuario(),
                        "ADM_CFDI_GCEXML", "ACFD||||", mensaje);
                irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                return;
            }

            if (request.getParameter("id") != null) {
                String[] valoresParam = request.getParameterValues("id");
                if (valoresParam.length == 1) {
                    if (request.getParameter("subproceso") != null
                            && request.getParameter("subproceso").equals("ENVIAR")) {
                        int ano = Integer.parseInt(JUtil.Elm(request.getParameter("id"), 1));
                        int mes = Integer.parseInt(JUtil.Elm(request.getParameter("id"), 2));

                        JAdmPeriodosSet set = new JAdmPeriodosSet(request);
                        set.m_Where = "Ano = '" + ano + "' and Mes = '" + mes + "'";
                        set.Open();

                        if (!set.getAbsRow(0).getCerrado()) {
                            idmensaje = 1;
                            mensaje = "PRECAUCION: No se pueden generar los archivos XML de la Contabilidada Electrnica, porque el mes seleccionado no esta cerrado aun.";
                            getSesion(request).setID_Mensaje(idmensaje, mensaje);
                            irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                            return;
                        }

                        GenerarXMLCE(request, response, ano, mes);
                        return;
                    } else {
                        getSesion(request).setID_Mensaje(idmensaje, mensaje);
                        irApag("/forsetiweb/administracion/adm_cfd_dlg_cegen.jsp", request, response);
                        return;
                    }

                } else {
                    idmensaje = 1;
                    mensaje += JUtil.Msj("GLB", "VISTA", "GLB", "SELEC-PROC", 2);
                    getSesion(request).setID_Mensaje(idmensaje, mensaje);
                    irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                    return;
                }
            } else {
                idmensaje = 3;
                mensaje += JUtil.Msj("GLB", "VISTA", "GLB", "SELEC-PROC", 1);
                getSesion(request).setID_Mensaje(idmensaje, mensaje);
                irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                return;
            }
        } else if (request.getParameter("proceso").equals("CONSULTAR_ST")) {
            // Revisa si tiene permisos
            if (!getSesion(request).getPermiso("ADM_CFDI_GCEXML")) {
                idmensaje = 3;
                mensaje += MsjPermisoDenegado(request, "CEF", "ADM_CFDI_GCEXML");
                getSesion(request).setID_Mensaje(idmensaje, mensaje);
                RDP("CEF", getSesion(request).getConBD(), "NA", getSesion(request).getID_Usuario(),
                        "ADM_CFDI_GCEXML", "ACFD||||", mensaje);
                irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                return;
            }

            if (request.getParameter("id") != null) {
                String[] valoresParam = request.getParameterValues("id");
                if (valoresParam.length == 1) {
                    int ano = Integer.parseInt(JUtil.Elm(request.getParameter("id"), 1));
                    int mes = Integer.parseInt(JUtil.Elm(request.getParameter("id"), 2));

                    JContCEModuloSet set = new JContCEModuloSet(request,
                            getSesion(request).getSesion("ADM_CFDI").getEspecial());
                    set.m_Where = "Ano = '" + ano + "' and Mes = '" + mes + "'";
                    set.Open();

                    if (set.getAbsRow(0).getGenerado().equals("N")) {
                        idmensaje = 1;
                        mensaje = "PRECAUCION: No se puede consultar el registro de estatus porque no existe el archivo generado en este mes.";
                        getSesion(request).setID_Mensaje(idmensaje, mensaje);
                        irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                        return;
                    }

                    irApag("/forsetiweb/administracion/adm_cfd_dlg_cons.jsp", request, response);
                    return;

                } else {
                    idmensaje = 1;
                    mensaje += JUtil.Msj("GLB", "VISTA", "GLB", "SELEC-PROC", 2);
                    getSesion(request).setID_Mensaje(idmensaje, mensaje);
                    irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                    return;
                }
            } else {
                idmensaje = 3;
                mensaje += JUtil.Msj("GLB", "VISTA", "GLB", "SELEC-PROC", 1);
                getSesion(request).setID_Mensaje(idmensaje, mensaje);
                irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                return;
            }
        } else if (request.getParameter("proceso").equals("CONSULTAR_ARCHIVOS")) {
            // Revisa si tiene permisos
            if (!getSesion(request).getPermiso("ADM_CFDI_GCEXML")) {
                idmensaje = 3;
                mensaje += MsjPermisoDenegado(request, "CEF", "ADM_CFDI_GCEXML");
                getSesion(request).setID_Mensaje(idmensaje, mensaje);
                RDP("CEF", getSesion(request).getConBD(), "NA", getSesion(request).getID_Usuario(),
                        "ADM_CFDI_GCEXML", "ACFD||||", mensaje);
                irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                return;
            }

            if (request.getParameter("id") != null) {
                String[] valoresParam = request.getParameterValues("id");
                if (valoresParam.length == 1) {
                    int ano = Integer.parseInt(JUtil.Elm(request.getParameter("id"), 1));
                    int mes = Integer.parseInt(JUtil.Elm(request.getParameter("id"), 2));

                    JContCEModuloSet set = new JContCEModuloSet(request,
                            getSesion(request).getSesion("ADM_CFDI").getEspecial());
                    set.m_Where = "Ano = '" + ano + "' and Mes = '" + mes + "'";
                    set.Open();

                    if (set.getAbsRow(0).getGenerado().equals("N")) {
                        idmensaje = 1;
                        mensaje = "PRECAUCION: No se puede consultar el archivo porque este mes no esta generado aun.";
                        getSesion(request).setID_Mensaje(idmensaje, mensaje);
                        irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                        return;
                    }

                    String nomArchFech;
                    if (mes < 10)
                        nomArchFech = "-" + ano + "-0" + mes;
                    else
                        nomArchFech = "-" + ano + "-" + mes;
                    String nomArch = "/usr/local/forseti/emp/" + getSesion(request).getBDCompania() + "/CE/"
                            + JUtil.getSesion(request).getSesion("ADM_CFDI").getEspecial() + nomArchFech
                            + ".xml";

                    String salida = "";
                    File f = new File(nomArch);
                    FileReader fr = null;
                    BufferedReader br = null;
                    try {
                        fr = new FileReader(f);
                        br = new BufferedReader(fr);
                        String s;
                        while ((s = br.readLine()) != null) {
                            salida += s + "\n";
                        }
                        br.close();
                    } catch (IOException e1) {
                        salida += e1;
                    }
                    response.setContentType("application/xml");
                    PrintWriter out = response.getWriter();
                    out.print(salida);
                    /*
                    String nombres[] = {   nomArch + "catalogo" + nomArchFech + ".xml",
                             nomArch + "balanza" + nomArchFech + ".xml",
                             nomArch + "polizas" + nomArchFech + ".xml",
                             nomArch + "CE" + nomArchFech + ".log"};
                    String destinos[] = {   "catalogo" + nomArchFech + ".xml",
                          "balanza" + nomArchFech + ".xml",
                          "polizas" + nomArchFech + ".xml",
                          "CE" + nomArchFech + ".log" };
                            
                    JBajarArchivo fd = new JBajarArchivo();
                    fd.doDownloadMultipleFilesInZip(response, getServletConfig().getServletContext(), ("CE" + nomArchFech + ".zip"), nombres, destinos);
                    */

                    return;
                } else {
                    idmensaje = 1;
                    mensaje += JUtil.Msj("GLB", "VISTA", "GLB", "SELEC-PROC", 2);
                    getSesion(request).setID_Mensaje(idmensaje, mensaje);
                    irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                    return;
                }
            } else {
                idmensaje = 3;
                mensaje += JUtil.Msj("GLB", "VISTA", "GLB", "SELEC-PROC", 1);
                getSesion(request).setID_Mensaje(idmensaje, mensaje);
                irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                return;
            }
        } else if (request.getParameter("proceso").equals("VALIDAR_ARCHIVOS")) {
            // Revisa si tiene permisos
            if (!getSesion(request).getPermiso("ADM_CFDI_GCEXML")) {
                idmensaje = 3;
                mensaje += MsjPermisoDenegado(request, "CEF", "ADM_CFDI_GCEXML");
                getSesion(request).setID_Mensaje(idmensaje, mensaje);
                RDP("CEF", getSesion(request).getConBD(), "NA", getSesion(request).getID_Usuario(),
                        "ADM_CFDI_GCEXML", "ACFD||||", mensaje);
                irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                return;
            }

            if (request.getParameter("id") != null) {
                String[] valoresParam = request.getParameterValues("id");
                if (valoresParam.length == 1) {
                    int ano = Integer.parseInt(JUtil.Elm(request.getParameter("id"), 1));
                    int mes = Integer.parseInt(JUtil.Elm(request.getParameter("id"), 2));

                    JContCEModuloSet set = new JContCEModuloSet(request,
                            getSesion(request).getSesion("ADM_CFDI").getEspecial());
                    set.m_Where = "Ano = '" + ano + "' and Mes = '" + mes + "'";
                    set.Open();

                    if (set.getAbsRow(0).getGenerado().equals("N")) {
                        idmensaje = 1;
                        mensaje = "PRECAUCION: No se puede validar el archivo porque este mes no esta generado aun.";
                        getSesion(request).setID_Mensaje(idmensaje, mensaje);
                        irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                        return;
                    }

                    String nomArchFech;
                    if (mes < 10)
                        nomArchFech = "-" + ano + "-0" + mes;
                    else
                        nomArchFech = "-" + ano + "-" + mes;
                    String nomArch = "/usr/local/forseti/emp/" + getSesion(request).getBDCompania() + "/CE/"
                            + JUtil.getSesion(request).getSesion("ADM_CFDI").getEspecial() + nomArchFech
                            + ".xml";

                    //Validacin
                    String ERROR = "", archivoXML = "";
                    boolean NAME_SPACE_AWARE = true;
                    boolean VALIDATING = true;
                    String SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
                    String SCHEMA_LANGUAGE_VAL = "http://www.w3.org/2001/XMLSchema";
                    String SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
                    String sFichXsd = "/usr/local/forseti/rec/";
                    if (JUtil.getSesion(request).getSesion("ADM_CFDI").getEspecial().equals("CECAT"))
                        sFichXsd += "CatalogoCuentas_1_1.xsd";
                    else if (JUtil.getSesion(request).getSesion("ADM_CFDI").getEspecial().equals("CEBAL"))
                        sFichXsd += "BalanzaComprobacion_1_1.xsd";
                    else
                        sFichXsd += "PolizasPeriodo_1_1.xsd";
                    try {
                        FileReader file = new FileReader(nomArch);
                        BufferedReader buff = new BufferedReader(file);
                        boolean eof = false;

                        while (!eof) {
                            String line = buff.readLine();
                            if (line == null)
                                eof = true;
                            else
                                archivoXML += line + "\n";
                        }
                        InputStream is = new ByteArrayInputStream(archivoXML.getBytes());
                        Reader xsdReader = new FileReader(sFichXsd);
                        //System.out.println("xmlReader:" + pathxml + "\n" + "xsdReader:" + sFichXsd + "\n"  + "xsdReader2:" + sFichXsd2);
                        SAXParserFactory factory = SAXParserFactory.newInstance();
                        factory.setNamespaceAware(NAME_SPACE_AWARE);
                        factory.setValidating(VALIDATING);

                        SAXParser parser = factory.newSAXParser();
                        parser.setProperty(SCHEMA_LANGUAGE, SCHEMA_LANGUAGE_VAL);
                        parser.setProperty(SCHEMA_SOURCE, new InputSource(xsdReader));

                        DefaultHandler handler = new XmlDefaultHandler();
                        //parser.parse(new InputSource(xmlReader), handler); 
                        parser.parse(is, handler);
                    } catch (FactoryConfigurationError e) {
                        e.printStackTrace();
                        ERROR += "FactoryConfiguration: " + e.getMessage() + "<br>";
                    } catch (ParserConfigurationException e) {
                        e.printStackTrace();
                        ERROR += "ParserConfiguration: " + e.getMessage() + "<br>";
                    } catch (SAXException e) {
                        e.printStackTrace();
                        ERROR += "SAX: " + e.getMessage() + "<br>";
                    } catch (IOException e) {
                        e.printStackTrace();
                        ERROR += "IO: " + e.getMessage() + "<br>";
                    }

                    if (!ERROR.equals("")) {
                        idmensaje = 3;
                        mensaje = "ERROR: Al verificar sintaxis del XML:" + "<br>" + ERROR;
                        getSesion(request).setID_Mensaje(idmensaje, mensaje);
                    } else {
                        idmensaje = 0;
                        mensaje = "El XML se verific y paso las pruebas de sintaxis";
                        getSesion(request).setID_Mensaje(idmensaje, mensaje);
                    }
                    irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                    return;
                } else {
                    idmensaje = 1;
                    mensaje += JUtil.Msj("GLB", "VISTA", "GLB", "SELEC-PROC", 2);
                    getSesion(request).setID_Mensaje(idmensaje, mensaje);
                    irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                    return;
                }
            } else {
                idmensaje = 3;
                mensaje += JUtil.Msj("GLB", "VISTA", "GLB", "SELEC-PROC", 1);
                getSesion(request).setID_Mensaje(idmensaje, mensaje);
                irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                return;
            }
        } else if (request.getParameter("proceso").equals("DESCARGAR_ARCHIVOS")) {
            // Revisa si tiene permisos
            if (!getSesion(request).getPermiso("ADM_CFDI_GCEXML")) {
                idmensaje = 3;
                mensaje += MsjPermisoDenegado(request, "CEF", "ADM_CFDI_GCEXML");
                getSesion(request).setID_Mensaje(idmensaje, mensaje);
                RDP("CEF", getSesion(request).getConBD(), "NA", getSesion(request).getID_Usuario(),
                        "ADM_CFDI_GCEXML", "ACFD||||", mensaje);
                irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                return;
            }

            if (request.getParameter("id") != null) {
                String[] valoresParam = request.getParameterValues("id");
                if (valoresParam.length == 1) {
                    int ano = Integer.parseInt(JUtil.Elm(request.getParameter("id"), 1));
                    int mes = Integer.parseInt(JUtil.Elm(request.getParameter("id"), 2));

                    JContCEModuloSet set = new JContCEModuloSet(request,
                            getSesion(request).getSesion("ADM_CFDI").getEspecial());
                    set.m_Where = "Ano = '" + ano + "' and Mes = '" + mes + "'";
                    set.Open();

                    if (set.getAbsRow(0).getGenerado().equals("N")) {
                        idmensaje = 1;
                        mensaje = "PRECAUCION: No se puede descargar el archivo porque este mes no esta generado aun.";
                        getSesion(request).setID_Mensaje(idmensaje, mensaje);
                        irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                        return;
                    }

                    String nomArchFech;
                    if (mes < 10)
                        nomArchFech = "-" + ano + "-0" + mes;
                    else
                        nomArchFech = "-" + ano + "-" + mes;

                    SAXBuilder builder = new SAXBuilder();
                    File xmlFile = new File("/usr/local/forseti/emp/" + getSesion(request).getBDCompania()
                            + "/CE/" + JUtil.getSesion(request).getSesion("ADM_CFDI").getEspecial()
                            + nomArchFech + ".xml");

                    try {
                        Document document = (Document) builder.build(xmlFile);
                        Element CEGeneral = document.getRootElement();
                        String nomArchZIP = CEGeneral.getAttributeValue("RFC")
                                + CEGeneral.getAttributeValue("Anio") + CEGeneral.getAttributeValue("Mes");
                        String destino = CEGeneral.getAttributeValue("RFC")
                                + CEGeneral.getAttributeValue("Anio") + CEGeneral.getAttributeValue("Mes");
                        if (JUtil.getSesion(request).getSesion("ADM_CFDI").getEspecial().equals("CECAT")) // es de egresos (Compras. Gastos)
                        {
                            nomArchZIP += "CT.zip";
                            destino += "CT.xml";
                        } else if (JUtil.getSesion(request).getSesion("ADM_CFDI").getEspecial()
                                .equals("CEBAL")) {
                            nomArchZIP += "B" + CEGeneral.getAttributeValue("TipoEnvio") + ".zip";
                            destino += "B" + CEGeneral.getAttributeValue("TipoEnvio") + ".xml";
                        } else {
                            nomArchZIP += "PL.zip";
                            destino += "PL.xml";
                        }
                        String nombres[] = { "/usr/local/forseti/emp/" + getSesion(request).getBDCompania()
                                + "/CE/" + JUtil.getSesion(request).getSesion("ADM_CFDI").getEspecial()
                                + nomArchFech + ".xml" };
                        String destinos[] = { destino };
                        JBajarArchivo fd = new JBajarArchivo();
                        fd.doDownloadMultipleFilesInZip(response, getServletConfig().getServletContext(),
                                nomArchZIP, nombres, destinos);
                        return;
                    } catch (JDOMException e) {
                        idmensaje = 1;
                        mensaje = "PRECAUCION: No se puede descargar el archivo porque este mes no esta generado aun.";
                        getSesion(request).setID_Mensaje(idmensaje, mensaje);
                        irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                        return;
                    }
                } else {
                    idmensaje = 1;
                    mensaje += JUtil.Msj("GLB", "VISTA", "GLB", "SELEC-PROC", 2);
                    getSesion(request).setID_Mensaje(idmensaje, mensaje);
                    irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                    return;
                }
            } else {
                idmensaje = 3;
                mensaje += JUtil.Msj("GLB", "VISTA", "GLB", "SELEC-PROC", 1);
                getSesion(request).setID_Mensaje(idmensaje, mensaje);
                irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                return;
            }
        } else if (request.getParameter("proceso").equals("DESENLAZAR_DOCUMENTO")) {
            // Revisa si tiene permisos
            if (!getSesion(request).getPermiso("ADM_CFDI_DESENLAZAR")) {
                idmensaje = 3;
                mensaje += MsjPermisoDenegado(request, "CEF", "ADM_CFDI_DESENLAZAR");
                getSesion(request).setID_Mensaje(idmensaje, mensaje);
                RDP("CEF", getSesion(request).getConBD(), "NA", getSesion(request).getID_Usuario(),
                        "ADM_CFDI_DESENLAZAR", "ACFD||||", mensaje);
                irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                return;
            }

            if (request.getParameter("id") != null) {
                String[] valoresParam = request.getParameterValues("id");
                if (valoresParam.length == 1) {
                    String status = JUtil.getSesion(request).getSesion("ADM_CFDI").getStatus();
                    if (!status.equals("OTROS")) {
                        JCFDCompViewSet set = new JCFDCompViewSet(request, status);
                        set.m_Where = "ID_CFD = '" + p(request.getParameter("id")) + "'";
                        set.Open();

                        if (set.getNumRows() < 1) {
                            idmensaje = 3;
                            mensaje = "ERROR: No se puede desenlazar el registro porque no existe";
                            getSesion(request).setID_Mensaje(idmensaje, mensaje);
                            irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                            return;
                        }

                        if (set.getAbsRow(0).getFSI_Tipo().equals("ENT")) {
                            idmensaje = 1;
                            mensaje = "PRECAUCION: No se puede desenlazar el documento porque aun no se encuentra enlazado a ningn registro.";
                            getSesion(request).setID_Mensaje(idmensaje, mensaje);
                            irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                            return;
                        }
                    } else {
                        JCFDCompOtrSet set = new JCFDCompOtrSet(request);
                        set.m_Where = "ID_CFD = '" + p(request.getParameter("id")) + "'";
                        set.Open();

                        if (set.getAbsRow(0).getFSI_Tipo().equals("ENT")) {
                            idmensaje = 1;
                            mensaje = "PRECAUCION: No se puede desenlazar el documento porque aun no se encuentra enlazado a ningn registro.";
                            getSesion(request).setID_Mensaje(idmensaje, mensaje);
                            irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                            return;
                        }
                    }

                    DesenlazarDocumento(request, response);
                    return;
                } else {
                    idmensaje = 1;
                    mensaje += JUtil.Msj("GLB", "VISTA", "GLB", "SELEC-PROC", 2);
                    getSesion(request).setID_Mensaje(idmensaje, mensaje);
                    irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                    return;
                }
            } else {
                idmensaje = 3;
                mensaje += JUtil.Msj("GLB", "VISTA", "GLB", "SELEC-PROC", 1);
                getSesion(request).setID_Mensaje(idmensaje, mensaje);
                irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                return;
            }
        } else if (request.getParameter("proceso").equals("ELIMINAR_DOCUMENTO")) {
            // Revisa si tiene permisos
            if (!getSesion(request).getPermiso("ADM_CFDI_CARGAR")) {
                idmensaje = 3;
                mensaje += MsjPermisoDenegado(request, "CEF", "ADM_CFDI_CARGAR");
                getSesion(request).setID_Mensaje(idmensaje, mensaje);
                RDP("CEF", getSesion(request).getConBD(), "NA", getSesion(request).getID_Usuario(),
                        "ADM_CFDI_CARGAR", "ACFD||||", mensaje);
                irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                return;
            }

            if (request.getParameter("id") != null) {
                String[] valoresParam = request.getParameterValues("id");
                if (valoresParam.length == 1) {
                    String status = JUtil.getSesion(request).getSesion("ADM_CFDI").getStatus();
                    if (!status.equals("OTROS")) {
                        JCFDCompViewSet set = new JCFDCompViewSet(request, status);
                        set.m_Where = "ID_CFD = '" + p(request.getParameter("id")) + "'";
                        set.Open();

                        if (set.getNumRows() < 1) {
                            idmensaje = 3;
                            mensaje = "ERROR: No se puede eliminar el registro porque no existe";
                            getSesion(request).setID_Mensaje(idmensaje, mensaje);
                            irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                            return;
                        }

                        if (!set.getAbsRow(0).getFSI_Tipo().equals("ENT")) {
                            idmensaje = 1;
                            mensaje = "PRECAUCION: No se puede eliminar el documento porque ya se encuentra enlazado a un registro.";
                            getSesion(request).setID_Mensaje(idmensaje, mensaje);
                            irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                            return;
                        }
                    } else {
                        JCFDCompOtrSet set = new JCFDCompOtrSet(request);
                        set.m_Where = "ID_CFD = '" + p(request.getParameter("id")) + "'";
                        set.Open();

                        if (!set.getAbsRow(0).getFSI_Tipo().equals("ENT")) {
                            idmensaje = 1;
                            mensaje = "PRECAUCION: No se puede eliminar el documento porque ya se encuentra enlazado a un registro.";
                            getSesion(request).setID_Mensaje(idmensaje, mensaje);
                            irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                            return;
                        }
                    }

                    EliminarDocumento(request, response);
                    return;
                } else {
                    idmensaje = 1;
                    mensaje += JUtil.Msj("GLB", "VISTA", "GLB", "SELEC-PROC", 2);
                    getSesion(request).setID_Mensaje(idmensaje, mensaje);
                    irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                    return;
                }
            } else {
                idmensaje = 3;
                mensaje += JUtil.Msj("GLB", "VISTA", "GLB", "SELEC-PROC", 1);
                getSesion(request).setID_Mensaje(idmensaje, mensaje);
                irApag("/forsetiweb/caja_mensajes.jsp", request, response);
                return;
            }
        } else // si no se mandan parametros, manda a error
        {
            idmensaje = 3;
            mensaje += JUtil.Msj("GLB", "VISTA", "GLB", "SELEC-PROC", 3);
            getSesion(request).setID_Mensaje(idmensaje, mensaje);
            irApag("/forsetiweb/caja_mensajes.jsp", request, response);
            return;
        }
    } else {
        idmensaje = 3;
        mensaje += JUtil.Msj("GLB", "VISTA", "GLB", "SELEC-PROC", 3);
        getSesion(request).setID_Mensaje(idmensaje, mensaje);
        irApag("/forsetiweb/caja_mensajes.jsp", request, response);
        return;
    }

}

From source file:nl.nn.adapterframework.pipes.XmlFileElementIteratorPipe.java

protected void iterateInput(Object input, IPipeLineSession session, String correlationID, Map threadContext,
        ItemCallback callback) throws SenderException, TimeOutException {
    InputStream xmlInput;//from  www.ja  v a  2  s  .  c o m
    try {
        xmlInput = new FileInputStream((String) input);
    } catch (FileNotFoundException e) {
        throw new SenderException("could not find file [" + input + "]", e);
    }
    ItemCallbackCallingHandler handler = new ItemCallbackCallingHandler(callback);

    log.debug("obtaining list of elements [" + getElementName() + "] using sax parser");
    try {
        SAXParserFactory parserFactory = XmlUtils.getSAXParserFactory();
        parserFactory.setNamespaceAware(true);
        SAXParser saxParser = parserFactory.newSAXParser();
        saxParser.parse(xmlInput, handler);
    } catch (Exception e) {
        if (handler.getTimeOutException() != null) {
            throw handler.getTimeOutException();
        }
        if (!handler.isStopRequested()) {
            throw new SenderException(
                    "Could not extract list of elements [" + getElementName() + "] using sax parser", e);
        }
    }
}

From source file:nl.nn.adapterframework.processors.InputOutputPipeProcessor.java

public PipeRunResult processPipe(PipeLine pipeLine, IPipe pipe, String messageId, Object message,
        IPipeLineSession pipeLineSession) throws PipeRunException {
    Object preservedObject = message;
    PipeRunResult pipeRunResult = null;/* w  w  w  . j  av a2  s .co m*/
    INamedObject owner = pipeLine.getOwner();

    IExtendedPipe pe = null;

    if (pipe instanceof IExtendedPipe) {
        pe = (IExtendedPipe) pipe;
    }

    if (pe != null) {
        if (StringUtils.isNotEmpty(pe.getGetInputFromSessionKey())) {
            if (log.isDebugEnabled())
                log.debug("Pipeline of adapter [" + owner.getName() + "] replacing input for pipe ["
                        + pe.getName() + "] with contents of sessionKey [" + pe.getGetInputFromSessionKey()
                        + "]");
            message = pipeLineSession.get(pe.getGetInputFromSessionKey());
        }
        if (StringUtils.isNotEmpty(pe.getGetInputFromFixedValue())) {
            if (log.isDebugEnabled())
                log.debug("Pipeline of adapter [" + owner.getName() + "] replacing input for pipe ["
                        + pe.getName() + "] with fixed value [" + pe.getGetInputFromFixedValue() + "]");
            message = pe.getGetInputFromFixedValue();
        }
        if ((message == null || StringUtils.isEmpty(message.toString()))
                && StringUtils.isNotEmpty(pe.getEmptyInputReplacement())) {
            if (log.isDebugEnabled())
                log.debug("Pipeline of adapter [" + owner.getName() + "] replacing empty input for pipe ["
                        + pe.getName() + "] with fixed value [" + pe.getEmptyInputReplacement() + "]");
            message = pe.getEmptyInputReplacement();
        }
    }

    if (pipe instanceof FixedForwardPipe) {
        FixedForwardPipe ffPipe = (FixedForwardPipe) pipe;
        pipeRunResult = ffPipe.doInitialPipe(message, pipeLineSession);
    }

    if (pipeRunResult == null) {
        pipeRunResult = pipeProcessor.processPipe(pipeLine, pipe, messageId, message, pipeLineSession);
    }
    if (pipeRunResult == null) {
        throw new PipeRunException(pipe, "Pipeline of [" + pipeLine.getOwner().getName()
                + "] received null result from pipe [" + pipe.getName() + "]d");
    }

    if (pe != null) {
        if (pe.isRestoreMovedElements()) {
            if (log.isDebugEnabled())
                log.debug("Pipeline of adapter [" + owner.getName()
                        + "] restoring from compacted result for pipe [" + pe.getName() + "]");
            Object result = pipeRunResult.getResult();
            if (result != null) {
                String resultString = (String) result;
                pipeRunResult.setResult(restoreMovedElements(resultString, pipeLineSession));
            }
        }

        if (pe.getChompCharSize() != null || pe.getElementToMove() != null
                || pe.getElementToMoveChain() != null) {
            log.debug("Pipeline of adapter [" + owner.getName() + "] compact received message");
            Object result = pipeRunResult.getResult();
            if (result != null) {
                String resultString = (String) result;
                try {
                    InputStream xmlInput = IOUtils.toInputStream(resultString, "UTF-8");
                    CompactSaxHandler handler = new CompactSaxHandler();
                    handler.setChompCharSize(pe.getChompCharSize());
                    handler.setElementToMove(pe.getElementToMove());
                    handler.setElementToMoveChain(pe.getElementToMoveChain());
                    handler.setElementToMoveSessionKey(pe.getElementToMoveSessionKey());
                    handler.setRemoveCompactMsgNamespaces(pe.isRemoveCompactMsgNamespaces());
                    handler.setContext(pipeLineSession);
                    SAXParserFactory parserFactory = XmlUtils.getSAXParserFactory();
                    parserFactory.setNamespaceAware(true);
                    SAXParser saxParser = parserFactory.newSAXParser();
                    try {
                        saxParser.parse(xmlInput, handler);
                        resultString = handler.getXmlString();
                    } catch (Exception e) {
                        log.warn("Pipeline of adapter [" + owner.getName()
                                + "] could not compact received message: " + e.getMessage());
                    }
                    handler = null;
                } catch (Exception e) {
                    throw new PipeRunException(pipe,
                            "Pipeline of [" + pipeLine.getOwner().getName()
                                    + "] got error during compacting received message to more compact format: "
                                    + e.getMessage());
                }
                pipeRunResult.setResult(resultString);
            }
        }

        if (StringUtils.isNotEmpty(pe.getStoreResultInSessionKey())) {
            if (log.isDebugEnabled())
                log.debug("Pipeline of adapter [" + owner.getName() + "] storing result for pipe ["
                        + pe.getName() + "] under sessionKey [" + pe.getStoreResultInSessionKey() + "]");
            Object result = pipeRunResult.getResult();
            pipeLineSession.put(pe.getStoreResultInSessionKey(), result);
        }
        if (pe.isPreserveInput()) {
            pipeRunResult.setResult(preservedObject);
        }
    }

    if (pe != null) {
        if (secLogEnabled && pe.isWriteToSecLog()) {
            String secLogMsg = "adapter [" + owner.getName() + "] pipe [" + pe.getName() + "]";
            if (pe.getSecLogSessionKeys() != null) {
                String sk = "";
                StringTokenizer st = new StringTokenizer(pe.getSecLogSessionKeys(), " ,;");
                while (st.hasMoreTokens()) {
                    if (sk.length() > 0) {
                        sk = sk + ",";
                    }
                    String key = st.nextToken();
                    Object value = pipeLineSession.get(key);
                    sk = sk + key + "=" + value;
                }
                secLogMsg = secLogMsg + " sessionKeys [" + sk + "]";
            }
            secLog.info(secLogMsg);
        }
    }

    return pipeRunResult;
}

From source file:nl.nn.adapterframework.receivers.ReceiverBase.java

private String processMessageInAdapter(IListener origin, Object rawMessage, String message, String messageId,
        String technicalCorrelationId, Map threadContext, long waitingDuration, boolean manualRetry)
        throws ListenerException {
    String result = null;/*from  ww w .  j  ava 2s .  c  o  m*/
    PipeLineResult pipeLineResult = null;
    long startProcessingTimestamp = System.currentTimeMillis();
    //      if (message==null) {
    //         requestSizeStatistics.addValue(0);
    //      } else {
    //         requestSizeStatistics.addValue(message.length());
    //      }
    lastMessageDate = startProcessingTimestamp;
    log.debug(getLogPrefix() + "received message with messageId [" + messageId + "] (technical) correlationId ["
            + technicalCorrelationId + "]");

    if (StringUtils.isEmpty(messageId)) {
        messageId = Misc.createSimpleUUID();
        if (log.isDebugEnabled())
            log.debug(getLogPrefix() + "generated messageId [" + messageId + "]");
    }

    if (getChompCharSize() != null || getElementToMove() != null || getElementToMoveChain() != null) {
        log.debug(getLogPrefix() + "compact received message");
        try {
            InputStream xmlInput = IOUtils.toInputStream(message, "UTF-8");
            CompactSaxHandler handler = new CompactSaxHandler();
            handler.setChompCharSize(getChompCharSize());
            handler.setElementToMove(getElementToMove());
            handler.setElementToMoveChain(getElementToMoveChain());
            handler.setElementToMoveSessionKey(getElementToMoveSessionKey());
            handler.setRemoveCompactMsgNamespaces(isRemoveCompactMsgNamespaces());
            if (threadContext != null) {
                handler.setContext(threadContext);
            }
            SAXParserFactory parserFactory = XmlUtils.getSAXParserFactory();
            parserFactory.setNamespaceAware(true);
            SAXParser saxParser = parserFactory.newSAXParser();
            try {
                saxParser.parse(xmlInput, handler);
                message = handler.getXmlString();
            } catch (Exception e) {
                warn("received message could not be compacted: " + e.getMessage());
            }
            handler = null;
        } catch (Exception e) {
            throw new ListenerException(
                    "error during compacting received message to more compact format: " + e.getMessage());
        }
    }

    String businessCorrelationId = null;
    if (correlationIDTp != null) {
        try {
            businessCorrelationId = correlationIDTp.transform(message, null);
        } catch (Exception e) {
            //throw new ListenerException(getLogPrefix()+"could not extract businessCorrelationId",e);
            log.warn(getLogPrefix() + "could not extract businessCorrelationId");
        }
        if (StringUtils.isEmpty(businessCorrelationId)) {
            String cidText;
            if (StringUtils.isNotEmpty(getCorrelationIDXPath())) {
                cidText = "xpathExpression [" + getCorrelationIDXPath() + "]";
            } else {
                cidText = "styleSheet [" + getCorrelationIDStyleSheet() + "]";
            }
            if (StringUtils.isNotEmpty(technicalCorrelationId)) {
                log.info(getLogPrefix() + "did not find correlationId using " + cidText
                        + ", reverting to correlationId of transfer [" + technicalCorrelationId + "]");
                businessCorrelationId = technicalCorrelationId;
            }
        }
    } else {
        businessCorrelationId = technicalCorrelationId;
    }
    if (StringUtils.isEmpty(businessCorrelationId)) {
        if (StringUtils.isNotEmpty(messageId)) {
            log.info(getLogPrefix() + "did not find (technical) correlationId, reverting to messageId ["
                    + messageId + "]");
            businessCorrelationId = messageId;
        }
    }
    log.info(getLogPrefix() + "messageId [" + messageId + "] technicalCorrelationId [" + technicalCorrelationId
            + "] businessCorrelationId [" + businessCorrelationId + "]");
    threadContext.put(IPipeLineSession.businessCorrelationIdKey, businessCorrelationId);
    String label = null;
    if (labelTp != null) {
        try {
            label = labelTp.transform(message, null);
        } catch (Exception e) {
            //throw new ListenerException(getLogPrefix()+"could not extract label",e);
            log.warn(getLogPrefix() + "could not extract label: (" + ClassUtils.nameOf(e) + ") "
                    + e.getMessage());
        }
    }
    if (hasProblematicHistory(messageId, manualRetry, rawMessage, message, threadContext,
            businessCorrelationId)) {
        if (!isTransacted()) {
            log.warn(getLogPrefix() + "received message with messageId [" + messageId
                    + "] which has a problematic history; aborting processing");
        }
        numRejected.increase();
        return result;
    }
    if (isDuplicateAndSkip(getMessageLog(), messageId, businessCorrelationId)) {
        numRejected.increase();
        return result;
    }
    if (getCachedProcessResult(messageId) != null) {
        numRetried.increase();
    }

    int txOption = this.getTransactionAttributeNum();
    TransactionDefinition txDef = SpringTxManagerProxy.getTransactionDefinition(txOption,
            getTransactionTimeout());
    //TransactionStatus txStatus = txManager.getTransaction(txDef);
    IbisTransaction itx = new IbisTransaction(txManager, txDef, "receiver [" + getName() + "]");
    TransactionStatus txStatus = itx.getStatus();

    // update processing statistics
    // count in processing statistics includes messages that are rolled back to input
    startProcessingMessage(waitingDuration);

    IPipeLineSession pipelineSession = null;
    String errorMessage = "";
    boolean messageInError = false;
    try {
        String pipelineMessage;
        if (origin instanceof IBulkDataListener) {
            try {
                IBulkDataListener bdl = (IBulkDataListener) origin;
                pipelineMessage = bdl.retrieveBulkData(rawMessage, message, threadContext);
            } catch (Throwable t) {
                errorMessage = t.getMessage();
                messageInError = true;
                ListenerException l = wrapExceptionAsListenerException(t);
                throw l;
            }
        } else {
            pipelineMessage = message;
        }

        numReceived.increase();
        // Note: errorMessage is used to pass value from catch-clause to finally-clause!
        pipelineSession = createProcessingContext(businessCorrelationId, threadContext, messageId);
        //         threadContext=pipelineSession; // this is to enable Listeners to use session variables, for instance in afterProcessMessage()
        try {
            if (getMessageLog() != null) {
                getMessageLog().storeMessage(messageId, businessCorrelationId, new Date(),
                        RCV_MESSAGE_LOG_COMMENTS, label, pipelineMessage);
            }
            log.debug(getLogPrefix() + "preparing TimeoutGuard");
            TimeoutGuard tg = new TimeoutGuard("Receiver " + getName());
            try {
                if (log.isDebugEnabled())
                    log.debug(getLogPrefix() + "activating TimeoutGuard with transactionTimeout ["
                            + transactionTimeout + "]s");
                tg.activateGuard(getTransactionTimeout());
                pipeLineResult = adapter.processMessageWithExceptions(businessCorrelationId, pipelineMessage,
                        pipelineSession);
                pipelineSession.put("exitcode", "" + pipeLineResult.getExitCode());
                result = pipeLineResult.getResult();
                errorMessage = "exitState [" + pipeLineResult.getState() + "], result [" + result + "]";
                if (log.isDebugEnabled()) {
                    log.debug(getLogPrefix() + "received result: " + errorMessage);
                }
                messageInError = txStatus.isRollbackOnly();
            } finally {
                log.debug(getLogPrefix() + "canceling TimeoutGuard, isInterrupted ["
                        + Thread.currentThread().isInterrupted() + "]");
                if (tg.cancel()) {
                    errorMessage = "timeout exceeded";
                    if (StringUtils.isEmpty(result)) {
                        result = "<timeout/>";
                    }
                    messageInError = true;
                }
            }
            if (!messageInError && !isTransacted()) {
                String commitOnState = ((Adapter) adapter).getPipeLine().getCommitOnState();

                if (StringUtils.isNotEmpty(commitOnState)
                        && !commitOnState.equalsIgnoreCase(pipeLineResult.getState())) {
                    messageInError = true;
                }
            }
        } catch (Throwable t) {
            if (TransactionSynchronizationManager.isActualTransactionActive()) {
                log.debug("<*>" + getLogPrefix() + "TX Update: Received failure, transaction "
                        + (txStatus.isRollbackOnly() ? "already" : "not yet") + " marked for rollback-only");
            }
            errorMessage = t.getMessage();
            messageInError = true;
            if (pipeLineResult == null) {
                pipeLineResult = new PipeLineResult();
            }
            if (StringUtils.isEmpty(pipeLineResult.getResult())) {
                String formattedErrorMessage = adapter.formatErrorMessage("exception caught", t, message,
                        messageId, this, startProcessingTimestamp);
                pipeLineResult.setResult(formattedErrorMessage);
            }
            ListenerException l = wrapExceptionAsListenerException(t);
            throw l;
        } finally {
            putSessionKeysIntoThreadContext(threadContext, pipelineSession);
        }
        //         if (result==null) {
        //            responseSizeStatistics.addValue(0);
        //         } else {
        //            responseSizeStatistics.addValue(result.length());
        //         }
        if (getSender() != null) {
            String sendMsg = sendResultToSender(technicalCorrelationId, result);
            if (sendMsg != null) {
                errorMessage = sendMsg;
            }
        }
    } finally {
        cacheProcessResult(messageId, businessCorrelationId, errorMessage, new Date(startProcessingTimestamp));
        if (!isTransacted() && messageInError) {
            if (!manualRetry) {
                moveInProcessToError(messageId, businessCorrelationId, message,
                        new Date(startProcessingTimestamp), errorMessage, rawMessage, TXNEW_CTRL);
            }
        }
        try {
            Map afterMessageProcessedMap;
            if (threadContext != null) {
                afterMessageProcessedMap = threadContext;
                if (pipelineSession != null) {
                    threadContext.putAll(pipelineSession);
                }
            } else {
                afterMessageProcessedMap = pipelineSession;
            }
            origin.afterMessageProcessed(pipeLineResult, rawMessage, afterMessageProcessedMap);
        } finally {
            long finishProcessingTimestamp = System.currentTimeMillis();
            finishProcessingMessage(finishProcessingTimestamp - startProcessingTimestamp);
            if (!txStatus.isCompleted()) {
                // NB: Spring will take care of executing a commit or a rollback;
                // Spring will also ONLY commit the transaction if it was newly created
                // by the above call to txManager.getTransaction().
                //txManager.commit(txStatus);
                itx.commit();
            } else {
                throw new ListenerException(
                        getLogPrefix() + "Transaction already completed; we didn't expect this");
            }
        }
    }
    if (log.isDebugEnabled())
        log.debug(getLogPrefix() + "messageId [" + messageId + "] correlationId [" + businessCorrelationId
                + "] returning result [" + result + "]");
    return result;
}

From source file:nl.nn.adapterframework.util.XmlUtils.java

public static synchronized SAXParserFactory getSAXParserFactory(boolean namespaceAware) {
    SAXParserFactory factory = new org.apache.xerces.jaxp.SAXParserFactoryImpl();
    factory.setNamespaceAware(namespaceAware);
    return factory;
}