Пример #1
0
  void start(XMLEntity entity) {
    if (LOG.isLoggable(Level.FINER)) {
      LOG.finer("Start of " + entity);
    }

    final boolean parentIncluded = (inclusionContext.isEmpty() ? true : inclusionContext.peek());
    inclusionContext.push(
        parentIncluded ? !configuration.excluded(entity) : configuration.included(entity));

    spacePreservationContext.push(
        spacePreservationContext.isEmpty() ? false : spacePreservationContext.peek());
    final Object xmlSpace = entity.getAttributes().get(TextConstants.XML_SPACE_ATTR_NAME);
    if (xmlSpace != null) {
      spacePreservationContext.pop();
      spacePreservationContext.push("preserve".equalsIgnoreCase(xmlSpace.toString()));
    }

    nodePath.set(entity.getAttributes());
    nodePath.push(0);

    elementContext.push(entity);

    for (XMLTransformerModule<T> m : modules) {
      m.start(this, entity);
    }
  }
Пример #2
0
  void start() {
    if (LOG.isLoggable(Level.FINER)) {
      LOG.finer("Start of document");
    }

    elementContext.clear();
    inclusionContext.clear();
    spacePreservationContext.clear();
    nodePath.clear();

    textBuffer = new FileBackedOutputStream(configuration.getTextBufferSize(), true);
    textStartOffset = -1;
    lastChar = (configuration.isRemoveLeadingWhitespace() ? ' ' : 0);

    sourceOffset = 0;
    textOffset = 0;

    sourceOffsetRange = TextRange.NULL;
    textOffsetRange = TextRange.NULL;

    this.nodePath.push(0);
    for (XMLTransformerModule<T> m : modules) {
      m.start(this);
    }
  }
Пример #3
0
  public void write(String text, boolean fromSource) {
    if (LOG.isLoggable(Level.FINER)) {
      LOG.finer(
          "Inserting Text: '"
              + text.replaceAll("[\r\n]+", "\\\\n")
              + "' ("
              + (fromSource ? "from source" : "generated")
              + ")");
    }
    try {
      final int textLength = text.length();
      final StringBuilder inserted = new StringBuilder();
      if (fromSource) {
        final boolean preserveSpace = isSpacePreserved();
        for (int cc = 0; cc < textLength; cc++) {
          char currentChar = text.charAt(cc);
          if (!preserveSpace
              && configuration.isCompressingWhitespace()
              && Character.isWhitespace(lastChar)
              && Character.isWhitespace(currentChar)) {
            mapOffsetDelta(0, 1);
            continue;
          }
          if (currentChar == '\n' || currentChar == '\r') {
            currentChar = ' ';
          }
          textBuffer.write(Character.toString(lastChar = currentChar).getBytes());
          inserted.append(lastChar);
          mapOffsetDelta(1, 1);
        }
      } else {
        textBuffer.write(text.getBytes());
        inserted.append(text);
        mapOffsetDelta(inserted.length(), 0);
      }

      final String insertedStr = inserted.toString();
      for (XMLTransformerModule<T> m : configuration.getModules()) {
        m.textWritten(this, text, insertedStr);
      }
    } catch (IOException e) {
      throw Throwables.propagate(e);
    }
  }
Пример #4
0
  public Layer<T> transform(final Layer<T> source) throws IOException, XMLStreamException {
    this.source = source;
    this.target = configuration.start(source);

    try {
      source.stream(
          new Text.Consumer() {
            @Override
            public void consume(Reader sourceReader) throws IOException {
              XMLStreamReader xmlStream = null;
              try {
                xmlStream = xmlInputFactory.createXMLStreamReader(sourceReader);

                final Stack<XMLEntity> entities = new Stack<XMLEntity>();
                start();
                while (xmlStream.hasNext()) {
                  final int event = xmlStream.next();
                  mapOffsetDelta(0, xmlStream.getLocation().getCharacterOffset() - sourceOffset);

                  switch (event) {
                    case XMLStreamConstants.START_ELEMENT:
                      endText();
                      nextSibling();
                      start(entities.push(XMLEntity.newElement(xmlStream)));
                      break;
                    case XMLStreamConstants.END_ELEMENT:
                      endText();
                      end(entities.pop());
                      break;
                    case XMLStreamConstants.COMMENT:
                      endText();
                      nextSibling();
                      emptyEntity(XMLEntity.newComment(xmlStream));
                      break;
                    case XMLStreamConstants.PROCESSING_INSTRUCTION:
                      endText();
                      nextSibling();
                      emptyEntity(XMLEntity.newPI(xmlStream));
                      break;
                    case XMLStreamConstants.CHARACTERS:
                    case XMLStreamConstants.ENTITY_REFERENCE:
                    case XMLStreamConstants.CDATA:
                      newText(xmlStream.getText());
                      break;
                  }
                }
                end();
              } catch (XMLStreamException e) {
                throw Throwables.propagate(e);
              } finally {
                XML.closeQuietly(xmlStream);
              }
            }
          });
      Reader textReader = null;
      try {
        configuration.end(target, textReader = read());
        return target;
      } finally {
        Closeables.close(textReader, false);
      }
    } catch (Throwable t) {
      Throwables.propagateIfInstanceOf(t, IOException.class);
      Throwables.propagateIfInstanceOf(Throwables.getRootCause(t), XMLStreamException.class);
      throw Throwables.propagate(t);
    }
  }
Пример #5
0
 public XMLTransformer(XMLTransformerConfiguration<T> configuration) {
   this.configuration = configuration;
   this.modules = configuration.getModules();
 }
Пример #6
0
 public boolean isNotable() {
   return !elementContext.isEmpty() && configuration.isNotable(elementContext.peek());
 }
Пример #7
0
 public boolean isLineElement() {
   return !elementContext.isEmpty() && configuration.isLineElement(elementContext.peek());
 }