PageRenderTime 36ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/okapi/steps/xliffkit/src/main/java/net/sf/okapi/steps/xliffkit/opc/OPCPackageUtil.java

http://okapi.googlecode.com/
Java | 82 lines | 48 code | 12 blank | 22 comment | 4 complexity | 59505bde48817117712a3f30e0e301d9 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0
  1. /*===========================================================================
  2. Copyright (C) 2008-2010 by the Okapi Framework contributors
  3. -----------------------------------------------------------------------------
  4. This library is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or (at
  7. your option) any later version.
  8. This library is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
  11. General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this library; if not, write to the Free Software Foundation,
  14. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. See also the full LGPL text here: http://www.gnu.org/copyleft/lesser.html
  16. ===========================================================================*/
  17. package net.sf.okapi.steps.xliffkit.opc;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  21. import org.apache.poi.openxml4j.opc.OPCPackage;
  22. import org.apache.poi.openxml4j.opc.PackagePart;
  23. import org.apache.poi.openxml4j.opc.PackageRelationship;
  24. import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
  25. import org.apache.poi.openxml4j.opc.PackagingURIHelper;
  26. public class OPCPackageUtil {
  27. public static List<PackagePart> getCoreParts(OPCPackage pack) {
  28. List<PackagePart> res = new ArrayList<PackagePart>();
  29. for (PackageRelationship rel : pack.getRelationshipsByType(TKitRelationshipTypes.CORE_DOCUMENT))
  30. try {
  31. res.add(pack.getPart(PackagingURIHelper.createPartName(rel.getTargetURI())));
  32. } catch (InvalidFormatException e) {
  33. // TODO Handle exception
  34. e.printStackTrace();
  35. }
  36. return res;
  37. }
  38. public static PackagePart getCorePart(OPCPackage pack) {
  39. List<PackagePart> res = new ArrayList<PackagePart>();
  40. for (PackageRelationship rel : pack.getRelationshipsByType(TKitRelationshipTypes.CORE_DOCUMENT))
  41. try {
  42. res.add(pack.getPart(PackagingURIHelper.createPartName(rel.getTargetURI())));
  43. } catch (InvalidFormatException e) {
  44. // TODO Handle exception
  45. e.printStackTrace();
  46. }
  47. return res.size() > 0 ? res.get(0) : null;
  48. }
  49. private static PackagePart getPartByRelationshipType(PackagePart part, String relationshipType) {
  50. try {
  51. PackageRelationshipCollection rels = part.getRelationshipsByType(relationshipType);
  52. if (rels.size() == 0) return null;
  53. OPCPackage pack = part.getPackage();
  54. return pack.getPart(PackagingURIHelper.createPartName(rels.getRelationship(0).getTargetURI()));
  55. } catch (InvalidFormatException e) {
  56. // TODO Handle exception
  57. e.printStackTrace();
  58. }
  59. return null;
  60. }
  61. public static PackagePart getSourcePart(PackagePart part) {
  62. return getPartByRelationshipType(part, TKitRelationshipTypes.SOURCE);
  63. }
  64. public static PackagePart getResourcesPart(PackagePart part) {
  65. return getPartByRelationshipType(part, TKitRelationshipTypes.RESOURCES);
  66. }
  67. }