“对于使用Perl和Shell脚本的”-C“选项”错误太晚了

时间:2017-08-14 13:48:40

标签: java html xml perl nlp

我有一个jar应用程序,它有几个函数,其中一个是从HTML转换为XML。当我尝试运行一个简单的命令,例如:

java -jar lt4el-cmd.jar send -l en "l2:https://en.wikipedia.org/wiki/Personal_computer"

我收到以下错误:

ERROR [Thread-1]: html2base/html2base-wrapper.sh: Too late for "-C" option at html2base/html2xml.pl line 1.
/tmp/lpc.30872.html: failed 
cat: /tmp/lpc.30872.xml: No such file or directory
 (LpcControl.java:229)
ERROR [Thread-1]: ana2ont/ana2ont.sh ${lang}: -:1: parser error : Document is empty
-:1: parser error : Start tag expected, '<' not found
Tokenization/tagging failed
^
-:1: parser error : Document is empty
unable to parse -
-:1: parser error : Document is empty
unable to parse -
 (LpcControl.java:229)
ERROR [Thread-1]: Error in conversion: Error running conversion script (ana2ont/ana2ont.sh ${lang}): 6 (AppInterface.java:159)

这是html2base-wrapper.sh脚本,似乎是第一个错误发生的地方。

#!/bin/bash

if [ "$1" == "check" ]; then
    . common.sh
    check_binary perl || exit 1
    check_perl_module HTML::TreeBuilder || exit 1
    check_perl_module XML::LibXML || exit 1
    check_binary tidy || exit 1
    check_binary xmllint || exit 1
    check_binary xsltproc || exit 1
    exit
fi

cat >"$TMPDIR/lpc.$$.html"
html2base/html2base.sh -d html2base/LT4ELBase.dtd -x html2base/LT4ELBase.xslt -t "$TMPDIR/lpc.$$.html" >&2
cat "$TMPDIR/lpc.$$.xml";
rm -f "$TMPDIR"/lpc.$$.{ht,x}ml

html2base.sh脚本:

#!/bin/bash
#
# Sample script for automated HTML -> XML conversion
#
# Miroslav Spousta <spousta@ufal.mff.cuni.cz>
# $Id: html2base.sh 462 2008-03-17 08:37:14Z qiq $

basedir=`dirname $0`;

# constants
HTML2XML_BIN=${basedir}/html2xml.pl
ICONV_BIN=iconv
TIDY_BIN=tidy
XMLLINT_BIN=xmllint
XSLTPROC_BIN=xsltproc
DTDPARSE_BIN=dtdparse
TMPDIR=/tmp

# default values
VERBOSE=0
ENCODING=
TIDY=0
VALIDATE=0
DTD=${basedir}/LT4ELBase.dtd
XSLT=${basedir}/LT4ELBase.xslt

usage()
{
        echo "usage: html2base.sh [options] file(s)"
        echo "XML -> HTML conversion script."
        echo
        echo "  -e, --encoding=charset  Convert input files from encoding to UTF-8 (none)"
        echo "  -d, --dtd=file      DTD to be used for conversion and validation ($DTD)"
        echo "  -x, --xslt=file     XSLT to be applied after conversion ($XSLT)"
        echo "  -t, --tidy      Run HTMLTidy on input HTML files"
        echo "  -a, --validate      Validate output XML files"
        echo "  -v, --verbose       Be verbose"
        echo "  -h, --help      Print this usage"
    exit 1;
}

OPTIONS=`getopt -o e:d:x:tahv -l encoding:,dtd:,xlst,tidy,validate,verbose,help -n 'convert.sh' -- "$@"`
if [ $? != 0 ]; then
    usage;
fi
eval set -- "$OPTIONS"
while true ; do
    case "$1" in
    -e | --encoding)    ENCODING=$2; shift 2 ;;
    -d | --dtd)     DTD=$2; shift 2 ;;
    -x | --xslt)        XSLT=$2; shift 2 ;;
    -t | --tidy)        TIDY=1; shift 1;;
    -a | --validate)    VALIDATE=1; shift 1;;
    -v | --verbose)     VERBOSE=1; shift 1 ;;
    -h | --help)        usage; shift 1 ;;
    --) shift ; break ;;
    *) echo "Internal error!" ; echo $1; exit 1 ;;
    esac
done

