public void writeEndElement() throws XMLStreamException {
   CurrentElement elem = elems.pop();
   unindent();
   if (elem.hasChildElements()) {
     baseWriter.writeCharacters("\n");
     writeSpaces();
   }
   baseWriter.writeEndElement();
   if (elems.empty()) {
     baseWriter.writeCharacters("\n");
   }
 }
 public void writeStartElement(
     java.lang.String prefix, java.lang.String localName, java.lang.String namespaceURI)
     throws XMLStreamException {
   QName currElemName = new QName(namespaceURI, localName);
   if (elems.empty()) {
     indentWithSpaces();
   } else {
     baseWriter.writeCharacters("");
     baseWriter.writeCharacters("\n");
     indentWithSpaces();
     CurrentElement elem = elems.peek();
     elem.setChildElements(true);
   }
   if (prefix == null && namespaceURI == null) {
     baseWriter.writeStartElement(localName);
   } else if (prefix == null) {
     baseWriter.writeStartElement(namespaceURI, localName);
   } else {
     baseWriter.writeStartElement(prefix, localName, namespaceURI);
   }
   elems.push(new CurrentElement(currElemName));
 }
示例#3
0
 /**
  * @param model
  * @param searchedQualifiedName
  * @param targets is the discoverer target hash map. This parameter is helpful to prevent
  *     discovery error caused by the visit of non valid model element.
  * @return the model element or null if not found
  */
 public static NamedElement getNamedElementByQualifiedName(
     final Model model,
     final String searchedQualifiedName,
     final Map<String, NamedElement> targets) {
   if (JavaUtil.TRACE_NE_BY_QN) {
     System.out.println(
         "Begin getNamedElementByQualifiedName(Model, String): " //$NON-NLS-1$
             + searchedQualifiedName);
   }
   NamedElement resultNamedElement = null;
   JavaUtil javaUtil = new JavaUtil();
   CurrentElement currentElement = javaUtil.new CurrentElement();
   if (searchedQualifiedName == null) {
     MoDiscoLogger.logError(
         "JavaDiscovererUtils::getNamedElementByQualifiedName(): qualifiedName parameter is null", //$NON-NLS-1$
         JavaPlugin.getDefault());
   } else {
     Iterator<Type> orphanTypes = model.getOrphanTypes().iterator();
     while (orphanTypes.hasNext()) {
       Type orphanType = orphanTypes.next();
       if (searchedQualifiedName.equals(orphanType.getName())) {
         resultNamedElement = orphanType;
       }
     }
     boolean end = false;
     while (currentElement.nextIndex() && resultNamedElement == null) {
       while (resultNamedElement == null && !end && currentElement.hasChanged2()) {
         try {
           if (currentElement.isEmpty()) {
             Iterator<Package> packages = model.getOwnedElements().iterator();
             while (packages.hasNext() && resultNamedElement == null
             /* && !currentElement.hasChanged() */ ) {
               Package packaje = packages.next();
               String currentPackageName = JavaUtil.getQualifiedName(packaje, false);
               if (searchedQualifiedName.startsWith(currentPackageName)) {
                 if (searchedQualifiedName.equals(currentPackageName)) {
                   resultNamedElement = packaje;
                 } else {
                   currentElement.setElement(packaje);
                 }
               }
             }
           } else {
             if (currentElement.getElement() instanceof Package) {
               Package packaje = (Package) currentElement.getElement();
               Iterator<Package> packages = packaje.getOwnedPackages().iterator();
               while (packages.hasNext() && resultNamedElement == null
               /* && !currentElement.hasChanged() */ ) {
                 Package subpackage = packages.next();
                 String subPackageName = JavaUtil.getQualifiedName(subpackage, false);
                 if (searchedQualifiedName.startsWith(subPackageName)) {
                   if (searchedQualifiedName.equals(subPackageName)) {
                     resultNamedElement = subpackage;
                   } else {
                     currentElement.setElement(subpackage);
                   }
                 }
               }
               Iterator<AbstractTypeDeclaration> abstractTypeDeclarations =
                   packaje.getOwnedElements().iterator();
               while (abstractTypeDeclarations.hasNext() && (resultNamedElement == null)
               /* && (!currentElement.hasChanged()) */ ) {
                 AbstractTypeDeclaration atd = abstractTypeDeclarations.next();
                 String atdName = JavaUtil.getQualifiedName(atd, false);
                 if (searchedQualifiedName.startsWith(atdName)) {
                   if (searchedQualifiedName.equals(atdName)) {
                     resultNamedElement = atd;
                   } else {
                     currentElement.setElement(atd);
                   }
                 }
               }
             } else if (currentElement.getElement() instanceof AbstractTypeDeclaration) {
               AbstractTypeDeclaration abstractTypeDeclaration =
                   (AbstractTypeDeclaration) currentElement.getElement();
               List<BodyDeclaration> bodyDeclarations =
                   abstractTypeDeclaration.getBodyDeclarations();
               // for enums, we have to retrieve enums
               // constants
               if (abstractTypeDeclaration instanceof EnumDeclaration) {
                 bodyDeclarations.addAll(
                     ((EnumDeclaration) abstractTypeDeclaration).getEnumConstants());
               }
               Iterator<BodyDeclaration> it = bodyDeclarations.iterator();
               while (it.hasNext() && resultNamedElement == null
               /* && !currentElement.hasChanged() */ ) {
                 BodyDeclaration bodyDeclaration = it.next();
                 if (bodyDeclaration instanceof AbstractTypeDeclaration) {
                   AbstractTypeDeclaration subAbstractTypeDeclaration =
                       (AbstractTypeDeclaration) bodyDeclaration;
                   String subATDQN = JavaUtil.getQualifiedName(subAbstractTypeDeclaration, false);
                   if (searchedQualifiedName.startsWith(subATDQN)) {
                     if (searchedQualifiedName.equals(subATDQN)) {
                       resultNamedElement = subAbstractTypeDeclaration;
                     } else {
                       currentElement.setElement(subAbstractTypeDeclaration);
                     }
                   }
                 } else if (!targets.containsValue(bodyDeclaration)) {
                   if (bodyDeclaration instanceof AbstractMethodDeclaration) {
                     AbstractMethodDeclaration abstractMethodDeclaration =
                         (AbstractMethodDeclaration) bodyDeclaration;
                     String amdQN = JavaUtil.getQualifiedName(abstractMethodDeclaration, true);
                     if (amdQN.contains("<<null>>")) { // $NON-NLS-1$
                       IStatus status =
                           new Status(
                               IStatus.WARNING,
                               JavaPlugin.PLUGIN_ID,
                               "Failed to compute qualifiedName:" //$NON-NLS-1$
                                   + amdQN
                                   + " ; searchedQualifiedName= " //$NON-NLS-1$
                                   + searchedQualifiedName,
                               new Exception());
                       JavaPlugin.getDefault().getLog().log(status);
                     }
                     if (searchedQualifiedName.startsWith(amdQN)) {
                       if (searchedQualifiedName.equals(amdQN)) {
                         resultNamedElement = abstractMethodDeclaration;
                       } else {
                         currentElement.setElement(abstractMethodDeclaration);
                       }
                     }
                   } else if (bodyDeclaration instanceof FieldDeclaration) {
                     FieldDeclaration fieldDeclaration = (FieldDeclaration) bodyDeclaration;
                     Iterator<VariableDeclarationFragment> variableDeclarationFragments =
                         fieldDeclaration.getFragments().iterator();
                     while (variableDeclarationFragments.hasNext()) {
                       VariableDeclarationFragment variableDeclarationFragment =
                           variableDeclarationFragments.next();
                       String variableDeclarationFragmentQN =
                           JavaUtil.getQualifiedName(variableDeclarationFragment, false);
                       if (searchedQualifiedName.equals(variableDeclarationFragmentQN)) {
                         resultNamedElement = variableDeclarationFragment;
                       }
                     }
                   } else if (bodyDeclaration instanceof EnumConstantDeclaration) {
                     EnumConstantDeclaration enumConstantDeclaration =
                         (EnumConstantDeclaration) bodyDeclaration;
                     String enumConstantQN =
                         JavaUtil.getQualifiedName(enumConstantDeclaration, false);
                     if (searchedQualifiedName.equals(enumConstantQN)) {
                       resultNamedElement = enumConstantDeclaration;
                     }
                   } else if (bodyDeclaration instanceof AnnotationTypeMemberDeclaration) {
                     AnnotationTypeMemberDeclaration annoMemberDeclaration =
                         (AnnotationTypeMemberDeclaration) bodyDeclaration;
                     String enumConstantQN =
                         JavaUtil.getQualifiedName(annoMemberDeclaration, false);
                     if (searchedQualifiedName.equals(enumConstantQN)) {
                       resultNamedElement = annoMemberDeclaration;
                     }
                   } else if (bodyDeclaration instanceof Initializer) {
                     // Nothing to do
                     assert true; // misc statement for
                     // checkstyle rule
                   } else {
                     throw new RuntimeException(
                         "Unexpected type in an AbstractTypeDeclaration contaiment: " //$NON-NLS-1$
                             + bodyDeclaration.getClass().getName());
                   }
                 }
               }
             } else {
               end = true;
             }
           }
         } catch (Exception e) {
           RuntimeException runtimeException =
               new RuntimeException(
                   "getNamedElementByQualifiedName failed while reading:" //$NON-NLS-1$
                       + JavaUtil.getQualifiedName((ASTNode) currentElement.getElement(), false),
                   e);
           IStatus status =
               new Status(
                   IStatus.ERROR,
                   JavaPlugin.PLUGIN_ID,
                   runtimeException.getMessage(),
                   runtimeException);
           JavaPlugin.getDefault().getLog().log(status);
           throw runtimeException;
         }
       }
     }
     if (JavaUtil.TRACE_NE_BY_QN) {
       System.out.println(
           "End getNamedElementByQualifiedName(Model, String): result=" //$NON-NLS-1$
               + resultNamedElement);
     }
   }
   return resultNamedElement;
 }