1

我正在尝试使用 Java 中的 JiBX 解析 spdx 文件。我只想解析一些元素,而不是文件中的所有元素,而不是按顺序解析,并且某些元素重复。我要解析的一些元素是:<creator>、<packageDownloadLocation> 或<checksumValue>。我遇到的问题是,解析后对象的属性为null。对象本身不为空。

你能帮我么?

绑定 XML 文件是:

<binding>
    <mapping name="RDF" class="com.transfer.SpdxTransferObject" ordered="false" flexible="true">
        <namespace uri="http://www.w3.org/1999/02/22-rdf-syntax-ns#" prefix="rdf" default="all"/>
        <namespace uri="http://usefulinc.com/ns/doap#" prefix="j.0" />
        <namespace uri ="http://www.w3.org/2000/01/rdf-schema#" prefix="rdfs" />
        <namespace uri="http://spdx.org/rdf/terms#" prefix="noPrefix"/>
            <value name="creator" field="mCreator" get-method="getCreator"  set-method="setCreator" default="none"/>
</mapping>

SpdxTransferObject 是一个带有 setter 和 getter 的简单 Java bean:

public class SpdxTO implements Serializable
{

    /** Default serial version ID */
    private static final long serialVersionUID = 1L;

    private String mCreator;

    private String mPackageDownloadLocation;

    private String mChecksumValue>;

    /* getters and setters */
    ......
}

Java代码是:

public class ParseSpdxFile
{
    /** Our logger */
    private static Log sLog = LoggerFactory.create();

    public static SpdxTransferObject parseFromFile(final File pFile) throws ValidationException
    {
        sLog.debug("Parsing the Spdx file");
        SpdxTO data = null;
        JiBXFileHandler handler;
        try
        {
            handler = new JiBXFileHandler(BindingDirectory.getFactory(SpdxTransferObject.class));
            data = (SpdxTransferObject) handler.loadFromXML(pFile);
        }
        catch (JiBXException e)
        {
            throw new ValidationException(e);
        }
        catch (Exception e)
        {
            throw new ValidationException(e);
        }
        return data;
    }
}

public class JiBXFileHandler
{
    private final IBindingFactory mBfact;

    public JiBXFileHandler(final IBindingFactory pBfact)
    {
        mBfact = pBfact;
    }

    public Object loadFromXML(final File pFile) throws Exception
    {

        FileInputStream in = null;
        try
        {
            IUnmarshallingContext uctx = mBfact.createUnmarshallingContext();
            in = new FileInputStream(pFile);
            return uctx.unmarshalDocument(in, null);
        }
        finally
        {
            IOUtils.closeQuietly(in);
        }
    }
}

spdx 文件如下:

