Example usage for org.jdom2 Comment getText

List of usage examples for org.jdom2 Comment getText

Introduction

In this page you can find the example usage for org.jdom2 Comment getText.

Prototype

public String getText() 

Source Link

Document

This returns the textual data within the Comment.

Usage

From source file:es.upm.dit.xsdinferencer.extraction.extractorImpl.TypesExtractorImpl.java

License:Apache License

/**
 * Recursive method that traverses an element to extract all the possible information from it.
 * It is recursive because it calls itself for each child of the element (obviously, infinite recursion 
 * is not possible as there are not, or there should not be, parent-child loops).  
 * The index of the current document is necessary in order to add well some information to 
 * the statistics./*from ww  w  .  ja  v  a2  s.  c o m*/
 * @param documentIndex index of current document
 * @param element the element to traverse (as a JDOM2 {@link Element})
 * @param enclosingComplexType the complex type which will contain the current element
 */
private void traverseElement(int documentIndex, Element element, String enclosingComplexType) {
    //Elements in the XSI namespace should be ignored
    if (element.getNamespaceURI().equalsIgnoreCase(XSI_NAMESPACE_URI))
        return;
    List<String> realPathUnfiltered = getRealPathOfElementUnfiltered(element, configuration, false,
            solvedNamespaceToPrefixMapping);
    String realPathFiltered = filterAndJoinRealPath(realPathUnfiltered);//Path for the statistics
    List<String> typePathUnfiltered = getRealPathOfElementUnfiltered(element, configuration, false,
            solvedNamespaceToPrefixMapping);
    List<String> suitablePath = getSuitablePath(typePathUnfiltered);//Path for type name inferencing
    //First, we will register the information of width and depth
    //The root is in a level whose width is 1, if we did not do the following, that width would be never registered
    if (element.isRootElement()) {
        statistics.registerWidth(documentIndex, 1);
    }
    statistics.registerDepth(documentIndex, realPathUnfiltered.size());
    int width = element.getChildren().size();
    if (width > 0) {
        statistics.registerWidth(documentIndex, width);
    }
    TypeNameInferencer typeNameInferencer = configuration.getTypeNameInferencer();
    String complexTypeName = typeNameInferencer.inferTypeName(suitablePath, configuration);//Complex type of this element
    //      //Little workaround that ensures that the same complex type is used 
    //      //when the elements on its path are the same (same name and namespace) but some of them 
    //      //use different namespace prefixes
    //      List<String> realPathUnfilteredKey=getRealPathOfElementUnfiltered(element, configuration, false, solvedNamespaceToPrefixMapping);
    //      List<String> suitablePathKey=getSuitablePath(realPathUnfilteredKey);//Path for type name inferencing
    //      String complexTypeNameKey = typeNameInferencer.inferTypeName(suitablePathKey, configuration);//Complex type of this element
    String complexTypeNameKey = complexTypeName;
    //The complex type object of this element.
    ComplexType complexType = complexTypes.get(complexTypeNameKey);
    if (complexType == null) {
        complexType = new ComplexType(complexTypeName, null, null, null);
        complexTypes.put(complexTypeNameKey, complexType); //New complex type
    }
    complexType.addSourceNodeNamespaceAndName(element.getNamespaceURI(), element.getName());
    //Comment processing
    for (Comment comment : element.getDescendants(Filters.comment())) {
        if (comment.getParentElement().equals(element))
            complexType.getComments().add(comment.getText());
    }

    //Key to find the corresponding SchemaElement
    //This key is: if the SchemaElement has an enclosing complex type (i.e., it is not a valid root), its name will be:
    //enclosingComplexType+typeNamesSeparator+elementName
    //If the element is a suitable root, the key is the name of the element.
    String schemaElementKey = (!enclosingComplexType.equals(""))
            ? enclosingComplexType + configuration.getTypeNamesAncestorsSeparator() + element.getName()
            : element.getName();
    if (configuration.getTypeNameInferencer() instanceof NameTypeNameInferencer) {
        schemaElementKey = element.getName(); //If we use a name-based type inferencer, the key is the name and we avoid problems.
    }
    SchemaElement schemaElement = elements.get(element.getNamespaceURI(), schemaElementKey);
    if (schemaElement == null) {
        schemaElement = new SchemaElement(element.getName(), element.getNamespaceURI(), complexType);//Complex type already not known.
        elements.put(element.getNamespaceURI(), schemaElementKey, schemaElement);
    }
    boolean wasAlreadyValidRoot = schemaElement.isValidRoot();
    schemaElement.setValidRoot(wasAlreadyValidRoot || element.isRootElement());

    ComplexTypeStatisticsEntry complexTypeStatisticsEntry = statistics.getComplexTypeInfo().get(complexType);
    if (complexTypeStatisticsEntry == null) {
        complexTypeStatisticsEntry = new ComplexTypeStatisticsEntry(xmlDocuments.size());
        statistics.getComplexTypeInfo().put(complexType, complexTypeStatisticsEntry);
    }

    AttributeListInferencer attributeListInferencer = attributeListInferencers.get(complexTypeName);

    if (attributeListInferencer == null) {
        attributeListInferencer = inferencersFactory.getAttributeListInferencerInstance(complexTypeName,
                configuration, solvedNamespaceToPrefixMapping, statistics);
        attributeListInferencers.put(complexTypeName, attributeListInferencer);
    }
    attributeListInferencer.learnAttributeList(element.getAttributes(), documentIndex);

    SimpleTypeInferencer simpleTypeInferencer = simpleTypeInferencersOfComplexTypes.get(complexTypeName);
    if (simpleTypeInferencer == null) {
        simpleTypeInferencer = inferencersFactory.getSimpleTypeInferencerInstance(complexTypeName,
                configuration);
        simpleTypeInferencersOfComplexTypes.put(complexTypeName, simpleTypeInferencer);
    }
    simpleTypeInferencer.learnValue(element.getText(), element.getNamespaceURI(), element.getName());

    //      SchemaElement previousChildSchemaElement=null; //We need to store the previous child in order to add the edge between it and the current child.
    List<SchemaElement> schemaElementChildren = new ArrayList<>(element.getChildren().size());
    for (int i = 0; i < element.getChildren().size(); i++) {
        Element child = element.getChildren().get(i);
        traverseElement(documentIndex, child, complexTypeName);
        String childSchemaElementKey = complexTypeName + configuration.getTypeNamesAncestorsSeparator()
                + child.getName();
        if (configuration.getTypeNameInferencer() instanceof NameTypeNameInferencer) {
            childSchemaElementKey = child.getName(); // If we use the name-based type name inferencer, the name is the key
        }
        SchemaElement childSchemaElement = elements.get(child.getNamespaceURI(), childSchemaElementKey);//The SchemaElement object does exist because the method traverseElement is called before this.
        //         if(i==0){
        //            automaton.addEdge(automaton.getInitialState(), childSchemaElement);
        //         }
        //         else {
        //            automaton.addEdge(previousChildSchemaElement, childSchemaElement);
        //            if(i==(element.getChildren().size()-1)){
        //               automaton.addEdge(childSchemaElement, automaton.getFinalState());
        //            }
        //         }
        complexTypeStatisticsEntry.registerElementCount(childSchemaElement, documentIndex);
        schemaElementChildren.add(childSchemaElement);
        //         previousChildSchemaElement=childSchemaElement;
    }

    ExtendedAutomaton automaton = automatons.get(complexTypeName);
    if (automaton == null) {
        automaton = new ExtendedAutomaton();
        SchemaElement initialState = new SchemaElement("initial", DEFAULT_PSEUDOELEMENTS_NAMESPACE, null);
        automaton.setInitialState(initialState);
        SchemaElement finalState = new SchemaElement("final", DEFAULT_PSEUDOELEMENTS_NAMESPACE, null);
        automaton.setFinalState(finalState);
        automatons.put(complexTypeName, automaton);
    }

    List<SchemaElement> schemaElementChildrenWithInitialAndFinal = new ArrayList<>(schemaElementChildren);
    schemaElementChildrenWithInitialAndFinal.add(0, automaton.getInitialState());
    schemaElementChildrenWithInitialAndFinal.add(automaton.getFinalState());
    automaton.learn(schemaElementChildrenWithInitialAndFinal);

    complexTypeStatisticsEntry.registerSubpatternsFromList(schemaElementChildren);
    complexTypeStatisticsEntry.registerValueOfNodeCount(element.getText(), schemaElement, documentIndex);

    statistics.registerElementAtPathCount(realPathFiltered, documentIndex);
    statistics.registerValueAtPathCount(realPathFiltered, element.getText(), documentIndex);
    if (enclosingComplexType.equals("")) {
        statistics.registerRootElementOccurrence(schemaElement);
    }
}

