Search in sources :

Example 6 with CaptionConverterException

use of org.opencastproject.caption.api.CaptionConverterException in project opencast by opencast.

the class CaptionServiceImpl method importCaptions.

/**
 * Imports captions using registered converter engine and specified language.
 *
 * @param input
 *          file containing captions
 * @param inputFormat
 *          format of imported captions
 * @param language
 *          (optional) captions' language
 * @return {@link List} of parsed captions
 * @throws UnsupportedCaptionFormatException
 *           if there is no registered engine for given format
 * @throws IllegalCaptionFormatException
 *           if parser encounters exception
 */
private List<Caption> importCaptions(File input, String inputFormat, String language) throws UnsupportedCaptionFormatException, CaptionConverterException {
    // get input format
    CaptionConverter converter = getCaptionConverter(inputFormat);
    if (converter == null) {
        logger.error("No available caption format found for {}.", inputFormat);
        throw new UnsupportedCaptionFormatException(inputFormat);
    }
    FileInputStream fileStream = null;
    try {
        fileStream = new FileInputStream(input);
        List<Caption> collection = converter.importCaption(fileStream, language);
        return collection;
    } catch (FileNotFoundException e) {
        throw new CaptionConverterException("Could not locate file " + input);
    } finally {
        IOUtils.closeQuietly(fileStream);
    }
}
Also used : CaptionConverterException(org.opencastproject.caption.api.CaptionConverterException) UnsupportedCaptionFormatException(org.opencastproject.caption.api.UnsupportedCaptionFormatException) FileNotFoundException(java.io.FileNotFoundException) CaptionConverter(org.opencastproject.caption.api.CaptionConverter) FileInputStream(java.io.FileInputStream) Caption(org.opencastproject.caption.api.Caption)

Example 7 with CaptionConverterException

use of org.opencastproject.caption.api.CaptionConverterException in project opencast by opencast.

the class DFXPCaptionConverter method importCaption.

/**
 * {@inheritDoc} Parser used for parsing XML document is DOM parser. Language parameter will determine which language
 * is searched for and parsed. If there is no matching language, empty collection is returned. If language parameter
 * is <code>null</code> first language found is parsed.
 *
 * @see org.opencastproject.caption.api.CaptionConverter#importCaption(java.io.InputStream, java.lang.String)
 */
@Override
public List<Caption> importCaption(InputStream in, String language) throws CaptionConverterException {
    // create new collection
    List<Caption> collection = new ArrayList<Caption>();
    Document doc;
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        doc = builder.parse(in);
        doc.getDocumentElement().normalize();
    } catch (ParserConfigurationException e) {
        throw new CaptionConverterException("Could not parse captions", e);
    } catch (SAXException e) {
        throw new CaptionConverterException("Could not parse captions", e);
    } catch (IOException e) {
        throw new CaptionConverterException("Could not parse captions", e);
    }
    // get all <div> elements since they contain information about language
    NodeList divElements = doc.getElementsByTagName("div");
    Element targetDiv = null;
    if (language != null) {
        // find first <div> element with matching language
        for (int i = 0; i < divElements.getLength(); i++) {
            Element n = (Element) divElements.item(i);
            if (n.getAttribute("xml:lang").equals(language)) {
                targetDiv = n;
                break;
            }
        }
    } else {
        if (divElements.getLength() > 1) {
            // more than one existing <div> element, no language specified
            logger.warn("More than one <div> element available. Parsing first one...");
        }
        if (divElements.getLength() != 0) {
            targetDiv = (Element) divElements.item(0);
        }
    }
    // check if we found node
    if (targetDiv == null) {
        logger.warn("No suitable <div> element found for language {}", language);
    } else {
        NodeList pElements = targetDiv.getElementsByTagName("p");
        // initialize start time
        Time time = null;
        try {
            time = new TimeImpl(0, 0, 0, 0);
        } catch (IllegalTimeFormatException e1) {
        }
        for (int i = 0; i < pElements.getLength(); i++) {
            try {
                Caption caption = parsePElement((Element) pElements.item(i));
                // check time
                if (caption.getStartTime().compareTo(time) < 0 || caption.getStopTime().compareTo(caption.getStartTime()) <= 0) {
                    logger.warn("Caption with invalid time encountered. Skipping...");
                    continue;
                }
                collection.add(caption);
            } catch (IllegalTimeFormatException e) {
                logger.warn("Caption with invalid time format encountered. Skipping...");
            }
        }
    }
    // return collection
    return collection;
}
Also used : CaptionConverterException(org.opencastproject.caption.api.CaptionConverterException) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) IllegalTimeFormatException(org.opencastproject.caption.api.IllegalTimeFormatException) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) ArrayList(java.util.ArrayList) Time(org.opencastproject.caption.api.Time) IOException(java.io.IOException) Document(org.w3c.dom.Document) Caption(org.opencastproject.caption.api.Caption) TimeImpl(org.opencastproject.caption.impl.TimeImpl) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 8 with CaptionConverterException

