org.jdom.Content Java Examples

The following examples show how to use org.jdom.Content. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MapBinding.java    From consulo with Apache License 2.0 6 votes vote down vote up
private void serializeKeyOrValue(@Nonnull Element entry, @Nonnull String attributeName, @Nullable Object value, @Nullable Binding binding, @Nonnull SerializationFilter filter) {
  if (value == null) {
    return;
  }

  if (binding == null) {
    entry.setAttribute(attributeName, XmlSerializerImpl.convertToString(value));
  }
  else {
    Object serialized = binding.serialize(value, entry, filter);
    if (serialized != null) {
      if (myMapAnnotation != null && !myMapAnnotation.surroundKeyWithTag()) {
        entry.addContent((Content)serialized);
      }
      else {
        Element container = new Element(attributeName);
        container.addContent((Content)serialized);
        entry.addContent(container);
      }
    }
  }
}
 
Example #2
Source File: SAXBuilder_JDOMTest.java    From learnjavabug with MIT License 6 votes vote down vote up
public static void main(String[] args) throws JDOMException, IOException {
    //todo 存在xxe漏洞
    SAXBuilder saxBuilder = new SAXBuilder();

    //todo 修复方式1
//    SAXBuilder saxBuilder = new SAXBuilder(true);

    //todo 修复方式2
//    SAXBuilder saxBuilder = new SAXBuilder();
//    saxBuilder.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
//    saxBuilder.setFeature("http://xml.org/sax/features/external-general-entities", false);
//    saxBuilder.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
//    saxBuilder.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(Payloads.FEEDBACK.getBytes());
    Document document = saxBuilder.build(byteArrayInputStream);
    Element element = document.getRootElement();
    List<Content> contents = element.getContent();
    for (Content content : contents) {
      System.out.println(content.getValue());
    }
  }
 
Example #3
Source File: Utils.java    From pom-manipulation-ext with Apache License 2.0 5 votes vote down vote up
/**
 * Method updateElement.
 * 
 * @param counter
 * @param shouldExist
 * @param name
 * @param parent
 * @return Element
 */
public static Element updateElement( final IndentationCounter counter, final Element parent, final String name,
                                     final boolean shouldExist )
{
    Element element = parent.getChild( name, parent.getNamespace() );
    if ( shouldExist )
    {
        if ( element == null )
        {
            element = factory.element( name, parent.getNamespace() );
            insertAtPreferredLocation( parent, element, counter );
        }
        counter.increaseCount();
    }
    else if ( element != null )
    {
        final int index = parent.indexOf( element );
        if ( index > 0 )
        {
            final Content previous = parent.getContent( index - 1 );
            if ( previous instanceof Text )
            {
                final Text txt = (Text) previous;
                if ( txt.getTextTrim().length() == 0 )
                {
                    parent.removeContent( txt );
                }
            }
        }
        parent.removeContent( element );
    }
    return element;
}
 
Example #4
Source File: XmlSerializerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
static String getTextValue(@Nonnull Element element, @Nonnull String defaultText) {
  List<Content> content = element.getContent();
  String value = defaultText;
  if (!content.isEmpty()) {
    Content child = content.get(0);
    if (child instanceof Text) {
      value = child.getValue();
    }
  }
  return value;
}
 
Example #5
Source File: JDOMBuilder.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static Element tag(String name, Content...content) {
  final Element element = new Element(name);
  for (Content c : content) {
    if (c instanceof AttrContent) {
      AttrContent attrContent = (AttrContent)c;
      element.setAttribute(attrContent.myName, attrContent.myValue);
    }
    else {
      element.addContent(c);
    }
  }

  return element;
}
 
Example #6
Source File: CustomWalker.java    From consulo with Apache License 2.0 5 votes vote down vote up
private final boolean isTextLike(final Content c) {
  switch (c.getCType()) {
    case Text:
    case CDATA:
    case EntityRef:
      return true;
    default:
      // nothing.
  }
  return false;
}
 
Example #7
Source File: CustomWalker.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * Create a Walker that preserves all content in its raw state.
 *
 * @param xx       the content to walk.
 * @param fstack   the current FormatStack
 * @param doescape Whether Text values should be escaped.
 */