<rdf:RDF
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:j.0="http://usefulinc.com/ns/doap#"
  xmlns="http://spdx.org/rdf/terms#"
  xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
  <SpdxDocument rdf:about="http://www.spdx.org/tools#SPDXANALYSIS">
    <reviewed>
      <Review>
        <rdfs:comment>This is just an example.</rdfs:comment>
        <reviewDate>2010-02-10T00:00:00Z</reviewDate>
        <reviewer>Person: Joe Reviewer</reviewer>
      </Review>
    </reviewed>
    <hasExtractedLicensingInfo>
      <ExtractedLicensingInfo rdf:nodeID="A0">
        <licenseText>license text</licenseText>
        <licenseId>LicenseRef-3</licenseId>
      </ExtractedLicensingInfo>
    </hasExtractedLicensingInfo>
    <specVersion>SPDX-0.8</specVersion>
    <describesPackage>
      <Package rdf:about="http://www.spdx.org/tools#SPDXANALYSIS?package">
        <licenseDeclared>
          <ConjunctiveLicenseSet>
            <member>
              <License rdf:about="http://spdx.org/licenses/MPL-1.1">
                <licenseId>MPL-1.1</licenseId>
              </License>
            </member>
            <member rdf:nodeID="A0"/>
            <member>
              <ExtractedLicensingInfo rdf:nodeID="A1">
                <licenseText>License text 2</licenseText>
                <licenseId>LicenseRef-2</licenseId>
              </ExtractedLicensingInfo>
            </member>
            <member>
              <ExtractedLicensingInfo rdf:nodeID="A2">
                <licenseText>license text 3</licenseText>
                <licenseId>LicenseRef-4</licenseId>
              </ExtractedLicensingInfo>
            </member>
            <member>
              <ExtractedLicensingInfo rdf:nodeID="A3">
                <licenseText>licence text 4</licenseText>
                <licenseId>LicenseRef-1</licenseId>
              </ExtractedLicensingInfo>
            </member>
            <member>
              <License rdf:about="http://spdx.org/licenses/Apache-2">
                <licenseId>Apache-2</licenseId>
              </License>
            </member>
          </ConjunctiveLicenseSet>
        </licenseDeclared>
        <packageDownloadLocation>http://www.spdx.org/tools</packageDownloadLocation>
        <hasFile>
          <File>
            <copyrightText>Copyright 2010, 2011</copyrightText>
            <licenseComments></licenseComments>
            <licenseInfoInFile rdf:resource="http://spdx.org/licenses/Apache-2"/>
            <licenseConcluded rdf:resource="http://spdx.org/licenses/Apache-2"/>
            <fileType>SOURCE</fileType>
            <checksum>
              <Checksum>
                <checksumValue>2fd4e1c67a2d28fced849ee1bb76e7391b93eb12</checksumValue>
                <algorithm>SHA1</algorithm>
              </Checksum>
            </checksum>
            <fileName>src/org/spdx/parser/Project.java</fileName>
          </File>
        </hasFile>
        <licenseInfoFromFiles rdf:nodeID="A3"/>
        <checksum>
          <Checksum>
            <checksumValue>2fd4e1c67a2d28fced849ee1bb76e7391b93eb12</checksumValue>
            <algorithm>SHA1</algorithm>
          </Checksum>
        </checksum>
        <sourceInfo>Version 1.0 of the SPDX Translator application</sourceInfo>
        <packageVerificationCode>
          <PackageVerificationCode>
            <packageVerificationCodeValue>4e3211c67a2d28fced849ee1bb76e7391b93feba</packageVerificationCodeValue>
            <packageVerificationCodeExcludedFile>SpdxTranslatorSpdx.txt</packageVerificationCodeExcludedFile>
            <packageVerificationCodeExcludedFile>SpdxTranslatorSpdx.rdf</packageVerificationCodeExcludedFile>
          </PackageVerificationCode>
        </packageVerificationCode>
        <packageFileName>spdxtranslator-1.0.zip</packageFileName>
        <description>This utility..</description>
        <licenseInfoFromFiles rdf:nodeID="A0"/>
        <hasFile>
          <File>
            <artifactOf>
              <j.0:Project>
                <j.0:homepage>http://www.openjena.org/</j.0:homepage>
                <j.0:name>Jena</j.0:name>
              </j.0:Project>
            </artifactOf>
            <copyrightText>(c) Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 jena</copyrightText>
            <licenseComments></licenseComments>
            <licenseInfoInFile rdf:nodeID="A3"/>
            <licenseConcluded rdf:nodeID="A3"/>
            <fileType>ARCHIVE</fileType>
            <checksum>
              <Checksum>
                <checksumValue>76e739c67a2d28fced849ee1bb76e7391b93eb12</checksumValue>
                <algorithm>SHA1</algorithm>
              </Checksum>
            </checksum>
            <fileName>Jenna-2.6.3/jena-2.6.3-sources.jar</fileName>
          </File>
        </hasFile>
        <name>SPDX Translator</name>
        <licenseInfoFromFiles rdf:nodeID="A1"/>
        <licenseInfoFromFiles rdf:nodeID="A2"/>
        <hasFile>
          <File>
            <artifactOf>
              <j.0:Project rdf:about="http://svn.apache.org/repos/asf/commons/proper/lang/trunk/doap_lang.rdf">
                <j.0:homepage>http://commons.apache.org/lang/</j.0:homepage>
                <j.0:name>Apache Commons Lang</j.0:name>
              </j.0:Project>
            </artifactOf>
            <copyrightText>Copyright 2001-2007</copyrightText>
            <licenseComments></licenseComments>
            <licenseInfoInFile rdf:resource="http://spdx.org/licenses/Apache-2"/>
            <licenseConcluded rdf:resource="http://spdx.org/licenses/Apache-2"/>
            <fileType>ARCHIVE</fileType>
            <checksum>
              <Checksum>
                <checksumValue>76e739c67a2d28fced849ee1bb76e7391b93eb12</checksumValue>
                <algorithm>SHA1</algorithm>
              </Checksum>
            </checksum>
            <fileName>lib/commons-lang-2.3.zip</fileName>
          </File>
        </hasFile>
        <copyrightText> Copyright 2010, 2011 Source </copyrightText>
        <licenseComments>The declared license</licenseComments>
        <licenseInfoFromFiles>
          <License rdf:about="http://spdx.org/licenses/Apache-1">
            <licenseId>Apache-1</licenseId>
          </License>
        </licenseInfoFromFiles>
        <summary>SPDX Translator utility</summary>
        <licenseInfoFromFiles rdf:resource="http://spdx.org/licenses/MPL-1.1"/>
        <licenseInfoFromFiles rdf:resource="http://spdx.org/licenses/Apache-2"/>
        <licenseConcluded>
          <ConjunctiveLicenseSet>
            <member rdf:resource="http://spdx.org/licenses/MPL-1.1"/>
            <member rdf:nodeID="A1"/>
            <member rdf:nodeID="A3"/>
            <member rdf:nodeID="A2"/>
            <member rdf:resource="http://spdx.org/licenses/Apache-2"/>
            <member rdf:nodeID="A0"/>
            <member rdf:resource="http://spdx.org/licenses/Apache-1"/>
          </ConjunctiveLicenseSet>
        </licenseConcluded>
      </Package>
    </describesPackage>
    <hasExtractedLicensingInfo rdf:nodeID="A1"/>
    <hasExtractedLicensingInfo rdf:nodeID="A3"/>
    <creationInfo>
      <CreationInfo>
        <created>2010-02-03T00:00:00Z</created>
        <rdfs:comment>This is an example of an SPDX spreadsheet format</rdfs:comment>
        <creator>Person: Steven O'Neall</creator>
        <creator>Company: Source Code Company</creator>
        <creator>Tool: SourceCode-V1.2</creator>
      </CreationInfo>
    </creationInfo>
    <hasExtractedLicensingInfo rdf:nodeID="A2"/>
    <reviewed>
      <Review>
        <rdfs:comment>Another example reviewer.</rdfs:comment>
        <reviewDate>2011-03-13T00:00:00Z</reviewDate>
        <reviewer>Person: John Reviewer</reviewer>
      </Review>
    </reviewed>
  </SpdxDocument>
</rdf:RDF>
4

0 回答 0