예제 #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
 void end() {
   emitOffsetMapping();
   if (LOG.isLoggable(Level.FINER)) {
     LOG.finer("End of document");
   }
   for (XMLTransformerModule<T> m : modules) {
     m.end(this);
   }
   this.nodePath.pop();
 }
예제 #4
0
 void endText() {
   if (textStartOffset >= 0 && textOffset > textStartOffset) {
     if (LOG.isLoggable(Level.FINER)) {
       LOG.finer("End of text node");
     }
     for (XMLTransformerModule<T> m : modules) {
       m.endText(this);
     }
   }
   textStartOffset = -1;
 }
예제 #5
0
  void emitOffsetMapping() {
    if (textOffsetRange.length() == 0 && sourceOffsetRange.length() == 0) {
      return;
    }

    if (LOG.isLoggable(Level.FINER)) {
      LOG.finer(
          "New offset mapping: text = " + textOffsetRange + "==> source += " + sourceOffsetRange);
    }
    for (XMLTransformerModule<T> m : modules) {
      m.offsetMapping(this, textOffsetRange, sourceOffsetRange);
    }
  }
예제 #6
0
  void end(XMLEntity entity) {
    if (LOG.isLoggable(Level.FINER)) {
      LOG.finer("End of " + entity);
    }

    for (XMLTransformerModule<T> m : modules) {
      m.end(this, entity);
    }

    elementContext.pop();
    nodePath.pop();
    spacePreservationContext.pop();
    inclusionContext.pop();
  }
예제 #7
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);
    }
  }
예제 #8
0
  void newText(String text) throws IOException {
    if (textStartOffset < 0) {
      nextSibling();
      textStartOffset = textOffset;

      if (LOG.isLoggable(Level.FINER)) {
        LOG.finer("Start of text node");
      }
      for (XMLTransformerModule<T> m : modules) {
        m.startText(this);
      }
    }

    if (LOG.isLoggable(Level.FINER)) {
      LOG.finer("Text: '" + text.replaceAll("[\r\n]+", "\\\\n") + "'");
    }
    for (XMLTransformerModule<T> m : modules) {
      m.text(this, text);
    }
  }