public CustomWalker(final List<? extends Content> xx, final FormatStack fstack, final boolean doescape) {
  super();
  this.fstack = fstack;
  this.content = xx.isEmpty() ? EMPTYIT : xx.iterator();
  this.escape = doescape ? fstack.getEscapeStrategy() : null;
  newlineindent = fstack.getPadBetween();
  endofline = fstack.getLevelEOL();
  if (!content.hasNext()) {
    alltext = true;
    allwhite = true;
  }
  else {
    boolean atext = false;
    boolean awhite = false;
    pending = content.next();
    if (isTextLike(pending)) {
      // the first item in the list is Text-like, and we pre-check
      // to see whether all content is text.... and whether it amounts
      // to something.
      pendingmt = buildMultiText(true);
      analyzeMultiText(pendingmt, 0, mtsourcesize);
      pendingmt.done();

      if (pending == null) {
        atext = true;
        awhite = mtsize == 0;
      }
      if (mtsize == 0) {
        // first content in list is ignorable.
        pendingmt = null;
      }
    }
    alltext = atext;
    allwhite = awhite;
  }
  hasnext = pendingmt != null || pending != null;
}
 
Example #8
Source File: CustomWalker.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * Add some JDOM Content (typically an EntityRef) that will be treated
 * as part of the Text-like sequence.
 *
 * @param c the content to add.
 */
public void appendRaw(final Content c) {
  closeText();
  ensurespace();
  mttext[mtsize] = null;
  mtdata[mtsize++] = c;
  mtbuffer.setLength(0);

}
 
Example #9
Source File: DescendantIterator.java    From jclic with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the next {@link Content} descendant.
 *
 * @return the next descendant
 */
public Object next() {
    if (!hasNext()) {
        throw new NoSuchElementException();
    }

    // If we need to descend, go for it and record where we are.
    // We do the shuffle here on the next next() call so remove() is easy
    // to code up.
    if (nextIterator != null) {
        push(iterator);
        iterator = nextIterator;
        nextIterator = null;
    }

    // If this iterator is finished, try moving up the stack
    while (!iterator.hasNext()) {
        if (stack.size() > 0) {
            iterator = pop();
        }
        else {
          throw new NoSuchElementException("Somehow we lost our iterator");
        }
    }

    Content child = (Content) iterator.next();
    if (child instanceof Element) {
        nextIterator = ((Element)child).getContent().iterator();
    }
    return child;
}
 
Example #10
Source File: StaxSerializer.java    From cxf with Apache License 2.0 5 votes vote down vote up
public void writeDocument(Document doc, XMLStreamWriter writer) throws XMLStreamException {
    writer.writeStartDocument("1.0");

    for (Iterator<?> itr = doc.getContent().iterator(); itr.hasNext();) {
        Content content = (Content)itr.next();

        if (content instanceof Element) {
            writeElement((Element)content, writer);
        }
    }

    writer.writeEndDocument();
}
 
Example #11
Source File: BucketsResourceHandler.java    From teamcity-s3-artifact-storage-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public Content getContent(final AmazonS3 s3Client, final Map<String, String> parameters) {
  AWSCommonParams.validate(parameters, true);
  final Element bucketsElement = new Element("buckets");
  for (Bucket bucket : withClientCorrectingRegion(s3Client, new HashMap<>(parameters), AmazonS3::listBuckets)) {
    final Element bucketElement = new Element("bucket");
    final String bucketName = bucket.getName();
    bucketElement.setText(bucketName);
    bucketsElement.addContent(bucketElement);
  }
  return bucketsElement;
}
 
Example #12
Source File: BucketLocationHandler.java    From teamcity-s3-artifact-storage-plugin with Apache License 2.0 5 votes vote down vote up
@Override
protected Content getContent(final AmazonS3 s3Client, final Map<String, String> parameters) {
  final String bucketName = S3Util.getBucketName(parameters);
  if (bucketName == null) {
    final String message = String.format("Invalid request: %s parameter was not set", S3Util.beanPropertyNameForBucketName());
    throw new IllegalArgumentException(message);
  }
  final Element bucketElement = new Element("bucket");
  bucketElement.setAttribute("name", bucketName);
  bucketElement
    .setAttribute("location", S3Util.withClientCorrectingRegion(s3Client, parameters, correctedClient -> getRegionName(correctedClient.getBucketLocation(bucketName))));
  return bucketElement;
}
 
Example #13
Source File: NetbeansBuildActionJDOMWriter.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Method updateElement.
 * 
 * @param counter
 * @param shouldExist
 * @param name
 * @param parent
 * @return Element
 */
protected Element updateElement(Counter counter, Element parent, String name, boolean shouldExist)
{
    Element element =  parent.getChild(name, parent.getNamespace());
    if (element != null && shouldExist) {
        counter.increaseCount();
    }
    if (element == null && shouldExist) {
        element = factory.element(name, parent.getNamespace());
        insertAtPreferredLocation(parent, element, counter);
        counter.increaseCount();
    }
    if (!shouldExist && element != null) {
        int index = parent.indexOf(element);
        if (index > 0) {
            Content previous = parent.getContent(index - 1);
            if (previous instanceof Text) {
                Text txt = (Text)previous;
                if (txt.getTextTrim().length() == 0) {
                    parent.removeContent(txt);
                }
            }
        }
        parent.removeContent(element);
    }
    return element;
}
 
