Search in sources :

Example 1 with IXmlEntity

use of org.whole.lang.xml.model.IXmlEntity in project whole by wholeplatform.

the class XmlNormalizerVisitor method visit.

@Override
public void visit(Content entity) {
    // recursively normalize nested composite entities
    IEntityIterator<IEntity> iterator = IteratorFactory.childMatcherIterator().withPattern(EntityKinds.COMPOSITE);
    iterator.reset(entity);
    while (iterator.hasNext()) {
        IEntity composite = iterator.next();
        ((IXmlEntity) composite).accept(this);
        // move Content's children in place of Content entity
        if (Matcher.match(Content, composite)) {
            for (int i = composite.wSize() - 1; i >= 0; i--) {
                IEntity child = composite.wGet(i);
                composite.wRemove(i);
                iterator.add(child);
            }
            iterator.remove();
        }
    }
    for (int i = 0; i < entity.wSize(); i++) {
        IContent child = (IContent) entity.wGet(i);
        if (Matcher.matchImpl(XmlEntityDescriptorEnum.CharData, child)) {
            IEntity nextChild;
            if (i + 1 < entity.wSize() && Matcher.matchImpl(XmlEntityDescriptorEnum.CharData, nextChild = entity.wGet(i + 1))) {
                StringBuilder sb = getStringBuilder();
                sb.append(child.wStringValue());
                do {
                    sb.append(nextChild.wStringValue());
                    entity.wRemove(i + 1);
                } while (i + 1 < entity.wSize() && Matcher.matchImpl(XmlEntityDescriptorEnum.CharData, nextChild = entity.wGet(i + 1)));
                child.wSetValue(sb.toString());
            }
            if (XmlUtils.isIgnorableWhitespace(child.wStringValue()))
                entity.wRemove(i--);
        }
        child.accept(this);
    }
}
Also used : IEntity(org.whole.lang.model.IEntity) IContent(org.whole.lang.xml.model.IContent) IXmlEntity(org.whole.lang.xml.model.IXmlEntity)

Aggregations

IEntity (org.whole.lang.model.IEntity)1 IContent (org.whole.lang.xml.model.IContent)1 IXmlEntity (org.whole.lang.xml.model.IXmlEntity)1