You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AFPForeignAttributeReader.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.render.afp;
  19. import java.io.File;
  20. import java.util.Map;
  21. import org.apache.commons.logging.Log;
  22. import org.apache.commons.logging.LogFactory;
  23. import org.apache.xmlgraphics.util.QName;
  24. import org.apache.fop.afp.AFPResourceInfo;
  25. import org.apache.fop.afp.AFPResourceLevel;
  26. import org.apache.fop.render.afp.extensions.AFPElementMapping;
  27. /**
  28. * Parses any AFP foreign attributes
  29. */
  30. public class AFPForeignAttributeReader {
  31. private static final Log log = LogFactory.getLog("org.apache.xmlgraphics.afp");
  32. /** the resource-name attribute */
  33. public static final QName RESOURCE_NAME = new QName(
  34. AFPElementMapping.NAMESPACE, "afp:resource-name");
  35. /** the resource-level attribute */
  36. public static final QName RESOURCE_LEVEL = new QName(
  37. AFPElementMapping.NAMESPACE, "afp:resource-level");
  38. /** the resource-group-file attribute */
  39. public static final QName RESOURCE_GROUP_FILE = new QName(
  40. AFPElementMapping.NAMESPACE, "afp:resource-group-file");
  41. /**
  42. * Main constructor
  43. */
  44. public AFPForeignAttributeReader() {
  45. }
  46. /**
  47. * Returns the resource information
  48. *
  49. * @param foreignAttributes the foreign attributes
  50. * @return the resource information
  51. */
  52. public AFPResourceInfo getResourceInfo(Map/*<QName, String>*/ foreignAttributes) {
  53. AFPResourceInfo resourceInfo = new AFPResourceInfo();
  54. if (foreignAttributes != null && !foreignAttributes.isEmpty()) {
  55. String resourceName = (String)foreignAttributes.get(RESOURCE_NAME);
  56. if (resourceName != null) {
  57. resourceInfo.setName(resourceName);
  58. }
  59. AFPResourceLevel level = getResourceLevel(foreignAttributes);
  60. if (level != null) {
  61. resourceInfo.setLevel(level);
  62. }
  63. }
  64. return resourceInfo;
  65. }
  66. /**
  67. * Returns the resource level
  68. *
  69. * @param foreignAttributes the foreign attributes
  70. * @return the resource level
  71. */
  72. public AFPResourceLevel getResourceLevel(Map/*<QName, String>*/ foreignAttributes) {
  73. AFPResourceLevel resourceLevel = null;
  74. if (foreignAttributes != null && !foreignAttributes.isEmpty()) {
  75. if (foreignAttributes.containsKey(RESOURCE_LEVEL)) {
  76. String levelString = (String)foreignAttributes.get(RESOURCE_LEVEL);
  77. resourceLevel = AFPResourceLevel.valueOf(levelString);
  78. // if external get resource group file attributes
  79. if (resourceLevel != null && resourceLevel.isExternal()) {
  80. String resourceGroupFile
  81. = (String)foreignAttributes.get(RESOURCE_GROUP_FILE);
  82. if (resourceGroupFile == null) {
  83. String msg = RESOURCE_GROUP_FILE + " not specified";
  84. log.error(msg);
  85. throw new UnsupportedOperationException(msg);
  86. }
  87. File resourceExternalGroupFile = new File(resourceGroupFile);
  88. SecurityManager security = System.getSecurityManager();
  89. try {
  90. if (security != null) {
  91. security.checkWrite(resourceExternalGroupFile.getPath());
  92. }
  93. } catch (SecurityException ex) {
  94. String msg = "unable to gain write access to external resource file: "
  95. + resourceGroupFile;
  96. log.error(msg);
  97. }
  98. try {
  99. boolean exists = resourceExternalGroupFile.exists();
  100. if (exists) {
  101. log.warn("overwriting external resource file: "
  102. + resourceGroupFile);
  103. }
  104. resourceLevel.setExternalFilePath(resourceGroupFile);
  105. } catch (SecurityException ex) {
  106. String msg = "unable to gain read access to external resource file: "
  107. + resourceGroupFile;
  108. log.error(msg);
  109. }
  110. }
  111. }
  112. }
  113. return resourceLevel;
  114. }
  115. }