if [ $# -eq 0 ]; then
    usage;
fi

DTD_XML=`echo "$DTD"|sed -e 's/\.dtd/.xml/'`
if [ "$VERBOSE" -eq 1 ]; then
    VERBOSE=--verbose
else
    VERBOSE=
fi

# create $DTD_XML if necessary
if [ ! -f "$DTD_XML" ]; then
    if ! $DTDPARSE_BIN $DTD -o $DTD_XML 2>/dev/null; then
        echo "cannot run dtdparse, cannot create $DTD_XML";
        exit 1;
    fi;
fi

# process file by file

total=0
nok=0
while [ -n "$1" ]; do
    file=$1;
    if [ -n "$VERBOSE" ]; then
        echo "Processing $file..."
    fi
    f="$file";
    result=0;
    if [ -n "$ENCODING" ]; then
        $ICONV_BIN -f "$ENCODING" -t utf-8 "$f" -o "$file.xtmp"
        result=$?
        error="encoding error"
        f=$file.xtmp
    fi
    if [ "$result" -eq 0 ]; then
        if [ "$TIDY" = '1' ]; then
            $TIDY_BIN --force-output 1 -q -utf8 >"$file.xtmp2" "$f" 2>/dev/null
            f=$file.xtmp2
        fi
        out=`echo $file|sed -e 's/\.x\?html\?$/.xml/'`
        if [ "$out" = "$file" ]; then
            out="$out.xml"
        fi
        $HTML2XML_BIN --simplify-ws $VERBOSE $DTD_XML -o "$out" "$f"
        result=$?
        error="failed"
    fi
    if [ "$result" -eq 0 ]; then
        $XSLTPROC_BIN --path `dirname $DTD` $XSLT "$out" |$XMLLINT_BIN --noblanks --format -o "$out.tmp1" -
        result=$?
        error="failed"
        mv "$out.tmp1" "$out"
        if [ "$result" -eq 0 -a "$VALIDATE" = '1' ]; then
            tmp=`dirname $file`/$DTD
            delete=0
            if [ ! -f $tmp ]; then
                cp $DTD $tmp
                delete=1
            fi
            $XMLLINT_BIN --path `dirname $DTD` --valid --noout "$out"
            result=$?
            error="validation error"
            if [ "$delete" -eq 1 ]; then
                rm -f $tmp
            fi
        fi
    fi
    if [ "$result" -eq 0 ]; then
        if [ -n "$VERBOSE" ]; then
            echo "OK"
        fi
    else
        echo "$file: $error "
        nok=`expr $nok + 1`
    fi
    total=`expr $total + 1`
    rm -f $file.xtmp $file.xtmp2
    shift;
done
if [ -n "$VERBOSE" ]; then
    echo
    echo "Total: $total, failed: $nok"
fi

html2xml.pl文件的开头部分:

#!/usr/bin/perl -W -C

# Simple HTML to XML (subset of XHTML) conversion tool. Should always produce a
# valid XML file according to the output DTD file specified.
#
# Miroslav Spousta <spousta@ufal.mff.cuni.cz>
# $Id: html2xml.pl 461 2008-03-09 09:49:42Z qiq $

use HTML::TreeBuilder;
use HTML::Element;
use HTML::Entities;
use XML::LibXML;
use Getopt::Long;
use Data::Dumper;
use strict;

我似乎无法确定问题所在。 ERROR [Thread-1]究竟是什么意思? 感谢

2 个答案:

答案 0 :(得分:1)

错误来自于在Perl脚本的shebang(manifest.json)行上-C,但没有将#!传递给-C。当有人

时会发生这种类型的错误
perl

而不是

perl html2base/html2xml.pl ...

答案 1 :(得分:0)

错误来自html2xml.pl脚本,正如其他用户正确提到的那样。我正在运行ubuntu 16.04.2系统,它带有默认的perl 5.22版本。正如post提到的那样,使用-C行上的perl 5.10.1选项(来自#!)需要您在执行时在命令行中指定它,我不知道该怎么办,因为我正在运行一个jar文件。我安装了perlbrew,而不是我用来获取perl的早期版本并将我的perl脚本修改为:

#!/usr/bin/path/to/perlbrew/perl -W -C

# Simple HTML to XML (subset of XHTML) conversion tool. Should always produce a
# valid XML file according to the output DTD file specified.
#
# Miroslav Spousta <spousta@ufal.mff.cuni.cz>
# $Id: html2xml.pl 461 2008-03-09 09:49:42Z qiq $
在使用perlbrew时,

This在设置shell脚本时也会派上用场。

感谢您的贡献。