Example #14
Source File: StaxSerializer.java    From cxf with Apache License 2.0 4 votes vote down vote up
public void writeElement(Element e, XMLStreamWriter writer) throws XMLStreamException {
    // need to check if the namespace is declared before we write the
    // start element because that will put the namespace in the context.
    String elPrefix = e.getNamespacePrefix();
    String elUri = e.getNamespaceURI();

    String boundPrefix = writer.getPrefix(elUri);
    boolean writeElementNS = false;
    if (boundPrefix == null || !elPrefix.equals(boundPrefix)) {
        writeElementNS = true;
    }

    writer.writeStartElement(elPrefix, e.getName(), elUri);

    List<?> namespaces = e.getAdditionalNamespaces();
    for (Iterator<?> itr = namespaces.iterator(); itr.hasNext();) {
        Namespace ns = (Namespace)itr.next();

        String prefix = ns.getPrefix();
        String uri = ns.getURI();

        writer.setPrefix(prefix, uri);
        writer.writeNamespace(prefix, uri);

        if (elUri.equals(uri) && elPrefix.equals(prefix)) {
            writeElementNS = false;
        }
    }

    for (Iterator<?> itr = e.getAttributes().iterator(); itr.hasNext();) {
        Attribute attr = (Attribute)itr.next();
        String attPrefix = attr.getNamespacePrefix();
        String attUri = attr.getNamespaceURI();

        if (attUri == null || attUri.isEmpty()) {
            writer.writeAttribute(attr.getName(), attr.getValue());
        } else {
            writer.writeAttribute(attPrefix, attUri, attr.getName(), attr.getValue());

            if (!isDeclared(writer, attPrefix, attUri)) {
                if (elUri.equals(attUri) && elPrefix.equals(attPrefix)) {
                    if (writeElementNS) {
                        writer.setPrefix(attPrefix, attUri);
                        writer.writeNamespace(attPrefix, attUri);
                        writeElementNS = false;
                    }
                } else {
                    writer.setPrefix(attPrefix, attUri);
                    writer.writeNamespace(attPrefix, attUri);
                }
            }
        }
    }

    if (writeElementNS) {
        if (elPrefix == null || elPrefix.length() == 0) {
            writer.writeDefaultNamespace(elUri);
        } else {
            writer.writeNamespace(elPrefix, elUri);
        }
    }

    for (Iterator<?> itr = e.getContent().iterator(); itr.hasNext();) {
        Content n = (Content)itr.next();
        if (n instanceof CDATA) {
            writer.writeCData(n.getValue());
        } else if (n instanceof Text) {
            writer.writeCharacters(((Text)n).getText());
        } else if (n instanceof Element) {
            writeElement((Element)n, writer);
        } else if (n instanceof Comment) {
            writer.writeComment(n.getValue());
        } else if (n instanceof EntityRef) {
            // EntityRef ref = (EntityRef) n;
            // writer.writeEntityRef(ref.)
        }
    }

    writer.writeEndElement();
}
 
Example #15
Source File: S3ClientResourceHandler.java    From teamcity-s3-artifact-storage-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public Content getContent(final Map<String, String> parameters) throws Exception {
  return S3Util.withS3Client(parameters, s3Client -> getContent(s3Client, parameters));
}
 
Example #16
Source File: JDOMBuilder.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static Content attr(final String name, final String value) {
  return new AttrContent(name, value);
}
 
Example #17
Source File: CustomWalker.java    From consulo with Apache License 2.0 2 votes vote down vote up
/**
 * Get the content at a position in the input content. Useful for subclasses
 * in their {@link #analyzeMultiText(MultiText, int, int)} calls.
 *
 * @param index the index to get the content at.
 * @return the content at the index.
 */
protected final Content get(final int index) {
  return mtsource[index];
}
 
Example #18
Source File: ResourceHandler.java    From teamcity-s3-artifact-storage-plugin with Apache License 2.0 votes vote down vote up
public Content getContent(Map<String, String> parameters) throws Exception; 
Example #19
Source File: S3ClientResourceHandler.java    From teamcity-s3-artifact-storage-plugin with Apache License 2.0 votes vote down vote up
protected abstract Content getContent(final AmazonS3 s3Client, final Map<String, String> parameters) throws Exception;