From source file:jodtemplate.pptx.postprocessor.StylePostprocessor.java

License:Apache License

private void processComment(final Comment comment, final Element at, final Slide slide,
        final Configuration configuration) throws JODTemplateException {
    String commentText = comment.getText();
    if (commentText.startsWith(STYLIZED_KEYWORD)) {
        commentText = StringUtils.removeStart(commentText, STYLIZED_KEYWORD);
        final String className = StringUtils.substringBefore(commentText, ":");
        commentText = StringUtils.removeStart(commentText, className + ": ");
        final Stylizer stylizer = configuration.getStylizer(className);
        if (stylizer == null) {
            throw new JODTemplateException("Unable to find stylizer");
        }/*from ww  w .j ava 2  s  . c o m*/
        final String text = StringUtils.removeStart(commentText, " stylized: ");
        final Element ar = at.getParentElement();
        final Element ap = ar.getParentElement();
        final int arIndex = ap.indexOf(ar);
        final Element arPr = getArPrElement(ar);
        final Element apPr = getApPrElement(ap);
        final Element sourceApPr = ObjectUtils.clone(apPr);
        cleanApPrElement(apPr);

        final List<Element> stylizedElements = stylizer.stylize(text, arPr, apPr, slide);

        ap.removeContent(ar);
        final List<Element> remains = getRemainingElements(arIndex, ap);
        for (Element el : remains) {
            ap.removeContent(el);
        }

        final int currentApIndex = injectElementsInDocument(stylizedElements, ap, apPr, arIndex);
        injectRemainsInDocument(remains, ap, sourceApPr, currentApIndex);
    }
}

