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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.fop.afp.AFPResourceInfo;
  24. import org.apache.fop.afp.AFPResourceLevel;
  25. import org.apache.fop.render.afp.extensions.AFPElementMapping;
  26. import org.apache.xmlgraphics.util.QName;
  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 String RESOURCE_NAME = "afp:resource-name";
  34. /** the resource-level attribute */
  35. public static final String RESOURCE_LEVEL = "afp:resource-level";
  36. /** the resource-group-file attribute */
  37. public static final String RESOURCE_GROUP_FILE = "afp:resource-group-file";
  38. /**
  39. * Main constructor
  40. */
  41. public AFPForeignAttributeReader() {
  42. }
  43. /**
  44. * Returns the resource information
  45. *
  46. * @param foreignAttributes the foreign attributes
  47. * @return the resource information
  48. */
  49. public AFPResourceInfo getResourceInfo(Map/*<QName, String>*/ foreignAttributes) {
  50. AFPResourceInfo resourceInfo = new AFPResourceInfo();
  51. if (foreignAttributes != null && !foreignAttributes.isEmpty()) {
  52. QName resourceNameKey = new QName(AFPElementMapping.NAMESPACE, RESOURCE_NAME);
  53. String resourceName = (String)foreignAttributes.get(resourceNameKey);
  54. if (resourceName != null) {
  55. resourceInfo.setName(resourceName);
  56. }
  57. AFPResourceLevel level = getResourceLevel(foreignAttributes);
  58. if (level != null) {
  59. resourceInfo.setLevel(level);
  60. }
  61. }
  62. return resourceInfo;
  63. }
  64. /**
  65. * Returns the resource level
  66. *
  67. * @param foreignAttributes the foreign attributes
  68. * @return the resource level
  69. */
  70. public AFPResourceLevel getResourceLevel(Map/*<QName, String>*/ foreignAttributes) {
  71. AFPResourceLevel resourceLevel = null;
  72. if (foreignAttributes != null && !foreignAttributes.isEmpty()) {
  73. QName resourceLevelKey = new QName(AFPElementMapping.NAMESPACE, RESOURCE_LEVEL);
  74. if (foreignAttributes.containsKey(resourceLevelKey)) {
  75. String levelString = (String)foreignAttributes.get(resourceLevelKey);
  76. resourceLevel = AFPResourceLevel.valueOf(levelString);
  77. // if external get resource group file attributes
  78. if (resourceLevel != null && resourceLevel.isExternal()) {
  79. QName resourceGroupFileKey = new QName(AFPElementMapping.NAMESPACE,
  80. RESOURCE_GROUP_FILE);
  81. String resourceGroupFile
  82. = (String)foreignAttributes.get(resourceGroupFileKey);
  83. if (resourceGroupFile == null) {
  84. String msg = RESOURCE_GROUP_FILE + " not specified";
  85. log.error(msg);
  86. throw new UnsupportedOperationException(msg);
  87. }
  88. File resourceExternalGroupFile = new File(resourceGroupFile);
  89. SecurityManager security = System.getSecurityManager();
  90. try {
  91. if (security != null) {
  92. security.checkWrite(resourceExternalGroupFile.getPath());
  93. }
  94. } catch (SecurityException ex) {
  95. String msg = "unable to gain write access to external resource file: "
  96. + resourceGroupFile;
  97. log.error(msg);
  98. }
  99. try {
  100. boolean exists = resourceExternalGroupFile.exists();
  101. if (exists) {
  102. log.warn("overwriting external resource file: "
  103. + resourceGroupFile);
  104. }
  105. resourceLevel.setExternalFilePath(resourceGroupFile);
  106. } catch (SecurityException ex) {
  107. String msg = "unable to gain read access to external resource file: "
  108. + resourceGroupFile;
  109. log.error(msg);
  110. }
  111. }
  112. }
  113. }
  114. return resourceLevel;
  115. }
  116. }