use of org.opencastproject.caption.api.CaptionConverterException in project opencast by opencast.

the class DFXPCaptionConverter method getLanguageList.

/**
 * {@inheritDoc} Uses SAX parser to quickly read the document and retrieve available languages.
 *
 * @see org.opencastproject.caption.api.CaptionConverter#getLanguageList(java.io.InputStream)
 */
@Override
public String[] getLanguageList(InputStream input) throws CaptionConverterException {
    // create lang list
    final List<String> langList = new LinkedList<String>();
    // get SAX parser
    SAXParserFactory factory = SAXParserFactory.newInstance();
    try {
        SAXParser parser = factory.newSAXParser();
        // create handler
        DefaultHandler handler = new DefaultHandler() {

            @Override
            public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
                if ("div".equals(qName)) {
                    // we found div tag - let's make a lookup for language
                    String lang = attributes.getValue("xml:lang");
                    if (lang == null) {
                        // should never happen
                        logger.warn("Missing xml:lang attribute for div element.");
                    } else if (langList.contains(lang)) {
                        logger.warn("Multiple div elements with same language.");
                    } else {
                        langList.add(lang);
                    }
                }
            }
        };
        // parse stream
        parser.parse(input, handler);
    } catch (ParserConfigurationException e) {
        // should not happen
        throw new RuntimeException(e);
    } catch (SAXException e) {
        throw new CaptionConverterException("Could not parse captions", e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return langList.toArray(new String[0]);
}
Also used : CaptionConverterException(org.opencastproject.caption.api.CaptionConverterException) Attributes(org.xml.sax.Attributes) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) LinkedList(java.util.LinkedList) SAXParserFactory(javax.xml.parsers.SAXParserFactory) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException)

Example 9 with CaptionConverterException

use of org.opencastproject.caption.api.CaptionConverterException in project opencast by opencast.

the class CaptionServiceRemoteImpl method getLanguageList.

/**
 * @see org.opencastproject.caption.api.CaptionService#getLanguageList(MediaPackageElement, String)
 */
@Override
public String[] getLanguageList(MediaPackageElement input, String format) throws UnsupportedCaptionFormatException, CaptionConverterException {
    HttpPost post = new HttpPost("/languages");
    try {
        List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
        params.add(new BasicNameValuePair("captions", MediaPackageElementParser.getAsXml(input)));
        params.add(new BasicNameValuePair("input", format));
        post.setEntity(new UrlEncodedFormEntity(params));
    } catch (Exception e) {
        throw new CaptionConverterException(e);
    }
    HttpResponse response = null;
    try {
        response = getResponse(post);
        if (response != null) {
            List<String> langauges = new ArrayList<String>();
            Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(EntityUtils.toString(response.getEntity(), "UTF-8"));
            NodeList languages = doc.getElementsByTagName("languages");
            for (int i = 0; i < languages.getLength(); i++) {
                Node item = languages.item(i);
                langauges.add(item.getTextContent());
            }
            logger.info("Catalog languages received from remote caption service");
            return langauges.toArray(new String[langauges.size()]);
        }
    } catch (Exception e) {
        throw new CaptionConverterException("Unable to get catalog languages " + input + " using a remote caption service", e);
    } finally {
        closeConnection(response);
    }
    throw new CaptionConverterException("Unable to get catalog languages" + input + " using a remote caption service");
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) CaptionConverterException(org.opencastproject.caption.api.CaptionConverterException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) Document(org.w3c.dom.Document) CaptionConverterException(org.opencastproject.caption.api.CaptionConverterException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) UnsupportedCaptionFormatException(org.opencastproject.caption.api.UnsupportedCaptionFormatException) BasicNameValuePair(org.apache.http.message.BasicNameValuePair)

Aggregations

CaptionConverterException (org.opencastproject.caption.api.CaptionConverterException)9 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 Caption (org.opencastproject.caption.api.Caption)5 UnsupportedCaptionFormatException (org.opencastproject.caption.api.UnsupportedCaptionFormatException)5 FileNotFoundException (java.io.FileNotFoundException)3 CaptionConverter (org.opencastproject.caption.api.CaptionConverter)3 IllegalTimeFormatException (org.opencastproject.caption.api.IllegalTimeFormatException)3 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)3 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 HttpResponse (org.apache.http.HttpResponse)2 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)2 HttpPost (org.apache.http.client.methods.HttpPost)2 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)2 Time (org.opencastproject.caption.api.Time)2 CaptionImpl (org.opencastproject.caption.impl.CaptionImpl)2 TimeImpl (org.opencastproject.caption.impl.TimeImpl)2 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)2