From source file:mil.tatrc.physiology.datamodel.doxygen.XSDToDoxygen.java

License:Open Source License

/**
 * Obtains any XML comments sibling content that immediately preceed this
 * element/*from  www  .j  a v a2s . co m*/
 * @param element
 * @return
 */
private String getComment(Element element) {
    Parent parent = element.getParent();
    int i = parent.indexOf(element);

    StringBuffer comments = new StringBuffer();

    // Search backward from this position for preceding comments
    for (int x = i - 1; x >= 0; x--) {
        Content c = parent.getContent(x);
        if (c.getCType() == Content.CType.Comment) {
            Comment comment = (Comment) c;
            String text = comment.getText().trim();
            if (isValidComment(text)) {
                // This comment pertains to the preceding element
                if (text.startsWith("<<")) {
                    break;
                }
                if (comments.length() > 0) {
                    comments.insert(0, '\n');
                }
                comments.insert(0, text);
            }
        } else if (c.getCType() == Content.CType.Element) {
            // Stop looking when we hit another element 
            break;
        }
    }

    // Search forward from this position for inline commentx
    for (int x = i + 1; x < parent.getContentSize(); x++) {
        Content c = parent.getContent(x);
        if (c.getCType() == Content.CType.Comment) {
            Comment comment = (Comment) c;
            String text = comment.getText().trim();
            if (isValidComment(text)) {
                // This comment pertains to the preceding element
                if (text.startsWith("<<")) {
                    // Strip the "<<"
                    text = text.substring(2);
                    if (comments.length() > 0) {
                        comments.insert(0, '\n');
                    }
                    comments.insert(0, text);
                } else {
                    break;
                }
            }
        } else if (c.getCType() == Content.CType.Element) {
            // Stop looking when we hit another element 
            break;
        }
    }
    return comments.toString();
}

From source file:org.rascalmpl.library.lang.xml.DOM.java

License:Open Source License

private IConstructor convertContent(Content content, boolean trim) throws Skip {
    if (content instanceof Element) {
        return convertElement((Element) content, trim);
    }//from   ww w  .j a  va  2 s . c o  m
    if (content instanceof CDATA) { // CDATA first (is subtype of Text)
        CDATA cdata = (CDATA) content;
        return vf.constructor(Factory.Node_cdata, getString(trim, cdata));
    }
    if (content instanceof Text) {
        Text text = (Text) content;
        return vf.constructor(Factory.Node_charData, getString(trim, text));
    }
    if (content instanceof Comment) {
        Comment comment = (Comment) content;
        IString data = vf.string(comment.getText());
        return vf.constructor(Factory.Node_comment, data);
    }
    if (content instanceof ProcessingInstruction) {
        ProcessingInstruction pi = (ProcessingInstruction) content;
        IString data = vf.string(pi.getData());
        return vf.constructor(Factory.Node_pi, data);
    }
    if (content instanceof EntityRef) {
        EntityRef er = (EntityRef) content;
        IString data = vf.string(er.getName());
        return vf.constructor(Factory.Node_entityRef, data);
    }
    throw new AssertionError("cannot convert JDOM content type " + content.getClass());
}