|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Sep 2002
Città: Monza
Messaggi: 598
|
[JAVA] costruire unalbero di parsing HTML
ciao amici,
mi è necessario analizzare delle pagine html, in particolare vorrei trovare o realizzare qualcosa che mi costruisca un albero di parsing html che mi dia una struttura da analizzare efficacemente. Dovrei essere in grado di catturare form html, l'action e gli input. Già riesco a farlo, ma le operazioni sono macchinose e nn sempre efficienti (almeno credo). Esistono per caso librerie già pronte per questo scopo? grazie e a presto
__________________
Nunc est bibendum |
|
|
|
|
|
#2 |
|
Member
Iscritto dal: May 2005
Messaggi: 80
|
Prova a guardare Xerces della Apache, mi sembra che oltre al parsing xml permetta il parsing html.
|
|
|
|
|
|
#3 |
|
Member
Iscritto dal: May 2005
Messaggi: 80
|
...per crearti un albero devi utilizzare il parsing DOM e non il SAX
|
|
|
|
|
|
#4 | |
|
Senior Member
Iscritto dal: Sep 2002
Città: Monza
Messaggi: 598
|
Quote:
Stavo vedendo le classi DOM. Conosci qualche sito dove potrei trovare qualche esempio in merito? Sinceramente nn ho capito ancora quale sia l'approccio per analizzare l'html. Presumo che dovrebbe venire tutto catturato e che automaticamente si possono estrarre le info che mi servono. Mi chiedevo però se nel caso dell'html, questi parser siano in grado di affrontare il problema di pagine html mal formate. grazie
__________________
Nunc est bibendum |
|
|
|
|
|
|
#5 |
|
Member
Iscritto dal: May 2005
Messaggi: 80
|
Quello che ho trovato per me è parsing xml con DOM su c++, non credo che faccia al caso tuo.
Per quanto ne so la libreria Xerces prevede anche metodi per la validazione, che però io ho accuratamente evitato Di esempi ne trovi anche navigando in http://xerces.apache.org/, il sito ufficiale; in alternativa tocca ricorrere a google (prova con "tutorial ...") Però aspetta, prova a quardare le 4 pagg. che partono da http://www.onlamp.com/pub/a/onlamp/2...om.html?page=1 e poi, per la visita dell'albero Codice:
import java.io.IOException; // Exception handling
import org.w3c.dom.*; // DOM interface
import org.apache.xerces.parsers.DOMParser; // Parser (to DOM)
/**************************************
class DocumentMaker encapsulates all parser dependent code.
If you change XML parsers, only this class
and the parser's import statement need be modified.
As written, DocumentMaker uses DOMParser from Apache.
**************************************/
class DocumentMaker {
private Document doc;
public Document getDocument () {return(doc);}
public DocumentMaker (String filename) {
try {
DOMParser dp = new DOMParser();
dp.parse(filename);
doc = dp.getDocument();
}
catch (Exception e) {
System.out.println("\nError: " + e.getMessage());
}
}
}
/**************************************
class NodeTypes encapsulates text names for the various node types.
Its asAscii method returns those strings according to its
nodeTypeNumber argument. It's a number to string translator.
**************************************/
class NodeTypes {
private static String[] nodenames={"ELEMENT","ATTRIBUTE","TEXT",
"CDATA_SECTION","ENTITY_REFERENCE",
"ENTITY","PROCESSING_INSTRUCTION",
"COMMENT","DOCUMENT","DOCUMENT_TYPE",
"DOCUMENT_FRAGMENT","NOTATION"};
public static String asAscii(int nodeTypeNumber) {
return(nodenames[nodeTypeNumber-1]);
}
}
/**************************************
class DOMwalker's job is to walk the DOM document
and print out each node's
type, its name, its value (null for Elements), and
in the case of elements, its attributes in parentheses.
The tree is walked non-recursively using standard DOM
traversal methods.
**************************************/
class DOMwalker {
private Node checker; // like a checker that gets moved from
// square to square in a checkers game
// points to "current" node
private void indentToLevel(int level) {
for(int n=0; n < level; n++) {
System.out.print(" ");
}
}
private void printNodeInfo(Node thisNode) {
System.out.print(NodeTypes.asAscii(thisNode.getNodeType()) + " : " +
thisNode.getNodeName() + " : " +
thisNode.getNodeValue() + " : ");
System.out.println();
}
!!!!!!!!!!!!!!!!! UTILISSIMO !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
public DOMwalker(Document doc) {
boolean ascending = false;
int level = 1;
System.out.println();
try {
checker=doc.getDocumentElement();
while (true) {
//*** TAKE ACTION ON NODE WITH CHECKER ***
if (!ascending) {
indentToLevel(level);
printNodeInfo(checker);
}
//*** GO DOWN IF YOU CAN ***
if ((checker.hasChildNodes()) && (!ascending)) {
checker=checker.getFirstChild();
ascending = false;
level++;
}
//*** OTHERWISE GO RIGHT IF YOU CAN ***
else if (checker.getNextSibling() != null) {
checker=checker.getNextSibling();
ascending = false;
}
//*** OTHERWISE GO UP IF YOU CAN ***
else if (checker.getParentNode() != null) {
checker=checker.getParentNode();
ascending = true;
level--;
}
//*** OTHERWISE YOU'VE ASCENDED BACK TO ***
//*** THE DOCUMENT ELEMENT, SO YOU'RE DONE ***
else {
break;
}
}
}
catch (Exception e) {
System.out.println("\nError: " + e.getMessage());
}
}
}
/**************************************
Class Hello is the repository of this program's main routine.
It removes empty text nodes, then walks the DOM tree and
prints out the DOM tree's info.
**************************************/
class Hello {
public static void main(String[] args) {
String filename = args[0];
System.out.print("Walking XML file " + filename + " ... ");
DocumentMaker docMaker = new DocumentMaker(filename);
Document doc = docMaker.getDocument();
try {
DOMwalker walker = new DOMwalker(doc);
}
catch (Exception e) {
System.out.println("\nError: " + e.getMessage());
}
}
}
Quello che brevemente posso anticiparti è che effettuando il parsing con il Document Object Model (DOM) ottieni un albero nel quale la radice è il documento stesso, e i nodi figli sono corrispondenti ai tag del documento. Se ti serve qualche altra delucidazione chiedi pure, ciao |
|
|
|
|
|
#6 |
|
Senior Member
Iscritto dal: Sep 2002
Città: Monza
Messaggi: 598
|
una idea me la sono fatta.
In pratica il mio obiettivo sarebbe questo: 1.catturare una pagina html (e so farlo) in java 2.creare un oggetto Albero con l'aiuto delle Dom (non so come) 3.interrogare opportunamente l'albero (ricerca) e avere metodi che possano restituirmi facilmente, ad esempio, l'action o il value degli input di una form Il punto 2 è cruciale ed avrei bisogno di aiuto. Sto scaricando la lib dal sito dell'xerces (ho un fulmine di 56k). ho visto le classi della libreria org.w3c.dom.html e ho notato proprio le interfacce che servirebbero a me (HTMLFormElement e HTMLInputElement) con relativi metodi. E qui nasce un altro problema, devo andare a riguardare come si crea una classe che implementi un'interface. Devo fare l'overloading dei metodi dell'interfaccia, ma nn so in che modo. Qualcuno mi consiglia una strada rapida, tempus fugit. grazie
__________________
Nunc est bibendum |
|
|
|
|
|
#7 |
|
Member
Iscritto dal: May 2005
Messaggi: 80
|
Ecco la funzione all'interno della quale effettuo il parsing di un file xmi (XML Metadata Interchange), ma come noterai è c++.
Spero ti sia comunque di aiuto. Codice:
/*************** void CreateModel(char* _modelFile, char* _fileScenario)
legge il file xmi del modello e crea la struttura della macchina a stati
conseguentemente
******************/
void StateMachine::CreateModel(char* _modelFile, char* _fileScenario)
{
/** inizializzo il parsing XML */
try { XMLPlatformUtils::Initialize(); }
catch(XMLException& toCatch)
{
char* message = XMLString::transcode(toCatch.getMessage());
std::cout << "XML toolkit initialization error: " << message << std::endl;
XMLString::release(&message);
}
/** creo un'istanza del parser DOM */
XercesDOMParser* parser = new XercesDOMParser();
// disabilito la validazione dei documenti XML
parser->setValidationScheme(XercesDOMParser::Val_Never);
parser->setDoSchema(false);
parser->setLoadExternalDTD(false);
try { parser->parse(_modelFile); }
catch (const XMLException& toCatch)
{
char* message = XMLString::transcode(toCatch.getMessage());
std::cout << "Exception message is: " << message << std::endl;
XMLString::release(&message);
}
catch (const DOMException& toCatch)
{
char* message = XMLString::transcode(toCatch.msg);
std::cout << "Exception message is: " << message << std::endl;
XMLString::release(&message);
}
catch (...)
{
std::cout << "Unexpected Exception" << std::endl;
}
/** creo un'istanza del builder DOM */
XMLCh tempStr[100];
XMLString::transcode("LS", tempStr, 99);
DOMImplementation* impl =
DOMImplementationRegistry::getDOMImplementation(tempStr);
DOMBuilder* builder = ((DOMImplementationLS*)impl)->
createDOMBuilder(DOMImplementationLS::MODE_SYNCHRONOUS, 0);
DOMDocument* modelDoc = 0;
try { modelDoc = builder->parseURI(_modelFile); }
catch (const XMLException& toCatch)
{
char* message = XMLString::transcode(toCatch.getMessage());
std::cout << "Exception message is: " << message << std::endl;
XMLString::release(&message);
}
catch (const DOMException& toCatch)
{
char* message = XMLString::transcode(toCatch.msg);
std::cout << "Exception message is: " << message << std::endl;
XMLString::release(&message);
}
catch (...)
{
std::cout << "Unexpected Exception" << std::endl;
}
if ( modelDoc == 0 )
{
std::cout << builder << std::endl;
std::cout << modelDoc << std::endl;
std::cout << "non ha tirato su il file!!!!!!" << std::endl;
}
/** Nomi dei nodi */
char* cAction = "UML2:OpaqueBehavior";
const XMLCh* action = XMLString::transcode(cAction);
char* cDeferrableEvent = "UML2:State.doActivity";
const XMLCh* deferrableEvent = XMLString::transcode(cDeferrableEvent);
char* cDiagram = "UML:Diagram";
const XMLCh* diagram = XMLString::transcode(cDiagram);
char* cEntryAction = "UML2:State.entry";
const XMLCh* entryAction = XMLString::transcode(cEntryAction);
char* cExitAction = "UML2:State.exit";
const XMLCh* exitAction = XMLString::transcode(cExitAction);
char* cFinalState = "UML2:FinalState";
const XMLCh* finalState = XMLString::transcode(cFinalState);
char* cGuard = "UML2:Constraint";
const XMLCh* guard = XMLString::transcode(cGuard);
char* cPseudostate = "UML2:Pseudostate";
const XMLCh* pseudostate = XMLString::transcode(cPseudostate);
char* cRegion = "UML2:Region";
const XMLCh* region = XMLString::transcode(cRegion);
char* cRegionDot = "UML2:Region.";
const XMLCh* regionDot = XMLString::transcode(cRegionDot);
char* cRegionSlash = "/UML2:Region";
const XMLCh* regionSlash = XMLString::transcode(cRegionSlash);
char* cRegionTransition = "UML2:Region.transition";
const XMLCh* regionTransition = XMLString::transcode(cRegionTransition);
char* cRegionTransitionSlash = "/UML2:Region.transition";
const XMLCh* regionTransitionSlash =
XMLString::transcode(cRegionTransitionSlash);
char* cState = "UML2:State";
const XMLCh* state = XMLString::transcode(cState);
char* cStateDot = "UML2:State.";
const XMLCh* stateDot = XMLString::transcode(cStateDot);
char* cStateSlash = "/UML2:State";
const XMLCh* stateSlash = XMLString::transcode(cStateSlash);
char* cStateMachine = "UML2:StateMachine";
const XMLCh* stateMachine = XMLString::transcode(cStateMachine);
char* cStateMachineDot = "UML2:StateMachine.";
const XMLCh* stateMachineDot = XMLString::transcode(cStateMachineDot);
char* cStateMachineSlash = "/UML2:StateMachine";
const XMLCh* stateMachineSlash = XMLString::transcode(cStateMachineSlash);
char* cTransition = "UML2:Transition";
const XMLCh* transition = XMLString::transcode(cTransition);
char* cTransitionDot = "UML2:Transition.";
const XMLCh* transitionDot = XMLString::transcode(cTransitionDot);
char* cTransitionSlash = "/UML2:Transition";
const XMLCh* transitionSlash = XMLString::transcode(cTransitionSlash);
char* cTransitionEffect = "UML2:Transition.effect";
const XMLCh* transitionEffect = XMLString::transcode(cTransitionEffect);
char* cTransitionGuard = "UML2:Transition.guard";
const XMLCh* transitionGuard = XMLString::transcode(cTransitionGuard);
char* cTransitionSource = "UML2:Transition.source";
const XMLCh* transitionSource = XMLString::transcode(cTransitionSource);
char* cTransitionTarget = "UML2:Transition.target";
const XMLCh* transitionTarget = XMLString::transcode(cTransitionTarget);
char* cTransitionTrigger = "UML2:Transition.trigger";
const XMLCh* transitionTrigger = XMLString::transcode(cTransitionTrigger);
char* cTrigger = "UML2:CallTrigger";
const XMLCh* trigger = XMLString::transcode(cTrigger);
char* cTriggerZone = "UML2:BehavioredClassifier.ownedTrigger";
const XMLCh* triggerZone = XMLString::transcode(cTriggerZone);
/** Nomi degli attributi */
char* cKind = "kind";
const XMLCh* kind = XMLString::transcode(cKind);
char* cName = "name";
const XMLCh* name = XMLString::transcode(cName);
char* cXMIID = "xmi.id";
const XMLCh* xmiID = XMLString::transcode(cXMIID);
char* cXMIIDref = "xmi.idref";
const XMLCh* xmiIDref = XMLString::transcode(cXMIIDref);
/** Strutture dati */
enum element {eRegion, eAndState, eOrState} lastElement;
DOMElement* childNodeElement = 0;
DOMElement* currentElement = 0;
DOMElement* fatherNodeElement = 0;
DOMElement* sourceNodeElement = 0;
DOMElement* targetNodeElement = 0;
DOMElement* triggerZoneNodeElement = 0;
DOMNode* childNode = 0;
DOMNode* currentNode = 0;
DOMNode* fatherNode = 0;
DOMNode* regionNode = 0;
DOMNode* sourceNode = 0;
DOMNode* targetNode = 0;
DOMNode* triggerZoneNode = 0;
DOMNodeList* currentNodeChildrenList = 0;
DOMNodeList* triggersList = 0;
const XMLCh* currentAttribute = 0;
char* currentKind = "";
char* currentName = "";
char* currentUUID = "";
bool ascending(false); // direzione di visita dell'albero
bool found(false);
bool modelZone(true); // serve ad evitare le informazioni grafiche
bool transitionsZone(false); // indica se vanno acquisite le transizioni
map<string, int>::iterator msiit;
map<string, vector<int> >::iterator msviit;
stack<int> fathersIDs;
stack<int> fathersTypes;
fathersIDs.push(0);
lastElement = eOrState;
fathersTypes.push(lastElement);
/** Visita dell'albero del documento */
currentElement = modelDoc->getDocumentElement();
currentNode = currentElement;
// acquisisco la lista dei trigger
currentNodeChildrenList = currentElement->
getElementsByTagName(triggerZone);
if ( currentNodeChildrenList->getLength() > 0 )
triggerZoneNode = currentNodeChildrenList->item(0);
if ( triggerZoneNode != 0 )
{
if ( DOMNode::ELEMENT_NODE == triggerZoneNode->getNodeType() )
triggerZoneNodeElement = dynamic_cast<DOMElement*>(triggerZoneNode);
triggersList = triggerZoneNodeElement->getElementsByTagName(trigger);
}
// visita depht first anticipata
while ( modelZone )
{
if ( DOMNode::ELEMENT_NODE == currentNode->getNodeType() )
currentElement = dynamic_cast<DOMElement*>(currentNode);
if ( !ascending ) // quindi nodo visitato per la prima volta
{
childNode = 0;
childNodeElement = 0;
/** TRANSITION */
if ( XMLString::equals(currentNode->getNodeName(), transition) &&
!(XMLString::equals(currentNode->getNodeName(), transitionDot)) &&
!(XMLString::equals(currentNode->getNodeName(), transitionSlash)) &&
transitionsZone )
{
// acquisisco l'UUID della transizione
char* transitionUUID = "";
char* triggerParam = "";
char* guardParam = "";
char* actionParam = "";
int sourceStateID = -1;
int targetStateID = -1;
currentAttribute = currentElement->getAttribute(xmiID);
transitionUUID = XMLString::transcode(currentAttribute);
// acquisisco il trigger della transizione
currentNodeChildrenList = currentElement->
getElementsByTagName(transitionTrigger);
if ( (currentNodeChildrenList->getLength() > 0) &&
(triggerZoneNode != 0) )
{
found = false;
childNode = currentNodeChildrenList->item(0);
if ( DOMNode::ELEMENT_NODE == childNode->getNodeType() )
childNodeElement = dynamic_cast<DOMElement*>(childNode);
currentNodeChildrenList = childNodeElement->
getElementsByTagName(trigger);
childNode = currentNodeChildrenList->item(0);
if ( DOMNode::ELEMENT_NODE == childNode->getNodeType() )
childNodeElement = dynamic_cast<DOMElement*>(childNode);
currentAttribute = childNodeElement->getAttribute(xmiIDref);
currentUUID = XMLString::transcode(currentAttribute);
for ( int i=0; (i<(triggersList->getLength()))&&(!found); i++ )
{
childNode = triggersList->item(i);
if ( DOMNode::ELEMENT_NODE == childNode->getNodeType() )
childNodeElement = dynamic_cast<DOMElement*>(childNode);
currentAttribute = childNodeElement->getAttribute(xmiID);
if ( XMLString::equals(XMLString::transcode(currentAttribute),
currentUUID) )
{
currentAttribute = childNodeElement->getAttribute(name);
triggerParam = XMLString::transcode(currentAttribute);
found = true;
}
}
}
// acquisisco la guardia della transizione
currentNodeChildrenList = currentElement->
getElementsByTagName(transitionGuard);
if ( currentNodeChildrenList->getLength() > 0 )
{
childNode = currentNodeChildrenList->item(0);
if ( DOMNode::ELEMENT_NODE == childNode->getNodeType() )
childNodeElement = dynamic_cast<DOMElement*>(childNode);
currentNodeChildrenList = childNodeElement->
getElementsByTagName(guard);
childNode = currentNodeChildrenList->item(0);
if ( DOMNode::ELEMENT_NODE == childNode->getNodeType() )
childNodeElement = dynamic_cast<DOMElement*>(childNode);
currentAttribute = childNodeElement->getAttribute(name);
guardParam = XMLString::transcode(currentAttribute);
}
// acquisisco la action della transizione
currentNodeChildrenList = currentElement->
getElementsByTagName(transitionEffect);
if ( currentNodeChildrenList->getLength() > 0 )
{
childNode = currentNodeChildrenList->item(0);
if ( DOMNode::ELEMENT_NODE == childNode->getNodeType() )
childNodeElement = dynamic_cast<DOMElement*>(childNode);
currentNodeChildrenList = childNodeElement->
getElementsByTagName(action);
childNode = currentNodeChildrenList->item(0);
if ( DOMNode::ELEMENT_NODE == childNode->getNodeType() )
childNodeElement = dynamic_cast<DOMElement*>(childNode);
currentAttribute = childNodeElement->getAttribute(name);
actionParam = XMLString::transcode(currentAttribute);
}
// determino se la transizione è internal o meno
currentAttribute = currentElement->getAttribute(kind);
currentKind = XMLString::transcode(currentAttribute);
// INTERNAL
if ( XMLString::equals(currentKind, "internal") )
{
// trovo lo stato di cui fa parte
currentNodeChildrenList = currentElement->
getElementsByTagName(transitionSource);
childNode = currentNodeChildrenList->item(0);
if ( DOMNode::ELEMENT_NODE == childNode->getNodeType() )
childNodeElement = dynamic_cast<DOMElement*>(childNode);
currentNodeChildrenList = childNodeElement->
getElementsByTagName(state);
sourceNode = currentNodeChildrenList->item(0);
if ( DOMNode::ELEMENT_NODE == sourceNode->getNodeType() )
sourceNodeElement = dynamic_cast<DOMElement*>(sourceNode);
currentAttribute = sourceNodeElement->getAttribute(xmiIDref);
currentUUID = XMLString::transcode(currentAttribute);
msiit = statesMapping.find(currentUUID);
if ( msiit != statesMapping.end() )
{
sourceStateID = (*msiit).second;
AddTransition(transitionUUID, sourceStateID, sourceStateID,
triggerParam, guardParam, actionParam, true);
if ( (!(EvaluateEvent(triggerParam, sourceStateID,
transitionsIndex-1, true, microStep))) ||
(!(EvaluateEvent(guardParam, sourceStateID,
transitionsIndex-1, true, microStep))) ||
(!(EvaluateAction(actionParam))) )
{
std::cout << "ERRORE NELLA TRANSIZIONE " << transitionUUID;
std::cout << std::endl;
}
}
}
else // EXTERNAL
{
// trovo il source della transizione
currentNodeChildrenList = currentElement->
getElementsByTagName(transitionSource);
sourceNode = currentNodeChildrenList->item(0);
if ( DOMNode::ELEMENT_NODE == sourceNode->getNodeType() )
sourceNodeElement = dynamic_cast<DOMElement*>(sourceNode);
currentNodeChildrenList = sourceNodeElement->
getElementsByTagName(state);
// se il source è uno state
if ( currentNodeChildrenList->getLength() > 0 )
{
sourceNode = currentNodeChildrenList->item(0);
if ( DOMNode::ELEMENT_NODE == sourceNode->getNodeType() )
sourceNodeElement = dynamic_cast<DOMElement*>(sourceNode);
currentAttribute = sourceNodeElement->getAttribute(xmiIDref);
currentUUID = XMLString::transcode(currentAttribute);
msiit = statesMapping.find(currentUUID);
if ( msiit != statesMapping.end() )
sourceStateID = (*msiit).second;
// trovo il target della transizione
currentNodeChildrenList = currentElement->
getElementsByTagName(transitionTarget);
targetNode = currentNodeChildrenList->item(0);
if ( DOMNode::ELEMENT_NODE == targetNode->getNodeType() )
targetNodeElement = dynamic_cast<DOMElement*>(targetNode);
currentNodeChildrenList = targetNodeElement->
getElementsByTagName(state);
// se il target è uno state
if ( currentNodeChildrenList->getLength() > 0 )
{
targetNode = currentNodeChildrenList->item(0);
if ( DOMNode::ELEMENT_NODE == targetNode->getNodeType() )
targetNodeElement = dynamic_cast<DOMElement*>(targetNode);
currentAttribute = targetNodeElement->getAttribute(xmiIDref);
currentUUID = XMLString::transcode(currentAttribute);
msiit = statesMapping.find(currentUUID);
if ( msiit != statesMapping.end() )
{
targetStateID = (*msiit).second;
AddTransition(transitionUUID, sourceStateID, targetStateID,
triggerParam, guardParam, actionParam, false);
if ( (!(EvaluateEvent(triggerParam, sourceStateID,
transitionsIndex-1, true, microStep))) ||
(!(EvaluateEvent(guardParam, sourceStateID,
transitionsIndex-1, true, microStep))) ||
(!(EvaluateAction(actionParam))) )
{
std::cout << "ERRORE NELLA TRANSIZIONE " << transitionUUID;
std::cout << std::endl;
}
}
}
else // se il target non è uno state
{
currentNodeChildrenList = targetNodeElement->
getElementsByTagName(pseudostate);
// se il target è uno pseudostate
if ( currentNodeChildrenList->getLength() > 0 )
{
targetNode = currentNodeChildrenList->item(0);
if ( DOMNode::ELEMENT_NODE == targetNode->getNodeType() )
targetNodeElement = dynamic_cast<DOMElement*>(targetNode);
currentAttribute = targetNodeElement->getAttribute(xmiIDref);
currentUUID = XMLString::transcode(currentAttribute);
// target choice o junction
if ( choicesJunctions.find(currentUUID) !=
choicesJunctions.end() )
{
msviit = cjInc.find(currentUUID);
if ( msviit == cjInc.end() )
{
vector<int> trvec;
trvec.push_back(transitionsIndex);
cjInc.insert(make_pair(currentUUID, trvec));
}
else
(*msviit).second.push_back(transitionsIndex);
AddTransition(transitionUUID, sourceStateID, -1, triggerParam,
guardParam, actionParam, false);
if ( (!(EvaluateEvent(triggerParam, sourceStateID,
transitionsIndex-1, true, microStep))) ||
(!(EvaluateEvent(guardParam, sourceStateID,
transitionsIndex-1, true, microStep))) ||
(!(EvaluateAction(actionParam))) )
{
std::cout << "ERRORE NELLA TRANSIZIONE " << transitionUUID;
std::cout << std::endl;
}
}
// target fork
else if ( forksInc.find(currentUUID) != forksInc.end() )
{
msiit = forksInc.find(currentUUID);
if ( (*msiit).second == (-1) )
{
(*msiit).second = transitionsIndex;
msiit = forksOutc.find(currentUUID);
targetStateID = (*msiit).second;
AddTransition(transitionUUID, sourceStateID,
targetStateID, triggerParam, guardParam,
actionParam, false);
if ( (!(EvaluateEvent(triggerParam, sourceStateID,
transitionsIndex-1, true, microStep))) ||
(!(EvaluateEvent(guardParam, sourceStateID,
transitionsIndex-1, true, microStep))) ||
(!(EvaluateAction(actionParam))) )
{
std::cout << "ERRORE NELLA TRANSIZIONE ";
std::cout << transitionUUID << std::endl;
}
}
}
// target join
else if ( joinsInc.find(currentUUID) != joinsInc.end() )
{
msiit = joinsInc.find(currentUUID);
int fatherID = GetState(sourceStateID)->GetAncestor(0);
if ( ((*msiit).second == (-1)) &&
(GetState(fatherID)->IsOrthogonal()) )
{
sourceStateID = GetState(fatherID)->GetAncestor(0);
(*msiit).second = transitionsIndex;
msiit = joinsOutc.find(currentUUID);
if ( (*msiit).second != (-1) )
{
targetStateID = (*msiit).second;
AddTransition(transitionUUID, sourceStateID,
targetStateID, triggerParam, guardParam,
actionParam, false);
}
else
{
targetStateID = GetState(sourceStateID)->GetAncestor(0);
AddTransition(transitionUUID, sourceStateID,
targetStateID, triggerParam, guardParam,
actionParam, false);
}
if ( (!(EvaluateEvent(triggerParam, sourceStateID,
transitionsIndex-1, true, microStep))) ||
(!(EvaluateEvent(guardParam, sourceStateID,
transitionsIndex-1, true, microStep))) ||
(!(EvaluateAction(actionParam))) )
{
std::cout << "ERRORE NELLA TRANSIZIONE ";
std::cout << transitionUUID << std::endl;
}
}
}
}
else // se il target non è né uno state né uno pseudostate
{
currentNodeChildrenList = targetNodeElement->
getElementsByTagName(finalState);
// se il target è un final state
if ( currentNodeChildrenList->getLength() > 0 )
{
targetStateID = GetState(sourceStateID)->GetAncestor(0);
AddTransition(transitionUUID, sourceStateID, targetStateID,
triggerParam, guardParam, actionParam, false);
if ( (!(EvaluateEvent(triggerParam, sourceStateID,
transitionsIndex-1, true, microStep))) ||
(!(EvaluateEvent(guardParam, sourceStateID,
transitionsIndex-1, true, microStep))) ||
(!(EvaluateAction(actionParam))) )
{
std::cout << "ERRORE NELLA TRANSIZIONE " << transitionUUID;
std::cout << std::endl;
}
}
}
}
}
else // se il source non è uno state
{
currentNodeChildrenList = sourceNodeElement->
getElementsByTagName(pseudostate);
// se il source è uno pseudostate
if ( currentNodeChildrenList->getLength() > 0 )
{
sourceNode = currentNodeChildrenList->item(0);
if ( DOMNode::ELEMENT_NODE == sourceNode->getNodeType() )
sourceNodeElement = dynamic_cast<DOMElement*>(sourceNode);
// determino il target della transizione
currentNodeChildrenList = currentElement->
getElementsByTagName(transitionTarget);
targetNode = currentNodeChildrenList->item(0);
if ( DOMNode::ELEMENT_NODE == targetNode->getNodeType() )
targetNodeElement = dynamic_cast<DOMElement*>(targetNode);
currentNodeChildrenList = targetNodeElement->
getElementsByTagName(state);
// se il target è uno state
if ( currentNodeChildrenList->getLength() > 0 )
{
targetNode = currentNodeChildrenList->item(0);
if ( DOMNode::ELEMENT_NODE == targetNode->getNodeType() )
targetNodeElement = dynamic_cast<DOMElement*>(targetNode);
currentAttribute = targetNodeElement->getAttribute(xmiIDref);
currentUUID = XMLString::transcode(currentAttribute);
msiit = statesMapping.find(currentUUID);
if ( msiit != statesMapping.end() )
targetStateID = (*msiit).second;
}
currentAttribute = sourceNodeElement->getAttribute(xmiIDref);
currentUUID = XMLString::transcode(currentAttribute);
// il source è un initial pseudostate
if ( initialTransitions.find(currentUUID) !=
initialTransitions.end() )
{
sourceStateID = (*msiit).second;
if ( targetStateID != (-1) )
{
GetState(sourceStateID)->
SetInitialTransition(transitionsIndex);
GetState(sourceStateID)->SetInitialSon(targetStateID);
AddTransition(transitionUUID, sourceStateID, targetStateID,
triggerParam, guardParam, actionParam, false);
if ( (!(EvaluateEvent(triggerParam, sourceStateID,
transitionsIndex-1, true, microStep))) ||
(!(EvaluateEvent(guardParam, sourceStateID,
transitionsIndex-1, true, microStep))) ||
(!(EvaluateAction(actionParam))) )
{
std::cout << "ERRORE NELLA TRANSIZIONE " << transitionUUID;
std::cout << std::endl;
}
}
}
// il source è un choice o junction
else if ( choicesJunctions.find(currentUUID) !=
choicesJunctions.end() )
{
if ( targetStateID != (-1) )
{
msviit = cjOutc.find(currentUUID);
if ( msviit == cjOutc.end() )
{
vector<int> trvec;
trvec.push_back(transitionsIndex);
cjOutc.insert(make_pair(currentUUID, trvec));
}
else
(*msviit).second.push_back(transitionsIndex);
AddTransition(transitionUUID, -1, targetStateID,
triggerParam, guardParam, actionParam, false);
if ( (!(EvaluateEvent(triggerParam, sourceStateID,
transitionsIndex-1, true, microStep))) ||
(!(EvaluateEvent(guardParam, sourceStateID,
transitionsIndex-1, true, microStep))) ||
(!(EvaluateAction(actionParam))) )
{
std::cout << "ERRORE NELLA TRANSIZIONE ";
std::cout << transitionUUID << std::endl;
}
}
}
// il source è un fork
else if ( forksOutc.find(currentUUID) != forksOutc.end() )
{
msiit = forksOutc.find(currentUUID);
if ( ((*msiit).second == (-1)) && (targetStateID != (-1)) )
{
int fatherID = GetState(targetStateID)->GetAncestor(0);
if ( GetState(fatherID)->IsOrthogonal() )
{
targetStateID = GetState(fatherID)->GetAncestor(0);
(*msiit).second = targetStateID;
msiit = forksInc.find(currentUUID);
if ( (*msiit).second != (-1) )
{
Transition* trf = GetTransition((*msiit).second);
if ( trf->GetTargetState() == (-1) )
trf->SetTargetState(targetStateID);
}
}
}
}
// il source è un join
else if ( joinsOutc.find(currentUUID) != joinsOutc.end() )
{
msiit = joinsOutc.find(currentUUID);
if ( ((*msiit).second == (-1)) && (targetStateID != (-1)) )
{
(*msiit).second = targetStateID;
msiit = joinsInc.find(currentUUID);
if ( (*msiit).second != (-1) )
{
Transition* trj = GetTransition((*msiit).second);
int src = trj->GetSourceState();
if ( trj->GetTargetState() ==
GetState(src)->GetAncestor(0) )
trj->SetTargetState(targetStateID);
}
}
}
}
}
}
}
/** STATE */
else if ( XMLString::equals(currentNode->getNodeName(), state) &&
!(XMLString::equals(currentNode->getNodeName(), stateDot)) &&
!(XMLString::equals(currentNode->getNodeName(), stateSlash)) &&
!(transitionsZone) )
{
// acquisizione nome e UUID
currentAttribute = currentElement->getAttribute(name);
currentName = XMLString::transcode(currentAttribute);
currentAttribute = currentElement->getAttribute(xmiID);
currentUUID = XMLString::transcode(currentAttribute);
// creazione dello stato
if ( fathersTypes.top() == eAndState )
AddState(currentUUID, currentName, GetState(fathersIDs.top()), true);
else if ( (fathersTypes.top() == eOrState) ||
(fathersTypes.top() == eRegion) )
AddState(currentUUID, currentName, GetState(fathersIDs.top()), false);
UpdateStatesMapping(currentUUID, (statesIndex-1));
DD.UpdateStateNames(currentName, (statesIndex-1));
// acquisizione entry action
currentNodeChildrenList = currentElement->
getElementsByTagName(entryAction);
if ( currentNodeChildrenList->getLength() > 0 )
{
for ( int i=0; i<currentNodeChildrenList->getLength(); i++ )
{
childNode = currentNodeChildrenList->item(i);
fatherNode = childNode->getParentNode();
if ( fatherNode->isSameNode(currentNode) )
{
char* entryActionParam = "";
if ( DOMNode::ELEMENT_NODE == childNode->getNodeType() )
childNodeElement = dynamic_cast<DOMElement*>(childNode);
currentNodeChildrenList = childNodeElement->
getElementsByTagName(action);
childNode = currentNodeChildrenList->item(0);
if ( DOMNode::ELEMENT_NODE == childNode->getNodeType() )
childNodeElement = dynamic_cast<DOMElement*>(childNode);
currentAttribute = childNodeElement->getAttribute(name);
entryActionParam = XMLString::transcode(currentAttribute);
if ( !(EvaluateAction(entryActionParam)) )
{
std::cout << "ERRORE NELLA ENTRY ACTION DELLO STATO ";
std::cout << (statesIndex-1) << std::endl;
}
GetState(statesIndex-1)->AddEntryAction(entryActionParam);
}
}
}
// acquisizione exit action
currentNodeChildrenList = currentElement->
getElementsByTagName(exitAction);
if ( currentNodeChildrenList->getLength() > 0 )
{
for ( int i=0; i<currentNodeChildrenList->getLength(); i++ )
{
childNode = currentNodeChildrenList->item(i);
fatherNode = childNode->getParentNode();
if ( fatherNode->isSameNode(currentNode) )
{
char* exitActionParam = "";
if ( DOMNode::ELEMENT_NODE == childNode->getNodeType() )
childNodeElement = dynamic_cast<DOMElement*>(childNode);
currentNodeChildrenList = childNodeElement->
getElementsByTagName(action);
childNode = currentNodeChildrenList->item(0);
if ( DOMNode::ELEMENT_NODE == childNode->getNodeType() )
childNodeElement = dynamic_cast<DOMElement*>(childNode);
currentAttribute = childNodeElement->getAttribute(name);
exitActionParam = XMLString::transcode(currentAttribute);
if ( !(EvaluateAction(exitActionParam)) )
{
std::cout << "ERRORE NELLA EXIT ACTION DELLO STATO ";
std::cout << (statesIndex-1) << std::endl;
}
GetState(statesIndex-1)->AddExitAction(exitActionParam);
}
}
}
// acquisizione deferrable event
currentNodeChildrenList = currentElement->
getElementsByTagName(deferrableEvent);
if ( currentNodeChildrenList->getLength() > 0 )
{
for ( int i=0; i<currentNodeChildrenList->getLength(); i++ )
{
childNode = currentNodeChildrenList->item(i);
fatherNode = childNode->getParentNode();
if ( fatherNode->isSameNode(currentNode) )
{
char* deferrableEventParam = "";
if ( DOMNode::ELEMENT_NODE == childNode->getNodeType() )
childNodeElement = dynamic_cast<DOMElement*>(childNode);
currentNodeChildrenList = childNodeElement->
getElementsByTagName(action);
childNode = currentNodeChildrenList->item(0);
if ( DOMNode::ELEMENT_NODE == childNode->getNodeType() )
childNodeElement = dynamic_cast<DOMElement*>(childNode);
currentAttribute = childNodeElement->getAttribute(name);
deferrableEventParam = XMLString::transcode(currentAttribute);
GetState(statesIndex-1)->AddDeferrableEvent(deferrableEventParam);
}
}
}
// determinazione del tipo di state
currentNodeChildrenList = currentElement->
getElementsByTagName(region);
if ( currentNodeChildrenList->getLength() == 1 )
{
fathersIDs.push(statesIndex-1);
lastElement = eOrState;
fathersTypes.push(lastElement);
}
else if ( currentNodeChildrenList->getLength() > 1 )
{
const XMLCh* currentFather = 0;
int numberOfRegions = 1;
regionNode = 0;
fatherNode = 0;
fatherNodeElement = 0;
regionNode = currentNodeChildrenList->item(0);
fatherNode = regionNode->getParentNode();
fatherNode = fatherNode->getParentNode();
if ( DOMNode::ELEMENT_NODE == fatherNode->getNodeType() )
fatherNodeElement = dynamic_cast<DOMElement*>(fatherNode);
currentFather = fatherNodeElement->getAttribute(xmiID);
for ( int i=1; i<(currentNodeChildrenList->getLength()); i++ )
{
regionNode = currentNodeChildrenList->item(i);
fatherNode = regionNode->getParentNode();
fatherNode = fatherNode->getParentNode();
if ( DOMNode::ELEMENT_NODE == fatherNode->getNodeType() )
fatherNodeElement = dynamic_cast<DOMElement*>(fatherNode);
if ( XMLString::equals(fatherNodeElement->getAttribute(xmiID),
currentFather) )
numberOfRegions++;
}
if ( numberOfRegions > 1 )
lastElement = eAndState;
else
lastElement = eOrState;
fathersIDs.push(statesIndex-1);
fathersTypes.push(lastElement);
}
}
/** PSEUDOSTATE */
else if ( XMLString::equals(currentNode->getNodeName(), pseudostate) &&
!(transitionsZone) )
{
currentAttribute = currentElement->getAttribute(kind);
currentKind = XMLString::transcode(currentAttribute);
// INITIAL
if ( XMLString::equals(currentKind, "initial") )
{
DOMNode* initialTransitionNode = 0;
DOMElement* initialTransitionNodeElement = 0;
currentNodeChildrenList = currentElement->
getElementsByTagName(transition);
initialTransitionNode = currentNodeChildrenList->item(0);
if ( DOMNode::ELEMENT_NODE == initialTransitionNode->getNodeType() )
initialTransitionNodeElement =
dynamic_cast<DOMElement*>(initialTransitionNode);
currentAttribute = initialTransitionNodeElement->
getAttribute(xmiIDref);
currentUUID = XMLString::transcode(currentAttribute);
initialTransitions.insert(make_pair(currentUUID, fathersIDs.top()));
}
// DEEP HISTORY
else if ( XMLString::equals(currentKind, "deepHistory") )
{
GetState(fathersIDs.top())->SetDeepHistory(true);
}
// SHALLOW HISTORY
else if ( XMLString::equals(currentKind, "shallowHistory") )
{
GetState(fathersIDs.top())->SetShallowHistory(true);
}
// CHOICE & JUNCTION
else if ( (XMLString::equals(currentKind, "choice")) ||
(XMLString::equals(currentKind, "junction")) )
{
currentAttribute = currentElement->getAttribute(xmiID);
currentUUID = XMLString::transcode(currentAttribute);
choicesJunctions.insert(currentUUID);
}
// FORK
else if ( XMLString::equals(currentKind, "fork") )
{
currentAttribute = currentElement->getAttribute(xmiID);
currentUUID = XMLString::transcode(currentAttribute);
forksInc.insert(make_pair(currentUUID, -1));
forksOutc.insert(make_pair(currentUUID, -1));
}
// JOIN
else if ( XMLString::equals(currentKind, "join") )
{
currentAttribute = currentElement->getAttribute(xmiID);
currentUUID = XMLString::transcode(currentAttribute);
joinsInc.insert(make_pair(currentUUID, -1));
joinsOutc.insert(make_pair(currentUUID, -1));
}
}
/** REGION */
else if ( XMLString::equals(currentNode->getNodeName(), region) &&
!(XMLString::equals(currentNode->getNodeName(), regionDot)) &&
!(XMLString::equals(currentNode->getNodeName(), regionSlash)) &&
!(transitionsZone) )
{
if ( (fathersTypes.top() == eAndState) ||
(fathersTypes.top() == eRegion) )
{
currentAttribute = currentElement->getAttribute(name);
currentName = XMLString::transcode(currentAttribute);
currentAttribute = currentElement->getAttribute(xmiID);
currentUUID = XMLString::transcode(currentAttribute);
AddState(currentUUID, currentName, GetState(fathersIDs.top()), true);
fathersIDs.push(statesIndex-1);
lastElement = eRegion;
fathersTypes.push(lastElement);
UpdateStatesMapping(currentUUID, (statesIndex-1));
DD.UpdateStateNames(currentName, (statesIndex-1));
}
}
/** STATE MACHINE */
else if ( XMLString::equals(currentNode->getNodeName(), stateMachine) &&
!(XMLString::equals(currentNode->getNodeName(), stateMachineDot)) &&
!(XMLString::equals(currentNode->getNodeName(), stateMachineSlash)) )
{
if ( states.size() == 1 )
{
currentAttribute = currentElement->getAttribute(name);
currentName = XMLString::transcode(currentAttribute);
states[0]->SetName(currentName);
currentAttribute = currentElement->getAttribute(xmiID);
currentUUID = XMLString::transcode(currentAttribute);
states[0]->SetUUID(currentUUID);
UpdateStatesMapping(currentUUID, 0);
DD.UpdateStateNames(currentName, 0);
}
else
{
std::cout << "l'unica state machine deve essere il top state";
std::cout << std::endl;
}
}
/** REGION TRANSITION */
else if ( XMLString::equals(currentNode->getNodeName(),
regionTransition) &&
!(XMLString::equals(currentNode->getNodeName(),
regionTransitionSlash)) )
transitionsZone = true;
/** DIAGRAM ZONE */
else if ( XMLString::equals(currentNode->getNodeName(), diagram) )
modelZone = false;
}
else // RISALITA
{
// da uno stato
if ( XMLString::equals(currentNode->getNodeName(), state) &&
!(XMLString::equals(currentNode->getNodeName(), stateDot)) &&
!(XMLString::equals(currentNode->getNodeName(), stateSlash)) &&
!(transitionsZone) )
{
currentNodeChildrenList = currentElement->
getElementsByTagName(region);
if ( currentNodeChildrenList->getLength() >= 1 )
{
fathersIDs.pop();
fathersTypes.pop();
}
}
// da una region
else if ( XMLString::equals(currentNode->getNodeName(), region) &&
!(XMLString::equals(currentNode->getNodeName(), regionDot)) &&
!(XMLString::equals(currentNode->getNodeName(), regionSlash)) &&
!(transitionsZone) )
{
if ( fathersTypes.top() == eRegion )
{
fathersIDs.pop();
fathersTypes.pop();
}
}
// da una region transition
else if ( XMLString::equals(currentNode->getNodeName(),
regionTransition) &&
!(XMLString::equals(currentNode->getNodeName(),
regionTransitionSlash)) )
transitionsZone = false;
}
/** acquisizione di nodi non visitati */
if ( (currentNode->hasChildNodes()) && (!ascending) )
currentNode = currentNode->getFirstChild();
else if ( currentNode->getNextSibling() != 0 )
{
currentNode = currentNode->getNextSibling();
ascending = false;
}
else if ( currentNode->getParentNode() != 0 )
{
currentNode = currentNode->getParentNode();
ascending = true;
}
else { break; }
}
builder->release();
delete parser;
XMLPlatformUtils::Terminate();
// aggiungo alle transizioni entranti in un choice o junction pseudostate
// le transizioni da esso uscenti nell'apposito vector
map<string, vector<int> >::const_iterator msvicit;
map<string, vector<int> >::const_iterator msvicit2;
vector<int>::const_iterator vicit;
vector<int>::const_iterator vicit2;
Transition* trwcd; // transizione con choice daughters
for ( msvicit=cjInc.begin(); msvicit!=cjInc.end(); msvicit++ )
{
msvicit2 = cjOutc.find((*msvicit).first);
if ( msvicit2 != cjOutc.end() )
{
for ( vicit=(*msvicit).second.begin();
vicit!=(*msvicit).second.end(); vicit++ )
{
trwcd = GetTransition(*vicit);
for ( vicit2=(*msvicit2).second.begin();
vicit2!=(*msvicit2).second.end(); vicit2++ )
trwcd->AddChoiceDaughter(*vicit2);
}
}
}
/** Inizializzazione del data dictionary */
DD.SetScenario(_fileScenario);
}
|
|
|
|
|
|
#8 |
|
Senior Member
Iscritto dal: Sep 2002
Città: Monza
Messaggi: 598
|
non riesco a fargli vedere
org.apache.xerces.parsers.DOMParser come si fa? ho scaricato i .zip di xerces-2_7_0 ma il file .bat (build.bat) non funziona allora ho preso tutti i file .jar e li ho messi nella directory lib di java. Lo so che è un metodo barbaro, ma come devo fare? grazie
__________________
Nunc est bibendum |
|
|
|
|
|
#9 |
|
Senior Member
Iscritto dal: Dec 2000
Città: bologna
Messaggi: 1309
|
prova nekoHtml(se nn ricordo male basato su xerces)
l'avevo usato tempo fa e nn era male |
|
|
|
|
|
#10 | |
|
Senior Member
Iscritto dal: Sep 2002
Città: Monza
Messaggi: 598
|
Quote:
li ho scaricati e mi sto studiando le classi per vedere se fanno ciò che voglio e come lo fanno. Qualcuno li conosce e sa consigliarmi, magari in base a considerazioni che potrei nn considerare? Grazie
__________________
Nunc est bibendum |
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 17:25.



















