use of org.jdom.Content in project maven-plugins by apache.
the class XmlAppendingTransformer method processResource.
public void processResource(String resource, InputStream is, List<Relocator> relocators) throws IOException {
Document r;
try {
SAXBuilder builder = new SAXBuilder(false);
builder.setExpandEntities(false);
if (ignoreDtd) {
builder.setEntityResolver(new EntityResolver() {
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
return new InputSource(new StringReader(""));
}
});
}
r = builder.build(is);
} catch (JDOMException e) {
throw new RuntimeException("Error processing resource " + resource + ": " + e.getMessage(), e);
}
if (doc == null) {
doc = r;
} else {
Element root = r.getRootElement();
for (@SuppressWarnings("unchecked") Iterator<Attribute> itr = root.getAttributes().iterator(); itr.hasNext(); ) {
Attribute a = itr.next();
itr.remove();
Element mergedEl = doc.getRootElement();
Attribute mergedAtt = mergedEl.getAttribute(a.getName(), a.getNamespace());
if (mergedAtt == null) {
mergedEl.setAttribute(a);
}
}
for (@SuppressWarnings("unchecked") Iterator<Content> itr = root.getChildren().iterator(); itr.hasNext(); ) {
Content n = itr.next();
itr.remove();
doc.getRootElement().addContent(n);
}
}
}
use of org.jdom.Content in project maven-plugins by apache.
the class MavenJDOMWriter method updateElement.
// -- void updateDistributionManagement(DistributionManagement, String, Counter, Element)
/**
* Method updateElement
*
* @param counter
* @param shouldExist
* @param name
* @param parent
*/
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;
}
use of org.jdom.Content in project intellij-community by JetBrains.
the class AbstractCollectionBinding method serialize.
@Nullable
@Override
public Object serialize(@NotNull Object o, @Nullable Object context, @Nullable SerializationFilter filter) {
Collection<Object> collection = getIterable(o);
String tagName = getTagName(o);
if (tagName == null) {
List<Object> result = new SmartList<Object>();
if (!ContainerUtil.isEmpty(collection)) {
for (Object item : collection) {
ContainerUtil.addAllNotNull(result, serializeItem(item, result, filter));
}
}
return result;
} else {
Element result = new Element(tagName);
if (!ContainerUtil.isEmpty(collection)) {
for (Object item : collection) {
Content child = (Content) serializeItem(item, result, filter);
if (child != null) {
result.addContent(child);
}
}
}
return result;
}
}
use of org.jdom.Content in project intellij-community by JetBrains.
the class XmlSerializerImpl method getTextValue.
@NotNull
static String getTextValue(@NotNull Element element, @NotNull String defaultText) {
List<Content> content = element.getContent();
int size = content.size();
StringBuilder builder = null;
for (int i = 0; i < size; i++) {
Content child = content.get(i);
if (child instanceof Text) {
String value = child.getValue();
if (builder == null && i == (size - 1)) {
return value;
}
if (builder == null) {
builder = new StringBuilder();
}
builder.append(value);
}
}
return builder == null ? defaultText : builder.toString();
}
use of org.jdom.Content in project intellij-community by JetBrains.
the class CommonCodeStyleSettingsManager method writeExternal.
public void writeExternal(@NotNull Element element) throws WriteExternalException {
synchronized (this) {
if (myCommonSettingsMap == null) {
return;
}
final Map<String, Language> idToLang = new THashMap<>();
for (Language language : myCommonSettingsMap.keySet()) {
idToLang.put(language.getID(), language);
}
String[] languages = ArrayUtil.toStringArray(ContainerUtil.union(myUnknownSettingsMap.keySet(), idToLang.keySet()));
Arrays.sort(languages);
for (String id : languages) {
final Language language = idToLang.get(id);
if (language != null) {
final CommonCodeStyleSettings commonSettings = myCommonSettingsMap.get(language);
Element commonSettingsElement = new Element(COMMON_SETTINGS_TAG);
commonSettings.writeExternal(commonSettingsElement);
commonSettingsElement.setAttribute(LANGUAGE_ATTR, language.getID());
if (!commonSettingsElement.getChildren().isEmpty()) {
element.addContent(commonSettingsElement);
}
} else {
final Content unknown = myUnknownSettingsMap.get(id);
if (unknown != null) {
element.addContent(unknown.detach());
}
}
}
}
}
Aggregations