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.

AFPPageSetupElement.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.extensions;
  19. import org.xml.sax.Attributes;
  20. import org.xml.sax.Locator;
  21. import org.apache.fop.apps.FOPException;
  22. import org.apache.fop.fo.Constants;
  23. import org.apache.fop.fo.FONode;
  24. import org.apache.fop.fo.PropertyList;
  25. import org.apache.fop.fo.extensions.ExtensionAttachment;
  26. /**
  27. * This class extends the org.apache.fop.extensions.ExtensionObj class. The
  28. * object facilitates extraction of elements from formatted objects based on
  29. * the static list as defined in the AFPElementMapping implementation.
  30. */
  31. public class AFPPageSetupElement extends AbstractAFPExtensionObject {
  32. private static final String ATT_ENCODING = "encoding";
  33. private static final String ATT_SRC = "src";
  34. /**
  35. * Constructs an AFP object (called by Maker).
  36. *
  37. * @param parent the parent formatting object
  38. * @param name the name of the afp element
  39. */
  40. public AFPPageSetupElement(FONode parent, String name) {
  41. super(parent, name);
  42. }
  43. private AFPPageSetup getPageSetupAttachment() {
  44. return (AFPPageSetup)getExtensionAttachment();
  45. }
  46. /** {@inheritDoc} */
  47. @Override
  48. protected void startOfNode() throws FOPException {
  49. super.startOfNode();
  50. if (AFPElementMapping.TAG_LOGICAL_ELEMENT.equals(getLocalName())) {
  51. if (parent.getNameId() != Constants.FO_SIMPLE_PAGE_MASTER
  52. && parent.getNameId() != Constants.FO_PAGE_SEQUENCE) {
  53. invalidChildError(getLocator(), parent.getName(), getNamespaceURI(), getName(),
  54. "rule.childOfPageSequenceOrSPM");
  55. }
  56. } else {
  57. if (parent.getNameId() != Constants.FO_SIMPLE_PAGE_MASTER
  58. && parent.getNameId() != Constants.FO_PAGE_SEQUENCE
  59. && parent.getNameId() != Constants.FO_DECLARATIONS) {
  60. invalidChildError(getLocator(), parent.getName(), getNamespaceURI(), getName(),
  61. "rule.childOfSPMorPSorDeclarations");
  62. }
  63. }
  64. }
  65. /** {@inheritDoc} */
  66. @Override
  67. protected void characters(char[] data, int start, int length,
  68. PropertyList pList, Locator locator) throws FOPException {
  69. StringBuffer sb = new StringBuffer();
  70. AFPPageSetup pageSetup = getPageSetupAttachment();
  71. if (pageSetup.getContent() != null) {
  72. sb.append(pageSetup.getContent());
  73. }
  74. sb.append(data, start, length);
  75. pageSetup.setContent(sb.toString());
  76. }
  77. /** {@inheritDoc} */
  78. @Override
  79. public void processNode(String elementName, Locator locator,
  80. Attributes attlist, PropertyList propertyList)
  81. throws FOPException {
  82. super.processNode(elementName, locator, attlist, propertyList);
  83. AFPPageSetup pageSetup = getPageSetupAttachment();
  84. if (AFPElementMapping.INCLUDE_PAGE_SEGMENT.equals(elementName)) {
  85. String attr = attlist.getValue(ATT_SRC);
  86. if (attr != null && attr.length() > 0) {
  87. pageSetup.setValue(attr);
  88. } else {
  89. missingPropertyError(ATT_SRC);
  90. }
  91. } else if (AFPElementMapping.TAG_LOGICAL_ELEMENT.equals(elementName)) {
  92. String attr = attlist.getValue(AFPPageSetup.ATT_VALUE);
  93. if (attr != null && attr.length() > 0) {
  94. pageSetup.setValue(attr);
  95. } else {
  96. missingPropertyError(AFPPageSetup.ATT_VALUE);
  97. }
  98. attr = attlist.getValue(ATT_ENCODING);
  99. if (attr != null) {
  100. try {
  101. pageSetup.setEncoding(Integer.parseInt(attr));
  102. } catch (NumberFormatException nfe) {
  103. invalidPropertyValueError(ATT_ENCODING, attr, nfe);
  104. }
  105. }
  106. }
  107. String placement = attlist.getValue(AFPPageSetup.ATT_PLACEMENT);
  108. if (placement != null && placement.length() > 0) {
  109. pageSetup.setPlacement(ExtensionPlacement.fromXMLValue(placement));
  110. }
  111. }
  112. /** {@inheritDoc} */
  113. @Override
  114. protected ExtensionAttachment instantiateExtensionAttachment() {
  115. return new AFPPageSetup(getLocalName());
  116. }
  117. }